Translate

Archives

Configuring RHEL7 Networking Using nmcli

The nmcli utility is a very useful for managing and configuring NetworkManager. It is what I recommend people use to configure networking interfaces during the RHSCA7 and RHCE7 exams rather than using nmtui, nm-applet, or directly editing an interface configuration file.

From the nmcli(1) manpage:

nmcli is a command-line tool for controlling NetworkManager and getting its status. It is not meant as a replacement of nm-applet or other similar clients. Rather it’s a complementary utility to these programs. The main nmcli’s usage is on servers, headless machines or just for power users who prefer the command line.

This post shows you how to use the nmcli utility to rename and assign static IPv4 addresses to a networking interface in a newly created CentOS 7.0 VM running on VMware Workstation 14.

To view the status of networking interfaces:

# nmcli dev status
DEVICE       TYPE      STATE      CONNECTION 
eno16777736  ethernet  connected  eno16777736       
lo           loopback  unmanaged  --         
# 

# nmcli connection show
NAME         UUID                                  TYPE            DEVICE      
eno16777736  570099b5-a252-4adc-ade7-83c6f440cc93  802-3-ethernet  eno16777736 

# nmcli connection show eno16777736 
connection.id:                          eno16777736
connection.uuid:                        570099b5-a252-4adc-ade7-83c6f440cc93
connection.interface-name:              --
connection.type:                        802-3-ethernet
connection.autoconnect:                 yes
connection.timestamp:                   1571045800
connection.read-only:                   no
connection.permissions:                 
connection.zone:                        --
connection.master:                      --
connection.slave-type:                  --
connection.secondaries:                 
connection.gateway-ping-timeout:        0
802-3-ethernet.port:                    --
802-3-ethernet.speed:                   0
802-3-ethernet.duplex:                  --
802-3-ethernet.auto-negotiate:          yes
802-3-ethernet.mac-address:             00:0C:29:BA:1A:27
802-3-ethernet.cloned-mac-address:      --
802-3-ethernet.mac-address-blacklist:   
802-3-ethernet.mtu:                     auto
802-3-ethernet.s390-subchannels:        
802-3-ethernet.s390-nettype:            --
802-3-ethernet.s390-options:            
ipv4.method:                            auto
ipv4.dns:                               
ipv4.dns-search:                        
ipv4.addresses:                         
ipv4.routes:                            
ipv4.ignore-auto-routes:                no
ipv4.ignore-auto-dns:                   no
ipv4.dhcp-client-id:                    --
ipv4.dhcp-send-hostname:                yes
ipv4.dhcp-hostname:                     --
ipv4.never-default:                     no
ipv4.may-fail:                          yes
ipv6.method:                            auto
ipv6.dns:                               
ipv6.dns-search:                        
ipv6.addresses:                         
ipv6.routes:                            
ipv6.ignore-auto-routes:                no
ipv6.ignore-auto-dns:                   no
ipv6.never-default:                     no
ipv6.may-fail:                          yes
ipv6.ip6-privacy:                       -1 (unknown)
ipv6.dhcp-hostname:                     --
GENERAL.NAME:                           eno16777736
GENERAL.UUID:                           570099b5-a252-4adc-ade7-83c6f440cc93
GENERAL.DEVICES:                        eno16777736
GENERAL.STATE:                          activated
GENERAL.DEFAULT:                        yes
GENERAL.DEFAULT6:                       no
GENERAL.VPN:                            no
GENERAL.ZONE:                           --
GENERAL.DBUS-PATH:                      /org/freedesktop/NetworkManager/ActiveConnection/0
GENERAL.CON-PATH:                       /org/freedesktop/NetworkManager/Settings/0
GENERAL.SPEC-OBJECT:                    --
GENERAL.MASTER-PATH:                    --
IP4.ADDRESS[1]:                         ip = 10.10.10.146/24, gw = 10.10.10.5
IP4.DNS[1]:                             10.10.10.5
IP4.DOMAIN[1]:                          localdomain
DHCP4.OPTION[1]:                        requested_domain_search = 1
DHCP4.OPTION[2]:                        requested_nis_domain = 1
DHCP4.OPTION[3]:                        requested_time_offset = 1
DHCP4.OPTION[4]:                        requested_broadcast_address = 1
DHCP4.OPTION[5]:                        requested_rfc3442_classless_static_routes = 1
DHCP4.OPTION[6]:                        requested_classless_static_routes = 1
DHCP4.OPTION[7]:                        requested_domain_name = 1
DHCP4.OPTION[8]:                        expiry = 1571047308
DHCP4.OPTION[9]:                        domain_name = localdomain
DHCP4.OPTION[10]:                       next_server = 10.10.10.254
DHCP4.OPTION[11]:                       broadcast_address = 10.10.10.255
DHCP4.OPTION[12]:                       dhcp_message_type = 5
DHCP4.OPTION[13]:                       requested_subnet_mask = 1
DHCP4.OPTION[14]:                       dhcp_lease_time = 1800
DHCP4.OPTION[15]:                       routers = 10.10.10.5
DHCP4.OPTION[16]:                       ip_address = 10.10.10.146
DHCP4.OPTION[17]:                       requested_static_routes = 1
DHCP4.OPTION[18]:                       requested_interface_mtu = 1
DHCP4.OPTION[19]:                       requested_nis_servers = 1
DHCP4.OPTION[20]:                       requested_wpad = 1
DHCP4.OPTION[21]:                       requested_ntp_servers = 1
DHCP4.OPTION[22]:                       requested_domain_name_servers = 1
DHCP4.OPTION[23]:                       domain_name_servers = 10.10.10.5
DHCP4.OPTION[24]:                       requested_ms_classless_static_routes = 1
DHCP4.OPTION[25]:                       requested_routers = 1
DHCP4.OPTION[26]:                       subnet_mask = 255.255.255.0
DHCP4.OPTION[27]:                       network_number = 10.10.10.0
DHCP4.OPTION[28]:                       requested_host_name = 1
DHCP4.OPTION[29]:                       dhcp_server_identifier = 10.10.10.254
IP6.ADDRESS[1]:                         ip = fe80::20c:29ff:feba:1a27/64, gw = ::
#

Bring down and up an interface:

# nmcli connection show
NAME         UUID                                  TYPE            DEVICE      
eno16777736  570099b5-a252-4adc-ade7-83c6f440cc93  802-3-ethernet  eno16777736 
# nmcli connection down eno16777736 
# nmcli connection show
NAME         UUID                                  TYPE            DEVICE 
eno16777736  570099b5-a252-4adc-ade7-83c6f440cc93  802-3-ethernet  --     
# nmcli connection up eno16777736 
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/1)
# nmcli connection show
NAME         UUID                                  TYPE            DEVICE      
eno16777736  570099b5-a252-4adc-ade7-83c6f440cc93  802-3-ethernet  eno16777736 
# 

Rename an interface to eth0:

# nmcli connection delete eno16777736 
# nmcli connection show
NAME  UUID  TYPE  DEVICE 
# nmcli connection add type ethernet con-name eth0 ifname eno16777736
Connection 'eth0' (ed802511-2a71-4069-bc4b-9a9e2918ef31) successfully added.
# nmcli connection show
NAME  UUID                                  TYPE            DEVICE      
eth0  ed802511-2a71-4069-bc4b-9a9e2918ef31  802-3-ethernet  eno16777736 
# 

Assign static IPv4 addresses to interface instead of using DHCP-supplied addresses:

# nmcli connection modify eth0 ipv4.addresses "10.10.10.146/24 10.10.10.5" ipv4.dns 10.10.10.146
# nmcli con show eth0 | grep ipv4\.addresses
ipv4.addresses:              { ip = 10.10.10.146/24, gw = 10.10.10.5 }
#

Note that RHEL 7.0 and CentOS 7.0 do not support the ipv4.gateway option; thus you need to use this syntax ipv4.addresses “<ip addr and mask> <gateway>” as shown above.

Set the connection to not use DHCP:

# nmcli connection modify eth0 ipv4.method manual
# nmcli connection down eth0 
# nmcli connection up eth0 
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/1)
# nmcli connection show 
NAME  UUID                                  TYPE            DEVICE      
eth0  ed802511-2a71-4069-bc4b-9a9e2918ef31  802-3-ethernet  eno16777736 
# 

Here is the contents the eth0 interface configuration file after the previous nmcli operations:

# cat /etc/sysconfig/network-scripts/ifcfg-eth0
TYPE=Ethernet
BOOTPROTO=none
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
NAME=eth0
UUID=ed802511-2a71-4069-bc4b-9a9e2918ef31
DEVICE=eno16777736
ONBOOT=yes
DNS1=10.10.10.146
IPADDR=10.10.10.146
PREFIX=24
GATEWAY=10.10.10.5
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
#

Assuming the OS is RHEL 7.0 or CentOS 7.0, the device is eno16777736, the current connection name is “Wired connection 1”, and you want to rename it to “eth0” with a static address of 10.10.10.148, subnet mask 255.255.255.0, gateway 10.10.10.5 and DNS 10.10.10.146, here is the minimum list of nmcli commands you need to run:

# nmcli con delete "Wired connection 1"
# nmcli connection add type ethernet con-name eth0 ifname eno16777736
# nmcli connection modify eth0 ipv4.addresses "10.10.10.148/24 10.10.10.5" ipv4.dns 10.10.10.146
# nmcli connection modify eth0 ipv4.method manual
# nmcli connection down eth0; nmcli connection up eth0

Here is a bash shell script which automates the necessary modifications to an interface:

#!/bin/bash

# customize as necessary
OLD="Wired connection 1"
NEW="eth1"
DEVICE="eno16777736"
GATEWAY="10.10.10.5"
ADDRESS="10.10.10.146/24 ${GATEWAY}"

nmcli con delete "$OLD"
nmcli connection add type ethernet con-name "$NEW" ifname "$DEVICE"
nmcli connection modify "$NEW" ipv4.addresses "$ADDRESS" 
nmcli connection modify "$NEW" ipv4.method manual
nmcli connection down "$NEW"; nmcli connection up "$NEW"

exit 0

Obviously, there many more things you can do with nmcli but this port should be sufficient for you to be able to quickly configure a static network interface in RHEL7 or CentOS7.

Enjoy!

Comments are closed.