How to configure IP address in Linux

An IP address is the software address of the computer. Two computers can communicate only if they have IP addresses. There are two ways to configure an IP address on Linux: temporary and permanent. A temporary IP address works only in the current login session. Linux stores it in the RAM and removes it when you log out from the current session. A permanent IP address works until a service or you manually update or change it. Linux stores it in a configuration file.

Setting a temporary IP address

To set a temporary IP address, we use the 'ip addr' command. This command adds the new IP address to the specified interface. It does not remove or update the existing IP address. It appends the current IP configuration.

Without any argument, it displays the IP configurations of all network devices.

ip addr

To view the IP configuration of a particular device, specify the name of the interface as an argument after the show option. The following command displays the IP configuration of the ens160 interface.

ip addr show ens160

To use this command to add a temporary IP address, we need to use the 'add' and 'dev' options with the command.

ip addr add [ip_address/subnet_mask] dev [interface_name]

Specify the IP address with the subnet mask after the add option and the name of the interface after the dev option.

The following command adds the IP address 192.168.1.10/24 to the ens160 interface.

ip addr add 192.168.1.10 255.255.255.0 dev ens160

To verify the new IP address, you can check the IP configuration of the device again.

ip addr ens160

The following image shows the above exercise.

ip addr command

To learn more about the 'ip addr' command, you can check the following tutorial.

Linux ip address Command Usages and Examples

Setting a permanent IP address

There are four methods to configure a permanent IP address. These methods are: -

  • Changing the configuration file
  • Using the nmcli command
  • Using the nmtui utility
  • Using the nn-connection-editor tool

Let's discuss each method in detail.

Changing IP addresses in configuration files

Linux uses a configuration file for each interface to store its configuration. It stores all configuration files in /etc/sysconfig/network-scripts directory. For the naming convention of interface configuration files, it uses the 'ifcfg' prefix. After this prefix, it uses the name of the network card. For example, if the interface's name is ens160, it will store its configuration in the ifcfg-ens160 file.

network configuration file

To configure a new IP address or change existing IP addresses, we use the IPADDR directive in this file. You can use any text editor to edit or update this file.

The following image shows how to change the existing IP address by editing the configuration file.

changing configuration files

Linux reads this file only when the interface starts. It does not actively monitor this file. If you change this file, you need to restart the interface to force Linux to reread the configuration file.

To force Linux to reread this file, you can use the ifdown and ifup commands. Specify the interface name as the argument with these commands.

#ifdown ens160
#ifdown ens160

The following image shows the above exercise.

restart interface to update the ip address

Using the nmcli command to configure IP addresses

If you do not want to edit the configuration file directly, you can use NetworkManager's tools. NetworkManager is the default network management service on Linux. It provides three tools for network configuration management. These tools are nmcli, nmtui, and nm-connection-editor.

NetworkManager uses the term 'connection' to refer to a network configuration file. It allows us to create multiple connections (configuration files) for the same interface. Multiple connections allow us to connect different networks without changing the IP configuration. For example, if you use your laptop at home and office, you can create two connections for your wireless interface: one for the home and another for the office. NetworkManager will automatically select the connection based on your location. If you use the laptop at the home, it will use the home connection, or if you use the laptop at the office, it will use the office connection.

To learn more about the NetworkManager, you can check the following tutorial.

Understanding Linux Network Manager Fundamental

Using the nmcli command to configure IP addresses

Use the following command to list all connections (configuration files) of the interface.

#nmcli con show [interface name]

The modify option allows us to modify all parameters of the connection. We can use this option to update the IP address. The following command sets the IP address to 192.168.1.100/24.

#nmcli con modify ens160 ipv4.address 192.168.1.100/24

After updating the IP address, use the following commands to restart the connection.

#nmcli con down ens160
#nmcli con up ens160

Now, check the IP address again.

#ip addr show ens160

The following image shows the above exercise.

using nmcli command to configure ip address

To learn more about the nmcli command, you check the following tutorial.

The nmcli command on Linux Examples and Usages

Using the nmtui utility to update IP addresses

The nmtui is a curses-based utility. It allows us to manage interfaces. The following command starts it.

#nmtui

nmtui command

Select the "Edit a connection" option and press the Enter key.

edit a connection

Select the NIC from the left pane, select the Edit option from the right pane, and press the Enter key.

select nic

Select the Automatic option from the IPv4 configuration option and press the Enter key.

automatic ip dhcp option

To obtain IP configuration from the DHCP Server, select the Automatic option. To set the IP configuration manually, use the Manual option.

dhcp option

Select the Show option and press the Enter key. Use the Tab key to switch between options.

show option

Set the IP address/subnet mask, Gateway IP address, and DNS Server IP address.

set ip adddress

Select the OK option and press the Enter key to accept the change.

confirm change

Select the Quit option and press the Enter key.

quit nmtui

When we exit the nmtui utility, it automatically applies the changes and updates the associated network configuration files. In the above example, since we assigned IP configuration to the eno16777736 interface, the nmtui utility updates the file /etc/sysconfig/network-scripts/ifcfg-eno16777736.

An interface restart is required to apply the new configuration. To restart the interface, use the ifdown-[NIC] and ifup-[NIC] commands.

verifing ip address update

To learn more about the nmtui utility, you check the following tutorial.

The nmtui Command and Utility on Linux

Using the nm-connection-editor graphical utility

The nm-connection-editor is a desktop tool. It works only on desktop. Open a terminal, and run the following command to start it.

#nm-connection-editor

From the opened window, select the appropriate NIC and click the Edit option

nm-connection-editor

Now use the following steps to add/update/edit IP configuration on the selected interface.

  • Switch to the IPv4 Settings.
  • Select the Manual option from the Method drop-down menu.
  • Click the Add button and configure IP addresses in respective fields.
  • Click the Save button.
  • Click the Close button on the main screen.

edit a connection

Restart the interface and verify the new IP configuration.

verify update

To learn more about the nm-connection-editor tool, you check the following tutorial.

The nm-connection-editor command on Linux

That's all for this tutorial. In this tutorial, we learned how to configure new IP addresses and manage existing IP addresses on Linux.

ComputerNetworkingNotes Linux Tutorials How to configure IP address in Linux