When you need a Linux system to have a static IP address rather than one that is set dynamically by DHCP, all that’s required is some configuration changes and a restart. Follow these steps to make the switch.
IP addresses on Linux systems are often assigned automatically by Dynamic Host Configuration Protocol (DHCP) servers. These are referred to as “dynamic addresses” and may change any time the system is rebooted. When a system is a server or will be remotely administered, however, it is generally more convenient for these systems to have static addresses, providing stable and consistent connections with users and applications.
Fortunately, the steps required to change a Linux system’s IP address from dynamic to static are fairly easy, though they will be a little different depending on the distribution you are using. In this post, we’ll look at how this task is managed on both Red Hat (RHEL) and Ubuntu systems.
There’s no simple command that you can run to determine whether the IP address on a Linux system is assigned by DHCP or static. If it changes when the system restarts, it’s clearly dynamically assigned, but even a dynamic address has some resistance to change. The best way is to look at the configuration file. More on this in the sections below.
RHEL 8
To configure a static IP address on a Red Hat system, let’s start by listing NetworkManager’s connection. The nmcli command shown below will list network connections and devices on the system. Note that the device names and the connection names are not the same.
$ nmcli dev status DEVICE TYPE STATE CONNECTION wlo1 wifi connected Comtrend7FB9 virbr0 bridge connected virbr0 enp3s0 ethernet unavailable -- lo loopback unmanaged -- virbr0-nic tun unmanaged --
To change the network interface from dynamic to static, you need to edit the file in the /etc/sysconfig/network-scripts directory that represents the public interface. In this example, it’s called ifcfg-Comtrend7BF9 (ifcfg- followed by the name of the connection). The boot protocol “BOOTPROTO=dhcp” line needs to be changed to “BOOTPROTO=static”. In addition, the IP address to be used has to be added to the file. The end result will look something like this (NOTE: Don’t add the “arrows” inserted below to highlight the lines you need to focus on):
HWADDR=7C:67:2A:CF:EF:9F ESSID=Comtrend7FB9 MODE=Managed KEY_MGMT=WPA-PSK SECURITYMODE=open MAC_ADDRESS_RANDOMIZATION=default TYPE=Wireless IPADDR=192.168.0.22 <== PROXY_METHOD=none BROWSER_ONLY=no BOOTPROTO=static <== DEFROUTE=yes IPV4_FAILURE_FATAL=no IPV6INIT=yes IPV6_AUTOCONF=yes IPV6_DEFROUTE=yes IPV6_FAILURE_FATAL=no IPV6_ADDR_GEN_MODE=stable-privacy NAME=Comtrend7FB9 UUID=2f5a6217-37c7-449f-bfaa-1d3fa5283482 ONBOOT=yes
Run the command systemctl restart NetworkManager after the changes have been made to make the changes effective.
Ubuntu 18.10
The nmcli (network manager command line interface) command can be used to list the network interfaces on an Ubuntu system. In the output below, we see both a loopback and a public network interface listed. The device on your system may have a different name. This one that reflects the location of the hardware.
Ubuntu> nmcli d DEVICE TYPE STATE CONNECTION enp0s25 ethernet unmanaged -- lo loopback unmanaged --
To examine the network interface configuration settings on an Ubuntu system, you would use a command like this:
Ubuntu> cat /etc/network/interfaces # interfaces(5) file used by ifup(8) and ifdown(8) auto lo iface lo inet loopback auto enp0s25 iface enp0s25 inet dhcp <== dynamically assigned
You can see from the last line in this output that the eth0 interface is currently assigned by DHCP. To change the setting to dynamic, you would change “dhcp” to “static” and add some other lines as well. For example, in the file as shown below, we have changed dhcp to static and specified the IP address we want to use along with other settings:
# interfaces(5) file used by ifup(8) and ifdown(8) auto lo iface lo inet loopback auto enp0s25 iface enp0s25 inet static address 192.168.0.11 netmask 255.255.255.0 network 192.168.0.0 broadcast 192.168.0.255
Restart the networking service or reboot the system to make the changes effective.
Wrap-up
Changing network settings should be done only when the changes will not affect current connections and you can back out the changes if needed. It’s always a good idea to make a copy of any configuration file before you make changes. Giving your backup copy a predictable name such as interfaces.prev, interfaces.orig or interfaces- will make it a little easier to identify.