How to configure network using nmcli command in Linux CentOS/RHEL/Ubuntu

nmcli: NetworkManager Command Line Interface (nmcli) is a powerful command-line tool designed to manage and control network connections and devices in Linux-based systems. It serves as a versatile alternative to graphical network management tools and offers administrators and users direct access to NetworkManager's functionalities through the terminal.

With nmcli, users can efficiently interact with NetworkManager, making it a valuable tool for both experienced administrators and those comfortable working with command-line interfaces. This utility enables a wide range of tasks related to network configuration and monitoring.

{getToc} $title={Table of Contents}

The basic syntax of a nmcli command is as follows:

# nmcli [OPTIONS] OBJECT { COMMAND | help }

Viewing all connections

[root@TechArticles ~]# nmcli connection show
OR
[root@TechArticles ~]# nmcli con sh

To view only currently active connections

[root@TechArticles ~]# nmcli connection show --active
NAME    UUID                                  TYPE      DEVICE
enp0s3  b972dea7-b72d-4e48-a838-bda20615e189  ethernet  enp0s3
virbr0  58bed60d-b638-4e39-978d-5417bee137dc  bridge    virbr0

To view only devices recognized by NetworkManager and their state

[root@TechArticles ~]# nmcli device status

DEVICE  TYPE      STATE                   CONNECTION
enp0s3  ethernet  connected               enp0s3
virbr0  bridge    connected (externally)  virbr0
enp0s8  ethernet  disconnected            --
enp0s9  ethernet  disconnected            --
lo      loopback  unmanaged               --
[root@TechArticles ~]#

How to start and stop a Network Interface Using nmcli command.

We can use nmcli tool to start and stop any network interface, including controllers.

# nmcli con up id bond0
# nmcli con up id port0
# nmcli dev disconnect bond0
# nmcli dev disconnect enp0s3

Example:

[root@TechArticles ~]# nmcli con up enp0s3
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/5)
[root@TechArticles ~]#
[root@TechArticles ~]# nmcli dev disconnect enp0s3

Device 'enp0s3' successfully disconnected.

Configuring static IP using nmcli command.

Let's check the current active IP address of system.

[root@TechArticles ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 08:00:27:e5:4f:ea brd ff:ff:ff:ff:ff:ff
    inet 192.168.111.77/24 brd 192.168.111.255 scope global dynamic noprefixroute enp0s3
       valid_lft 3491sec preferred_lft 3491sec
    inet6 2409:4089:8214:b7e0:a00:27ff:fee5:4fea/64 scope global dynamic noprefixroute
       valid_lft 3548sec preferred_lft 3548sec
    inet6 fe80::a00:27ff:fee5:4fea/64 scope link noprefixroute
       valid_lft forever preferred_lft forever
3: enp0s8: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 08:00:27:c2:15:90 brd ff:ff:ff:ff:ff:ff
4: enp0s9: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 08:00:27:77:43:28 brd ff:ff:ff:ff:ff:ff
5: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP>mtu 1500 qdisc noqueue state DOWN group default qlen 1000
    link/ether 52:54:00:8b:26:aa brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
       valid_lft forever preferred_lft forever

As per above Interface details, I am going to configure IP on enp0s8 interface. In your case it may be different device name, configure accordingly.

For demonstration purpose I am taking below IPs. Please Note: In your case IP will be diffrent.

IP address:		 192.168.111.100/24
Default gateway:	 192.168.111.100
Preferred DNS:		  8.8.8.8
IP addressing 		  static 

Let's configre first IP address for device enp0s8.

[root@TechArticles ~]# nmcli con mod enps08 ipv4.addresses 192.168.111.100/24
Error: unknown connection 'enps08'.
[root@TechArticles ~]#

Oops its says unknown connection, but in below command output you can see that device is there but disconnected and there is no connection created till now.

[root@TechArticles ~]# nmcli device status
DEVICE  TYPE      STATE                   CONNECTION
enp0s3  ethernet  connected               enp0s3
virbr0  bridge    connected (externally)  virbr0
enp0s8  ethernet  disconnected            --
enp0s9  ethernet  disconnected            --
lo      loopback  unmanaged               --

You can check for connection by running below command.

[root@TechArticles ~]# nmcli con show
NAME    UUID                                  TYPE      DEVICE
enp0s3  b972dea7-b72d-4e48-a838-bda20615e189  ethernet  enp0s3
virbr0  58bed60d-b638-4e39-978d-5417bee137dc  bridge    virbr0

As per above command output there is no connection created, So we have to create the connection first for enp0s8.

[root@TechArticles ~]# nmcli connection add type ethernet connection.interface-name enp0s8 con-name enp0s8
Connection 'enp0s8' (14cb1621-2a4a-47f6-8092-3323e7cb4e65) successfully added.

[root@TechArticles ~]# nmcli con show
NAME             UUID                                  TYPE      DEVICE
enp0s3           b972dea7-b72d-4e48-a838-bda20615e189  ethernet  enp0s3
enp0s8  14cb1621-2a4a-47f6-8092-3323e7cb4e65  ethernet  enp0s8
virbr0           58bed60d-b638-4e39-978d-5417bee137dc  bridge    virbr0

As you can see that device enp0s8 is now available in connecion list. Let's move further to configure the IP.

[root@TechArticles ~]# nmcli connection modify enp0s8 ipv4.addresses 192.168.111.77/24

Assign the gateway.

[root@TechArticles ~]# nmcli connection modify enp0s8 ipv4.gateway 192.168.111.1

Assign the DNS server IP.

[root@TechArticles ~]# nmcli con mod enp0s8 ipv4.dns "8.8.8.8"

Then change the connection status from auto to manual and enable the interface at boot.

[root@TechArticles ~]# nmcli con mod enp0s8 ipv4.method manual
[root@TechArticles ~]# nmcli con mod enp0s8 autoconnect yes

To save the changes, run the command.

[root@TechArticles ~]# nmcli con up enp0s8

Then verify if the IP is configured or not to interface

[root@TechArticles ~]# ip addr show dev enp0s8
3: enp0s8: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 08:00:27:c2:15:90 brd ff:ff:ff:ff:ff:ff
    inet 192.168.111.100/24 brd 192.168.111.255 scope global dynamic noprefixroute enp0s8
       valid_lft 560sec preferred_lft 560sec
    inet6 fe80::6618:b371:6916:184b/64 scope link noprefixroute
       valid_lft forever preferred_lft forever

All above changed will be wrriten in /etc/sysconfig/network-scripts/ifcfg-enp0s8 file.

You can view the details of /etc/sysconfig/network-scripts/ifcfg-enp0s8 by running below command.

[root@TechArticles ~]# cat /etc/sysconfig/network-scripts/ifcfg-enp0s8
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=none
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=enp0s8
UUID=14cb1621-2a4a-47f6-8092-3323e7cb4e65
DEVICE=enp0s8
ONBOOT=yes
IPADDR=192.168.111.100
PREFIX=24
GATEWAY=192.168.111.1
DNS1=8.8.8.8

Was this article of use to you? Post your insightful thoughts or recommendations in the comments section if you don't find this article to be helpful or if you see any outdated information, a problem, or a typo to help this article better.

Jay

I love keeping up with the latest tech trends and emerging technologies like Linux, Azure, AWS, GCP, and other cutting-edge systems. With experience working with various technology tools and platforms, I enjoy sharing my knowledge through writing. I have a talent for simplifying complex technical concepts to make my articles accessible to all readers. Always looking for fresh ideas, I enjoy the challenge of presenting technical information in engaging ways. My ultimate aim is to help readers stay informed and empowered on their tech journeys.

Post a Comment

Previous Post Next Post

Contact Form