Beginner’s Guide: Using nmcli for Linux Networking
The “Network Manager Command Line Interface” popularly known as “nmcli” is one of the most popular tools for managing Networking in Linux systems. It can create, delete, activate, deactivate, edit and display network connections. This post is a comprehensive guide on how to use nmcli like a pro.
nmcli is a command line tool for managing the NetworkManager and reporting network status. It’s the command line equivalent of nmtui.
I have seen many administrators use nmtui to manage their networks because of its simplicity. However, nmcli allows you to access advanced functionalities of NetworkManager that you would not find in nmtui. That’s what I want to show you in this article.
If you need help with Linux, I’ve got something that can help you right away!
Download my free Linux commands cheat sheet – it’s a quick reference guide with all the essential commands you’ll need to get things done on your system. Click here to get it for free!
NetworkManager: Brief Overview
Up to this point, you must have noticed that nmcli isn’t a standalone utility. It relies on NetworkManager. So, what is NetworkManager?
NetworkManager is a system service that simplifies and automates network configuration and management on Linux systems. It handles various connections, including Ethernet, Wi-Fi, mobile broadband, and VPNs, ensuring devices maintain reliable connectivity.
NetworkManager can be managed using graphical tools, the text-based nmtui, and the nmcli command line tool, making it easy to use whether you are working in a desktop or server environment.
Typical uses of nmcli include:
- Scripts: Unlike many network management tools, you can use nmcli in scripts to automate network configuration and management tasks. Additionally, nmcli supports a concise output format that is easier for scripts to read and process.
- Servers, headless machines and terminals: These systems don’t come with a graphical interface, therefore, you cannot set up networking graphically. ‘nmcli’ provides a quick way to configure and monitor network settings directly from the command line.
Now let’s see the options available in more detail, and how to use them.
Command Syntax
nmcli uses a specific syntax to allow you to interact with NetworkManager. Below is the general syntax for nmcli. It might look somehow complex, but trust me, when we start looking at examples you will notice how easy it is.
nmcli [OPTIONS] OBJECT { COMMAND | help }
Let’s look at each parameter in the above command.
- nmcli: This is the command-line utility that we want to use.
- [OPTIONS]: These are optional parameters/ flags that modify the behavior of the nmcli command.
- OBJECT: This specifies the type of network object you want to interact with (e.g., device, connection, etc.).
- COMMAND: This parameter specifies the action you want to perform on the object.
If you’re new to the Linux command line, this article will give you the most important Linux commands to know, plus a free downloadable cheat sheet to keep handy.
Download your exclusive free PDF containing the most useful Linux commands to elevate your skills!
Download now
Usage Examples
Let’s look at some real-world examples of the nmcli command in Linux.
View Connections
nmcli connection show
This command displays a list of all network connections managed by NetworkManager. It provides information about each connection, including its name, UUID, type (Ethernet, Wi-Fi, etc.), and current status (whether it’s active or not).
The screenshot above shows my computer is currently connected to a Wi-Fi network called “PHINIX.”
Check the Device Status
nmcli dev status
This command displays the status of all network devices managed by NetworkManager. It provides information such as device name, type (e.g., Ethernet, Wi-Fi), state (connected or disconnected) and the current connection (e.g., Wi-Fi name).
Display Device Details
You can also use nmcli to get detailed information about a specific network device managed by NetworkManager, here’s the syntax:nmcli device show <device-name>
So, for example:nmcli device show wlp1s0
To get the name of the network device, use the “nmcli dev status” discussed in the previous section. In my case, the network device is “wlp1s0.”
The ‘nmcli device show <device_name>’ command provides information like:
- Device type (e.g., Ethernet, Wi-Fi)
- Hardware address (MAC address)
- Current operational state (connected or disconnected)
- Assigned IP addresses (IPv4 and IPv6)
- Gateway and DNS server addresses
- Routing information (routes to different networks)
- Additional device-specific settings and parameters
Add a new network connection
You can add a new connection in nmcli using the command below:
nmcli connection add type <connection_type> ifname <interface> con-name <connection_name>
Here is a detailed breakdown:
- <connection_type>: Specifies the type of connection to be created (e.g., ethernet for Ethernet connections, wifi for Wi-Fi connections).
- <interface>: Specifies the network interface name to associate with the connection (e.g., eth0 for Ethernet interfaces, wlan0 for Wi-Fi interfaces).
- <connection_name>: Sets the name for the new connection, which will be used to identify it within NetworkManager. Keep it something human-readable and related to the connection you are creating (e.g., my_ethernet_connection).
Below is the command I used to create an Ethernet connection:
nmcli connection add type ethernet ifname eno1 con-name my_ethernet_conn
Tip: If you get an error like “Error: Failed to add ‘my_ethernet_conn’ connection: Insufficient privileges,” you have to use “sudo.”
Now, when I run the “nmcli connection show” command to view connections, I should see my new ethernet connection listed:
nmcli connection show
Modify Connection Settings
To modify an existing connection, use the “nmcli connection modify command.” Let’s use the new ethernet connection I created in the previous section as an example.
Download your exclusive free PDF containing the most useful Linux commands to elevate your skills!
Download now
Have a look at the command below which sets static IP addressing to the ethernet connection:
nmcli connection modify my_ethernet_conn ipv4.addresses '192.168.1.100/24' ipv4.gateway '192.168.1.1' ipv4.dns '8.8.8.8,8.8.4.4' ipv4.method manual
In this example:
- my_ethernet_conn is the name of your existing Ethernet connection.
- ipv4.addresses, ipv4.gateway, ipv4.dns, and ipv4.method are parameters you can modify according to your specific network configuration needs.
By using nmcli connection modify, you can adjust various parameters of your existing network connection to suit your networking requirements.
Main Options
When using nmcli, you can apply various main options to modify its behaviour. Here are some of the main options available:
- -t, –terse: Provides output in a terse format, suitable for scripting or easier parsing.
- -f, –fields fields: Specifies specific fields to display in the output. Useful for customizing the information shown.
- -e, –escape: Escapes special characters in fields like passwords or SSIDs, ensuring they are correctly interpreted.
- –s, –show-secrets: Displays passwords and other sensitive information in the output. Use with caution for security reasons.
- -g, –get-values: Retrieves values of properties as key-value pairs, which can be handy for scripting and automation.
- -p, –pretty: Formats the output in a more human-readable format, enhancing readability at the cost of simplicity for parsing.
- -m, –mode mode: Sets the operation mode, such as interactive mode (interactive) or non-interactive mode (batch).
These options provide flexibility when using nmcli to manage and configure network settings on Linux systems, catering to different use cases from interactive sessions to automated scripts.
Tips
Here are a few advanced tips for using nmcli:
Setup Wi-Fi hotspot with nmcli
Did you know you can create a Wi-Fi hotspot on your Linux system? nmcli enables you to do that with a few commands.
Check out this article for a step-by-step guide.
Starting and Stopping a Network Interface
To start and stop a network interface using nmcli, you can use the following commands:nmcli device connect <device>
nmcli device disconnect <device>
First, you will need to know the name of your network device. You can do that using the command below:nmcli dev status
To start a network interface execute the “connect” command like this:nmcli device connect eth0
Use the command below to stop a network interface, for example:nmcli device disconnect eth0
Alternatives
Below are several nmcli alternatives that you can use to manage your network.
- nmtui: A text-based interface for NetworkManager that provides a simple, menu-driven method to manage network connections directly from the terminal.
- GNOME NetworkManager (nm-connection-editor): This is a graphical network connection manager provided by the GNOME desktop environment. It allows users to manage and configure network connections using a graphical interface, offering visual representations of network settings and connections.
- KDE Plasma NetworkManager (plasma-nm): This is the network management applet and settings module used in the KDE Plasma desktop environment.
Related questions
How to set a VPN connection in nmcli?
Setting up a VPN connection with nmcli proved to be quite a technical task. In my case, I was trying to set up OpenVPN, and it took me a while, involving a lot of research in various Linux forums. Below are the steps I used:
- Open this GitHub repository to download the script you will use. Alternatively, you can select all the code and paste it into a text file. Remember to save the text file with a “.sh” extension.
- Modify the script with your specific VPN details.
- Create a password file with your VPN credentials.
- Run the script to set up and manage your VPN connection.
Download your exclusive free PDF containing the most useful Linux commands to elevate your skills!
Download now
Going Further
If you want to learn more about nmcli, check out this official documentation.
Download your exclusive free PDF containing the most useful Linux commands to elevate your skills!
Download now
If you are looking for exclusive tutorials, I post a new course each month, available for premium members only. Join the community to get access to all of them right now!
Additional Resources
Overwhelmed with Linux commands?
My e-book, “Master Linux Commands”, is your essential guide to mastering the terminal. Get practical tips, real-world examples, and a bonus cheat sheet to keep by your side.
Grab your copy now.
VIP Community
If you just want to hang out with me and other Linux fans, you can also join the community. I share exclusive tutorials and behind-the-scenes content there. Premium members can also visit the website without ads.
More details here.
Need help building something with Python?
Python is a great language to get started with programming on any Linux computer.
Learn the essentials, step-by-step, without losing time understanding useless concepts.
Get the e-book now.