Networking https://datastorageguy.com Consulting Services and Tech-Tips from Ben Patridge Mon, 11 Sep 2023 21:56:31 +0000 en-US hourly 1 https://wordpress.org/?v=6.9.1 https://datastorageguy.com/wp-content/uploads/2025/11/cropped-dsgplatter-32x32.png Networking https://datastorageguy.com 32 32 189268276 Display ethernet devices on a Fedora system using the Linux Pseudo Filesystem and create a static IP Address https://datastorageguy.com/2023/09/11/display-ethernet-devices-on-a-fedora-system-using-the-linux-pseudo-filesystem-and-create-a-static-ip-address/ Mon, 11 Sep 2023 21:56:31 +0000 https://datastorageguy.com/?p=267 Encountered a fun riddle recently whereby on a Fedora 34 system I needed to locate all ethernet interfaces on a system that are obtaining a DHCP address, then after the first reboot create a static IP address.

I am a big fan of looking thru the Linux pseudo filesystem in /sys

In this instance I wanted to exclude any Virtual, Bridge and Local Interfaces.

Therefore I do the following:

Listing all Interfaces in sysfs

# ls /sys/class/net|egrep -v "^vir|^lo|^wl|^br"

Example

# ls /sys/class/net|egrep -v "^vir|^lo|^wl|^br"
eno1

PART 1 – CHANGE FROM DHCP TO STATIC IP ADDRESS

To change from DHCP to a STATIC IP Address

First display the current interfaces

# nmcli con show
NAME    UUID                                  TYPE      DEVICE
enp1s0  46145016-ead7-4128-8d91-4b0b828162db  ethernet  enp1s0

and to set a STATIC IP with NAME “System-NIC-110” the format would be

# nmcli con add con-name "System-NIC-110" ifname enp1s0 type ethernet ip4 192.168.122.110/24 gw4 192.168.122.1

Then you would delete the original interface afterwards

# nmcli con del enp1s0

Then either reboot or restart networking

# nmcli networking off
# nmcli networking on

Now you will see the new interface name.

# nmcli con show
NAME            UUID                                  TYPE      DEVICE
System-NIC-110  fc1ca250-1481-4014-82bd-ad97d1c48ae4  ethernet  enp1s0

PART II – Script method to change all interfaces after first reboot

Here is a script example how to iterate thru all devices on the system and create static IP addresses:

#!/bin/bash
###################################################################
# SCRIPT BY The Data Storage Guy (https://datastorageguy.com)
# As an example how iterate thru all ethernet devices on a Fedora
# system and assign Static IP Addresses with a custom name
#
# location: /usr/local/bin/set-static-ip.sh
###################################################################
# The following will go thru all of the ethernet devices found
#
# It is used to on create static IP addresses with connection NAME
#
# $connection_prefix-$starting_octet
#
# with IP address using the format
#
# $ip_subnet.$starting_octet
#
# It will also increment should there be more
# than one ethernet device found
#
# the parameters are used within the following configuration file
###################################################################
connection_prefix="System-NIC"
ip_subnet="192.168.122"
starting_octet="110"
mask="24"
gateway="192.168.122.1"
###################################################################
PS4='${LINENO}: '
###################################################################
SCRIPT=$(basename ${BASH_SOURCE[0]})
###################################################################
export PATH=$PATH:/usr/bin/
log=/var/log/$(echo $SCRIPT|cut -d. -f1).log
line="-----------------------------------------------"
step=1
n=1
###################################################################
logm() {
        LOGDATE=`date '+%Y-%m-%dT%H:%M:%S'`
        echo $line |tee -a $log
        echo "[$LOGDATE] [$step]  $1" |tee -a $log |tee >(logger -t $SCRIPT)
        ((step++))
}
###################################################################
logm "Displaying Network General Status"
logm "Issuing command: nmcli general status"
nmcli general status  >>$log

logm "Displaying Network Interfaces"
logm "Issuing command: nmcli con show"
nmcli con show |tee -a $log | logger -t $SCRIPT

while read ethname; do
        ip="${ip_subnet}.${starting_octet}"
        UUID=$(nmcli conn show |awk -v iface=$ethname '$NF ~ iface {print $(NF-2)}')
        logm "Setting $ethname ip address to ${ip}/24 with name "${connection_prefix}-$ip" for ethernet $ethname UUID=$UUID"
        logm "Issuing Command:  nmcli con add con-name "${connection_prefix}-$starting_octet" ifname $ethname type ethernet ip4 ${ip}/$mask gw4 $gateway"
        nmcli con add con-name "${connection_prefix}-$starting_octet" ifname $ethname type ethernet ip4 ${ip}/$mask gw4 $gateway  |tee -a $log
        if [ $? == 0 ]; then
                logm "Successfully added con-name ${connection_prefix}-$starting_octet"
        else
                logm "ERROR: Problem adding con-name ${connection_prefix}-$starting_octet"
        fi
        sleep .2
        logm "Deleting $ethname: nmcli con del $ethname"
        logm "Issuing command: nmcli con del $ethname"
        nmcli con del $ethname |tee -a $log
        if [ $? == 0 ]; then
                logm "Successfully deleted $ethname: nmcli con del $ethname"
        else
                logm "ERROR: Unable to delete $ethname: nmcli con del $ethname"
        fi
        ((n++))
        ((starting_octet++))
done< <(ls /sys/class/net|egrep -v "^vir|^lo|^wl|^br")

logm "Displaying Network Interfaces"
logm "Issuing command: nmcli con show"
nmcli con show |tee -a $log |logger -t $SCRIPT

logm "Checking for the install cron job entry"
logm "Issuing command: crontab -l"
crontab -l |tee -a $log|logger -t $SCRIPT

logm "Restarting Network Services"
logm "Issuing command: nmcli networking off"
nmcli networking off |tee -a $log
sleep .2
logm "Issuing command: nmcli networking on"
nmcli networking on |tee -a $log
sleep 2
logm "Displaying Network Interfaces"
logm "Issuing command: ncmli con show"
nmcli con show |tee -a $log |logger -t $SCRIPT

if [ $(nmcli con show|grep -q $connection_prefix;echo $?) == 0 ]; then
        logm "GOOD: Found $connection_prefix Network Device"
        crontab -l |tee -a $log
        if [ $? == 0 ]; then
                logm "Sleeping for 15 seconds... then removing cron job"
                sleep 15
                logm "Removing cron jobs"
                logm "Issuing command: crontab -r"
                crontab -r &>/dev/null
        fi
else
        logm "ERROR: Unable to find $connection_prefix Network device"
fi


logm "Completed Network Setting Setup"
###################################################################

Now we create a cron entry to run only at reboot and log to /root/cron.log

@reboot  /usr/bin/bash /usr/local/bin/set-static-ip.sh >> /root/cron.log

Now when we reboot we see the new network settings.

# nmcli con show
NAME            UUID                                  TYPE      DEVICE
System-NIC-110  62a904ab-2be2-4223-bc13-0b7daba2409e  ethernet  enp1s0
System-NIC-111  350008d7-f075-4ca5-ad97-f5ba8e6f20d0  ethernet  enp7s0

The /var/log/set-static-ip.log shows the following script steps

# cat /var/log/set-static-ip.log
-----------------------------------------------
[2023-09-11T13:28:20] [1]  Displaying Network General Status
-----------------------------------------------
[2023-09-11T13:28:20] [2]  Issuing command: nmcli general status
STATE                  CONNECTIVITY  WIFI-HW  WIFI     WWAN-HW  WWAN
connected (site only)  limited       enabled  enabled  enabled  enabled
-----------------------------------------------
[2023-09-11T13:28:20] [3]  Displaying Network Interfaces
-----------------------------------------------
[2023-09-11T13:28:20] [4]  Issuing command: nmcli con show
NAME    UUID                                  TYPE      DEVICE
enp1s0  197a76a0-a28a-4db4-8e64-a90557e47c9f  ethernet  enp1s0
enp7s0  d0cf6083-8f35-4be1-8705-95ac804eecac  ethernet  enp7s0
-----------------------------------------------
[2023-09-11T13:28:20] [5]  Setting enp1s0 ip address to 192.168.122.110/24 with name System-NIC-192.168.122.110 for ethernet enp1s0 UUID=197a76a0-a28a-4db4-8e64-a90557e47c9f
-----------------------------------------------
[2023-09-11T13:28:20] [6]  Issuing Command:  nmcli con add con-name System-NIC-110 ifname enp1s0 type ethernet ip4 192.168.122.110/24 gw4 192.168.122.1
Connection 'System-NIC-110' (6bd11875-8971-41d0-9089-c3caebc02bb0) successfully added.
-----------------------------------------------
[2023-09-11T13:28:20] [7]  Successfully added con-name System-NIC-110
-----------------------------------------------
[2023-09-11T13:28:20] [8]  Deleting enp1s0: nmcli con del enp1s0
-----------------------------------------------
[2023-09-11T13:28:20] [9]  Issuing command: nmcli con del enp1s0
Connection 'enp1s0' (197a76a0-a28a-4db4-8e64-a90557e47c9f) successfully deleted.
-----------------------------------------------
[2023-09-11T13:28:20] [10]  Successfully deleted enp1s0: nmcli con del enp1s0
-----------------------------------------------
[2023-09-11T13:28:20] [11]  Setting enp7s0 ip address to 192.168.122.111/24 with name System-NIC-192.168.122.111 for ethernet enp7s0 UUID=d0cf6083-8f35-4be1-8705-95ac804eecac
-----------------------------------------------
[2023-09-11T13:28:20] [12]  Issuing Command:  nmcli con add con-name System-NIC-111 ifname enp7s0 type ethernet ip4 192.168.122.111/24 gw4 192.168.122.1
Connection 'System-NIC-111' (8eee96f7-8a42-4bb4-b57a-e15d222743f1) successfully added.
-----------------------------------------------
[2023-09-11T13:28:20] [13]  Successfully added con-name System-NIC-111
-----------------------------------------------
[2023-09-11T13:28:20] [14]  Deleting enp7s0: nmcli con del enp7s0
-----------------------------------------------
[2023-09-11T13:28:20] [15]  Issuing command: nmcli con del enp7s0
Connection 'enp7s0' (d0cf6083-8f35-4be1-8705-95ac804eecac) successfully deleted.
-----------------------------------------------
[2023-09-11T13:28:20] [16]  Successfully deleted enp7s0: nmcli con del enp7s0
-----------------------------------------------
[2023-09-11T13:28:20] [17]  Displaying Network Interfaces
-----------------------------------------------
[2023-09-11T13:28:20] [18]  Issuing command: nmcli con show
NAME            UUID                                  TYPE      DEVICE
System-NIC-110  6bd11875-8971-41d0-9089-c3caebc02bb0  ethernet  enp1s0
System-NIC-111  8eee96f7-8a42-4bb4-b57a-e15d222743f1  ethernet  enp7s0
virbr0          02b8c655-36de-4379-b096-b48de5941117  bridge    virbr0
-----------------------------------------------
[2023-09-11T13:28:21] [19]  Checking for the install cron job entry
-----------------------------------------------
[2023-09-11T13:28:21] [20]  Issuing command: crontab -l
@reboot  /usr/bin/bash /usr/local/bin/set-static-ip.sh >> /root/cron.log
-----------------------------------------------
[2023-09-11T13:28:21] [21]  Restarting Network Services
-----------------------------------------------
[2023-09-11T13:28:21] [22]  Issuing command: nmcli networking off
-----------------------------------------------
[2023-09-11T13:28:21] [23]  Issuing command: nmcli networking on
-----------------------------------------------
[2023-09-11T13:28:21] [24]  Displaying Network Interfaces
-----------------------------------------------
[2023-09-11T13:28:21] [25]  Issuing command: ncmli con show
NAME            UUID                                  TYPE      DEVICE
System-NIC-110  6bd11875-8971-41d0-9089-c3caebc02bb0  ethernet  enp1s0
System-NIC-111  8eee96f7-8a42-4bb4-b57a-e15d222743f1  ethernet  enps7
-----------------------------------------------
[2023-09-11T13:28:21] [26]  GOOD: Found System-NIC Network Device
@reboot  /usr/bin/bash /usr/local/bin/set-static-ip.sh >> /root/cron.log
-----------------------------------------------
[2023-09-11T13:28:21] [27]  Sleeping for 15 seconds... then removing cron job
-----------------------------------------------
[2023-09-11T13:28:36] [28]  Removing cron jobs
-----------------------------------------------
[2023-09-11T13:28:36] [29]  Issuing command: crontab -r
-----------------------------------------------
[2023-09-11T13:28:36] [30]  Completed Network Setting Setup
]]>
267
List only ethernet Network Adapters on Debian/Fedora https://datastorageguy.com/2023/09/07/list-only-ethernet-network-adapters-on-debian-fedora/ Thu, 07 Sep 2023 19:14:12 +0000 https://datastorageguy.com/?p=257 Sometimes you want a quick way to verify all ethernet network adapters that are seen by the kernel at boot time. I find going directly to the pseudo filesystem is the fastest.

For example.

# ls /sys/class/net|egrep -v "^vir|^lo|^wl"
enp1s0

Similarly, you an modify the egrep accordingly if you want to view wifi and ethernet adapters and exclude any bridge or virtual adapters

# ls /sys/class/net|egrep -v "^v|^b|lo"
eno1
wlp3s0

]]>
257
Telnet to a port on a remote machine without ncat or telnet, or nmap https://datastorageguy.com/2022/05/19/telnet-to-a-port-on-a-remote-machine-without-ncat-or-telnet-or-nmap/ Thu, 19 May 2022 23:59:01 +0000 https://datastorageguy.com/?p=160 There are situations where you may be working on Linux based system, and telnet, ncat or nmap does not exist.

In Linux, you can route a test thru the protocol, destination IP address, and destination  port using the “/dev” linux ‘pseudo device’ 

The ‘telnet’ command we are all familiar with is essentially a wrapper for this procedure.

This can be done safely without impact to system operation.

# echo > /dev/tcp/10.10.154.121/10600 && echo "TCP Port 10600 is open"

The same works with UDP

 echo > /dev/udp/10.10.154.121/111 && echo "UDP Port 111 is open"

To test multiple TCP ports via for loop:

# for i in 88 111 135 139 445 464 3268 3269 ; do
echo > /dev/tcp/10.10.154.133/$i && echo "TCP Port $i is open"
done

]]>
160
Obtain TCP Network netstat output without netstat https://datastorageguy.com/2022/05/19/obtain-tcp-network-netstat-output-without-netstat/ Thu, 19 May 2022 18:08:50 +0000 https://datastorageguy.com/?p=137

There are times you may be on a system and for whatever insane reason, netstat is not installed.

Here is how you can obtain interface information without netstat

# awk '(f==0) { i=1; while ( i<=NF) {n[i] = $i; i++ }; f=1; next} (f==1){ i=2; while ( i<=NF){ printf "%s = %d\n", n[i], $i; i++}; f=0}'  /proc/net/netstat |column -t

Example results

# awk '(f==0) { i=1; while ( i<=NF) {n[i] = $i; i++ }; f=1; next} (f==1){ i=2; while ( i<=NF){ printf "%s = %d\n", n[i], $i; i++}; f=0}'  /proc/net/netstat |column -t
SyncookiesSent             =  0
SyncookiesRecv             =  0
SyncookiesFailed           =  5
EmbryonicRsts              =  0
PruneCalled                =  0
RcvPruned                  =  0
OfoPruned                  =  0
OutOfWindowIcmps           =  0
LockDroppedIcmps           =  0
ArpFilter                  =  0
TW                         =  1160
TWRecycled                 =  0
TWKilled                   =  0
PAWSPassive                =  0
PAWSActive                 =  0
PAWSEstab                  =  5
DelayedACKs                =  25254
DelayedACKLocked           =  21
DelayedACKLost             =  4129
ListenOverflows            =  0
ListenDrops                =  0
TCPPrequeued               =  419476
TCPDirectCopyFromBacklog   =  7131447
TCPDirectCopyFromPrequeue  =  70660208
TCPPrequeueDropped         =  0
TCPHPHits                  =  650154
TCPHPHitsToUser            =  35433
TCPPureAcks                =  241811
TCPHPAcks                  =  29236
TCPRenoRecovery            =  0
TCPSackRecovery            =  21
TCPSACKReneging            =  0
TCPFACKReorder             =  0
]]>
137