This tutorial explains how to configure IP address in Linux from ip addr command, nmcli command, nmtui text tool and from nm-connection editor in details with examples.
This tutorial is prepared for Centos / RHEL 7 or higher version. A lot of things have been changed in RHEL 7. Some features explained in this tutorial will not work in earlier version of RHEL / Centos. If you have previous version of RHEL /Centos, please use this tutorial.How to assign IP address in RedHat Linux 6
The most essential part of networking is assigning IP address, default gateway and DNS server IP address. An IP address is the unique identity of a computer in network. Without it computer will not be able to communicate with other computers in network. Before we learn how to assign an IP address, let’s learn how to view the current IP configuration.
How to view the IP address in Linux
To view the current IP configuration we can use ip addr show command. This command supports abbreviate form which means we can use ip a s command instead of ip addr show command. Even more we can use just ip a command. By default this command will list current IP configuration of all interfaces.
Output includes following information :-
Current State :- First line shows the current state of interface. UP keyword indicates that interface is currently UP and functioning properly.
MAC Address :- Next line shows the hardware (MAC) address with broadcast address of interface.
IPv4 Address :- This line shows the current IPv4 configuration (IP Address, Subnet Mask, Broadcast address) on interface.
IPv6 Address :- This line shows the current IPv6 configuration on interface.
Interface Name :- First keyword shows the name of interface. In earlier version NICs were named as eth (Ethernet), em (embedded) and wlan (wireless lan). NICs were numbered based on the order of detection during the system boot. For example first discovered Ethernet (eth) would be named and numbered as eth0. This naming scheme has scalability issue. For example a system which has multiple NICs and NICs are dynamically removed or added, in this situation this naming scheme will work not effectively. From RHEL7 default naming convention for NICs is based on firmware, device, topology and device types. With new naming convention network card names always remain consist. In new naming convention :-
- First two letters represents the type of NIC. For Ethernet interface it always begins with en, for WLAN interfaces it begins with wl, for loopback adapters it starts with lo and for WWAN it begins with ww.
- Next letter represents the location of NIC. For onboard letter o is used, for hot plug slot letter a is used and for PCI slot letter p is used.
- Remaining numeric numbers represents the index, port or ID number.
For example above figure illustrates the name used by NIC is eno16777734. In this name en stands for Ethernet device, o stands for onboard and 16777734 stands for unique index number. Combinedly eno16777736 stands for an onboard Ethernet device (with its unique index number).
To view the IP configuration of a specific interface we can use its name as argument with ip addr show command.
To view only link state, use ip link show command for all interfaces and ip link show [interface] command for a particular interface.
To view the default gateway IP use ip route show command.
To add new (or additional) temporary IP configuration on NIC we can use ip addr add dev [inteface] command. IP configuration added by this command will not be available at next login.
Okay now we know how to view the IP configuration. But have you wonder from where this IP configuration comes? Let’s figure out the answer.
Every interface has an associated configuration file which stores its relative parameters such IP address, protocol and configuration values. These configuration settings are automatically applied on interface while it get activated. RHEL uses a central location for all (NICs) configuration files which is /etc/sysconfig/network-scripts directory.
Configuration file’s name start with ifcfg and followed by the name of network card. For example configuration file for eno16777736 Ethernet card will be ifcfg-eno16777736.
Let’s have a look in this file and understand some important configuration settings
Key directives (configuration settings)
BOOTPROTO :- This directive defines how this interface should receive IP configuration. There are four valid options; dhcp, bootp, none and static.
- dhcp :- to obtain IP address from DHCP Server
- bootp :- to boot off of a network boot server and get IP
- none :- no booting protocol will be used
- static :- to use a static IP address
DEFROUTE :- This directive defines whether to use this interface as default route or not.
IPADDR :- This directive is used to assign the IP address on this interface.
PREFIX :- This directive is used to set the subnet mask.
GATEWAY :- This directive is used to set the default gateway IP address.
DNS1 :- This directive is used to specify the first DNS sever IP address.
ONBOOT :- This directive defines whether to activate this interface at system boot or not.
How to configure IP Address
To configure IP address we have three options; nmtui text tool, nm-connection-editor graphic utility and nmcli command. We will understand all three options one by one. Let’s start with nmtui text tool.
Execute nmtui command from root account.
Select “Edit a connection” and press enter key.
Select NIC from left pane on which you want to set the IP address and select Edit option from right pane and press Enter key.
Select Automatic from IPv4 configuration and press enter key
To obtain IP from DHCP Sever keep Automatic. To set static IP use Manual option.
Navigate to Show option and press Enter key
Set the IP address / subnet mask, Gateway IP address and DNS Server IP address.
Select OK and press Enter key to accept the change
Select Quit and press Enter key
When we exit from nmtui utility, it automatically updates the associated configuration files. For instance in above example we have assigned IP configuration to NIC eno16777736. In this case nmtui will update the /etc/sysconfig/network-scripts/ifcfg-eno16777736 file. An interface restart is required to apply the new configuration. We can use ifdown-[NIC] and ifup-[NIC] commands to restart the interface.
We have successfully assigned the IP address from nmtui text tool.
Now let’s do the same task from nm-connection-editor graphical utility. Login from root account and open terminal. In terminal execute nm-connection-editor command.
From opened window select appropriate NIC and click Edit option
Switch to IPv4 Settings and select Manual option from Method drop down menu. Click Add button and configure IP addresses in respective fields and click Save button and Close button at main screen.
Restart the interface and verify the new IP configuration.
So far we have used two methods to assign the IP addresses.
Finally we will use nmcli command to assign the IP addresses. Login from root account and assign IP addresses as illustrate in following figure.
nmcli con show:- This command will list the available connections with their associated devices.
A device is the network interface card and connection is the configuration used by device. A device can have multiple connections.
As above figure shows connection eno16777736 is associated with Ethernet device eno16777736. We will modify this connection to update the IP configuration.
nmcli con mod eno16777736 ipv4.addresses 192.168.1.100/24 :- This command will set the new IP address and subnet mask.
nmcli con mod eno16777736 ipv4.gateway 192.168.1.1 :- This command will set the new default gateway IP address.
nmcli con mod eno16777736 ipv4.dns 192.168.1.1 :- This command will set the new DNS Server IP address.
As we know new IP configuration applies on start-up. Next two commands will restart the interface.
ifdown eno16777736 :- This command will shut down the interface.
ifup eno16777736 :- This command will bring up the device back.
- nm-connection-editor is available only in X-Window system. It provides less but sufficient features for end users to manage the network connections. Network administrators rarely use this tool to manage the network connections.
- Usually nmcli command and nmtui tool are used to manage the network connection.
- Among these options, nmcli command is the most powerful.
- nmcli command is little bit hard to learn but works in every condition.
- In exam you can use nmtui text tool instead of nmcli command.
- For real life system administration its worth to learn nmcli command as it is available in most situations such as rescue mode, remote connection, terminal etc.