Home / Linux / How to configure a static IP address on Linux

How to configure a static IP address on Linux

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.

Loading...

>> Source Link

Check Also

Microsoft, Intel and others are doubling down on open source Linux security

Microsoft is continuing its broad ongoing push to contribute with open source projects, joining the …

%d bloggers like this: