Linux nmcli Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, we will explore the powerful nmcli command-line tool for managing network connections in Linux. nmcli is part of the NetworkManager service and provides a wide range of functionalities, including configuring network interfaces, connecting to and disconnecting from networks, and troubleshooting network issues. We will start by introducing the nmcli command and checking its version, then move on to configuring network interfaces using nmcli commands. Finally, we will learn how to use nmcli for troubleshooting network problems.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/RemoteAccessandNetworkingGroup(["`Remote Access and Networking`"]) linux(("`Linux`")) -.-> linux/SystemInformationandMonitoringGroup(["`System Information and Monitoring`"]) linux/RemoteAccessandNetworkingGroup -.-> linux/ifconfig("`Network Configuring`") linux/RemoteAccessandNetworkingGroup -.-> linux/ip("`IP Managing`") linux/SystemInformationandMonitoringGroup -.-> linux/service("`Service Managing`") subgraph Lab Skills linux/ifconfig -.-> lab-422843{{"`Linux nmcli Command with Practical Examples`"}} linux/ip -.-> lab-422843{{"`Linux nmcli Command with Practical Examples`"}} linux/service -.-> lab-422843{{"`Linux nmcli Command with Practical Examples`"}} end

Introduction to nmcli Command

In this step, we will introduce the nmcli command, which is a powerful command-line tool for managing network connections in Linux. nmcli stands for "NetworkManager Command Line Interface" and is part of the NetworkManager service, which is responsible for managing network connections in modern Linux distributions.

First, let's check the version of nmcli installed on our system:

nmcli --version

Example output:

nmcli tool, version 1.36.0

The nmcli command provides a wide range of functionalities for managing network interfaces, connections, and network-related settings. Some of the key features of nmcli include:

  • Viewing and managing network connections
  • Configuring network interfaces
  • Connecting to and disconnecting from networks
  • Monitoring network status and troubleshooting network issues
  • Interacting with the NetworkManager service

In the following steps, we will explore how to use nmcli to perform various network management tasks.

Configuring Network Interfaces with nmcli

In this step, we will learn how to use the nmcli command to configure network interfaces on our Linux system.

First, let's list all the available network interfaces on our system:

nmcli device status

Example output:

DEVICE  TYPE      STATE      CONNECTION
eth0    ethernet  connected  eth0
lo      loopback  unmanaged  --

As you can see, we have an Ethernet interface eth0 that is currently connected, and a loopback interface lo that is unmanaged.

Now, let's configure a new Ethernet connection using nmcli:

sudo nmcli connection add type ethernet con-name my-ethernet ifname eth0 ip4 192.168.1.100/24 gw4 192.168.1.1

This command creates a new Ethernet connection named my-ethernet and associates it with the eth0 interface. We also set the IPv4 address to 192.168.1.100/24 and the gateway to 192.168.1.1.

To activate the new connection, run:

sudo nmcli connection up my-ethernet

You should see the connection status change to "connected".

To verify the configuration, you can run:

nmcli device status
nmcli connection show my-ethernet

The output should show the eth0 interface as "connected" and the details of the my-ethernet connection.

Troubleshooting Network Issues with nmcli

In this step, we will learn how to use the nmcli command to troubleshoot network issues on our Linux system.

Let's start by simulating a network issue by disconnecting the my-ethernet connection we created in the previous step:

sudo nmcli connection down my-ethernet

Now, let's check the network status:

nmcli device status

You should see that the eth0 interface is now in the "disconnected" state.

To troubleshoot the issue, we can use the nmcli command to get more detailed information about the connection:

nmcli connection show my-ethernet

This will display the configuration details of the my-ethernet connection, including any errors or issues that may be preventing the connection from being established.

If you want to see the logs related to the network connection, you can use the nmcli command to monitor the NetworkManager service:

sudo nmcli monitor

This will start monitoring the NetworkManager service and display any relevant log messages in real-time. You can press Ctrl+C to stop the monitoring.

Finally, to reconnect the my-ethernet connection, you can use the following command:

sudo nmcli connection up my-ethernet

This should bring the eth0 interface back to the "connected" state.

Summary

In this lab, we were introduced to the powerful nmcli command, which is a command-line tool for managing network connections in Linux. We learned how to check the version of nmcli installed on our system and explored the key features of this tool, including viewing and managing network connections, configuring network interfaces, connecting to and disconnecting from networks, and troubleshooting network issues.

We then delved into the process of configuring network interfaces using nmcli. We learned how to list all available network interfaces, create a new Ethernet connection, and activate the connection. These skills are essential for effectively managing network settings in a Linux environment.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like