Display ethernet devices on a Fedora system using the Linux Pseudo Filesystem and create a static IP Address

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
This entry was posted in Command Line FU, Linux, Networking and tagged , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *