Setting a Static IP Address and Device Name

From Mage
Jump to navigation Jump to search

A quick and dirty guide to setting up a static IP with a reliable device name.

Find MAC address

Find your hardware's MAC address using ifconfig:

ifconfig -a

Example output is shown below. Make a note of the HWaddr of the interface you want to rename. We'll need this MAC address in the next step.

┌(foo@server)─(07:26 AM Tue Jun 19)─(~)
└> ifconfig -a
enp4s0    Link encap:Ethernet  HWaddr 00:1C:C0:AE:B5:E6
          inet addr:192.168.1.100  Bcast:192.168.1.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:41620 errors:0 dropped:0 overruns:0 frame:0
          TX packets:40231 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:21601203 (20.6 MiB)  TX bytes:6145876 (5.8 MiB)
          Interrupt:21 Base address:0xe000 

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:8 errors:0 dropped:0 overruns:0 frame:0
          TX packets:8 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:480 (480.0 b)  TX bytes:480 (480.0 b)


Set udev rules

We're going to set a udev rule that renames the interface upon boot and starts any services that may depend on them. The following rule will look for the MAC address we noted in the previous step (make sure the letters are in lowercase in this file). If found, it will rename the interface from the kernel generated one to the name of our choosing. Here we're renaming enp4s0 to lan0. Avoid using names the kernel may use by default such as eth0.

vi /etc/udev/rules.d/10-lan0.rules

SUBSYSTEM=="net", ACTION=="add", ATTR{address}=="00:1c:c0:ae:b5:e6", NAME="lan0"
ENV{SYSTEMD_WANTS}="network@lan0.service"


Create systemd service

You may have noticed the reference to network@lan0.service in the previous step. This lets udev know to start a systemd service named network@lan0.service after the interface has been renamed to lan0. Systemd can be a true pain in the butt when trying to set services to only come online after a network interface is up, so let's create a framework where services can be specified in the NIC's startup service. We can do this using an old school init.d style script. Because this is launched via udev, we don't need to bother with systemd's [Install] section or actually enabling this service.


vi /etc/systemd/system/network\@lan0.service

[Unit]
Description=Network Connectivity (lan0)

[Service]
Type=oneshot
RemainAfterExit=yes

ExecStart=/etc/network/lan0.sh start

ExecStop=/etc/network/lan0.sh stop


Create the network init script

Remember to set the script to executable only by root after it's creation by issuing:

touch /etc/network/lan0.sh
chown root:root /etc/network/lan0.sh
chmod 700 /etc/network/lan0.sh


vi /etc/network/lan0.sh

#!/bin/bash
#
# Wait for udev renames and bring port lan0 online.

start() {
   if [ -n "`ip link | grep lan0 | grep UP`" ]; then
      echo -n "Attempting to bring up lan0 : Interface lan0 is already up!"
      return
   else
      while [ -z "`ip link | grep lan0 | grep UP`" ]
      do
         echo -n "Attempting to bring up lan0 : "

         # Test to see if the LAN ports have been assigned yet.

         if [ "`ip link | grep lan0`" ]; then
            ip link set dev lan0 up
            ip addr add 192.168.1.100/255.255.255.0 broadcast 192.168.1.255 dev lan0
            # If this is the default link that traffic should be routed through, set the default route.
            ip route add default via 192.168.1.1
            # Enhance the send/receive windows on these routes
            ip route change default via 192.168.1.1 dev lan0 initcwnd 25 initrwnd 25
            ip route change 192.168.1.0/24 dev lan0 proto kernel scope link src 192.168.1.100 initcwnd 25 initrwnd 25

            # Start any services that depend on this NIC HERE
            #systemctl start sshd
         else
            echo "lan0 unavailable, waiting 0.1s."
            sleep 0.1
         fi
      done
   fi

   echo -n "Done."
   return
}

stop() {
   echo -n "Bringing down interface lan0 : "

   # Stop any services that depend on this NIC HERE
   #systemctl stop sshd

   ip route del 192.168.1.0/24 dev lan0 proto kernel scope link src 192.168.1.100
   ip addr flush dev lan0
   ip link set dev lan0 down

   echo -n "Done."
   return
}

status() {
   if [ "`which ifconfig`" ]; then
      ifconfig lan0
      echo
   fi

   ip link show lan0
}

restart() {
   stop
   echo
   start
}

case "$1" in
   start)
      start
      ;;
   stop)
      stop
      ;;
   status)
      status
      ;;
   restart)
      restart
      ;;
   *)
      echo -n "   Usage: ./lan0.sh (start | stop | status | restart)"
      ;;
esac


Reboot

Reboot your machine to verify the changes have been applied correctly. Make sure the NIC has been renamed and that it has been assigned an IP address. Verify that any services that depend on the NIC have started correctly.