Skip to Content

how to configure network with nmcli command in Linux

nmcli is a command-line client for NetworkManager, which is a system network service that manages network devices and connections, primarily for Linux-based operating systems.

nmcli provides a way to display network status, manage network connections, and control the NetworkManager daemon, all from the command line.

Here’s a brief overview of nmcli and how to use it:

General Syntax

nmcli [OPTIONS] OBJECT { COMMAND | help }

  • OPTIONS: Global options for nmcli (e.g., -p for pretty output).
  • OBJECT: The type of object you want to manage (e.g., connection, device).
  • COMMAND: The action you want to perform on the object.

 

A “device” refers to a network interface on your system. This can be an Ethernet interface, a Wi-Fi interface, a Bluetooth interface, a VPN virtual interface, and more.

The term “connection” refers to a set of configuration settings that describe how to connect to a network. Essentially, a connection is a named profile that contains all the details needed to connect to a specific network.

Each connection has:

  • A unique name.
  • A type (e.g., Wi-Fi, Ethernet, VPN, bridge, etc.).
  • Various settings appropriate for that type. For instance, a Wi-Fi connection would have SSID, security type, and password, while an Ethernet connection might have details about IP configuration (static or DHCP).

 

Common Usages

Display General Status:

nmcli general status

List All Network Devices:

nmcli device

Show Details of a Specific Device:
Replace devicename with the name of your device (e.g., eth0, wlan0).

nmcli device show devicename

List All Network Connections:

nmcli connection show

Activate/Deactivate a Connection:
Replace connectionname with the name of the connection you want to activate or deactivate.

nmcli connection up connectionname
nmcli connection down connectionname

Add a New Connection:
For example, to add a new Wi-Fi connection:

nmcli connection add con-name "MyWiFi" type wifi ifname wlan0 ssid "SSID_NAME"

Delete a Connection:
Replace connectionname with the name of the connection you want to delete.

nmcli connection delete connectionname

Modify a Connection:
For example, to change the IPv4 method of a connection to manual and set an IP address:

nmcli connection modify connectionname ipv4.method manual ipv4.addresses "192.168.1.10/24"

3. Tips:

Help Command: If you’re unsure about how to use a specific command, you can always append help to get more information. For example:

nmcli device help

Remember, while nmcli is a powerful tool, making changes to your network settings can disrupt your connection. Always double-check commands, especially when modifying or deleting connections.

configure network in Linux with nmcli command

Configuring a network using the nmcli command in Linux involves a series of steps. Below is a basic guide on how to do this for a wired (ethernet) and wireless (Wi-Fi) connection.

 Wired (Ethernet) Connection:
a. List Available Devices:

nmcli device

b. Add a New Connection:
Replace eth0 with your device name, and my-ethernet with your desired connection name.

nmcli connection add type ethernet con-name my-ethernet ifname eth0

c. Set Static IP (Optional):

If you want to set a static IP, netmask, and gateway:

nmcli connection modify my-ethernet ipv4.addresses "192.168.1.10/24" ipv4.gateway "192.168.1.1" ipv4.method manual

d. Set DNS (Optional):

nmcli connection modify my-ethernet ipv4.dns "8.8.8.8,8.8.4.4"

e. Activate the Connection:

nmcli connection up my-ethernet

create bonding with nmcli command

Bonding is a method of aggregating multiple network interfaces into a single logical “bonded” interface, typically to provide redundancy or increased throughput. Using `nmcli`, you can easily set up bonding in Linux.

Here’s a step-by-step guide on how to create a bond using nmcli:

1. Install Necessary Packages:
First, ensure that the NetworkManager package is installed and running.

sudo apt install network-manager

2. Create the Bond:
Use the following command to create a bond. In this example, the bond is named bond0 and uses the mode 802.3ad (LACP).

nmcli con add type bond con-name bond0 ifname bond0 mode 802.3ad primary eth0

3. Add Slave Interfaces to the Bond:
Now, add the required Ethernet interfaces to the bond. In this example, eth0 and eth1 are added as slaves to bond0.

nmcli con add type ethernet con-name bond0-slave1 ifname eth0 master bond0
nmcli con add type ethernet con-name bond0-slave2 ifname eth1 master bond0

4. Configure IP Details for the Bond:
You can assign an IP address to the bonded interface like so:

nmcli con mod bond0 ipv4.addresses "192.168.1.10/24" ipv4.gateway "192.168.1.1" ipv4.method manual

5. Activate the Bond:
Bring up the bond and its slave interfaces:

nmcli con up bond0
nmcli con up bond0-slave1
nmcli con up bond0-slave2

6. Verify the Bond Configuration:
To check the status of the bond and its slaves:

nmcli con show
nmcli -p con show bond0
cat /proc/net/bonding/bond0

7. Optional Settings:
There are several bonding options that you can configure depending on your needs, such as setting the bonding mode (balance-rr, active-backup, 802.3ad, etc.), configuring ARP monitoring, and setting the bonding interval. For example, to change the bonding mode:

nmcli con mod bond0 bond.options "mode=active-backup"

Note: Always refer to the official documentation or the nmcli man page for a comprehensive list of options and capabilities. Also, make sure that your switch or router supports the bonding mode you’re using (especially for modes like 802.3ad).

Daniel Lim

Monday 6th of November 2023

I am always confused with networkmanager and netplan. Is there any link I can read to get this clear?

Brook

Saturday 4th of November 2023

Looks like this command can be used on Ubuntu system. I will try it.

Guise

Saturday 4th of November 2023

Agree! It's particularly handy when working on servers where GUI is not an option

Henry Sae

Saturday 4th of November 2023

I find it fascinating how nmcli simplifies complex network configurations into single-line commands.

Eric W

Saturday 4th of November 2023

Great job on covering a topic that is essential for network management and troubleshooting!