How to configure IP address in Linux

This tutorial explains how to configure an IP address in Linux. Learn how to use the nmcli, nmtui and nm-connection-editor in detail with practical examples.

IP configurations can be assigned to an interface in two ways; temporary and permanent. In previous parts of this article, we learned how to assign a temporary IP address along with how to view the current IP configuration.

In this tutorial, we will learn how to permanently configure or assign an IP address. To manage the IP configuration permanently, we have three tools or utilities; nmtui, nmcli, and nm-connection-editor. Let's understand how to use each of these tools.

Using the nmtui command for IP configuration

Access a root shell and run the nmtui command.

nmtui command

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

nmtui selecting interface

Select that NIC from the left pane, on which you want to set the IP address and select the Edit option from the right pane and press the Enter key.

nmtui edit connection

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

nmtui automatic option

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

connection type manual

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

nmtui show manual option

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

set ip configuration manually

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

nmtui ip assigned

Select the Quit option and press the Enter key.

quit from nmtui interface

When we exit from the nmtui utility, it automatically applies the changes on related interfaces and updates the associated network configuration files. For instance in above task the nmtui updates the /etc/sysconfig/network-scripts/ifcfg-eno16777736 file as we assigned IP configuration to the eno16777736 interface.

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

nmtui verified

We have successfully assigned the IP address from the nmtui utility.

Using the nm-connection-editor graphical utility

Now let's do the same task from the nm-connection-editor graphical utility. Access the root shell and execute the nm-connection-editor command.

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

starting 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 at the main screen.

setting-up ip address on interface

Restart the interface and verify the new IP configuration.

IP-configuration verification

Using the nmcli command to assign the IP addresses

To assign IP address from command line, you can use the nmcli command. This command the following syntax.

#nmcli con mod [interface-name] ipv4.address [IP-address/subnet-mask]
#nmcli con mod [interface-name] ipv4.gateway [IP-address of gateway]
#nmcli con mod [interface-name] ipv4.dns [IP-address of DNS server]

To know the name of interface, you can use the "nmcli con show" or "show ip addr" commands.

The following image shows an example of the "nmcli con mod" command.

example of nmcli command

The commands, used in this example, are explained below.

nmcli con show:-
This command lists the available connections with their associated devices name.

A device is the network interface card and a connection is a configuration used by that device. A device can have multiple connections.

As the above image shows, the connection eno16777736 is associated with the Ethernet device named eno16777736. We modified this connection to set the new IP configuration.

nmcli con mod eno16777736 ipv4.addresses :-
This command sets the new IP address and subnet mask on the eno16777736 interface.

nmcli con mod eno16777736 ipv4.gateway :-
This command sets the new default gateway IP address for the eno16777736 interface.

nmcli con mod eno16777736 ipv4.dns :-
This command sets the new DNS Server IP address for the eno16777736 interface.

As we know, new IP configuration applies when the interface is activated. The next two commands are used to restart the interface.

ifdown eno16777736 :-
This command shut down the interface.

ifup eno16777736 :-
This command brings up the interface back.

Key points
  • The nm-connection-editor is available only in the X-Window system. It provides less but sufficient features for end users to manage the network connections. Network administrators rarely use this tool for managing IP configuration.
  • Usually network administrators use the nmcli command and nmtui tool to manage the network connection.
  • Among these options, the nmcli command is the most powerful.
  • The nmcli command is little bit hard to learn and use but works in every situation.
  • In the exam, you can use the nmtui text tool instead of the nmcli command.

That's all for this tutorial. If you like this tutorial, please don't forget to share it from your favorite social networking site.

ComputerNetworkingNotes Linux Tutorials How to configure IP address in Linux