Ubuntu 24.04, like many modern Linux distributions, relies on the NetworkManager for managing network connections. While the graphical tools available in Ubuntu make network management accessible to users of all skill levels, nmcli – the command-line interface for NetworkManager – provides a powerful and flexible alternative. Whether you’re managing servers, automating tasks, or just prefer the command line, nmcli is an indispensable tool.

This guide will cover the basics of using nmcli to manage network interfaces and settings on Ubuntu 24.04. We’ll explore how to view, configure, and troubleshoot network connections using this powerful command.

Prerequisites

  • Ubuntu 24.04 installed on your system.
  • Basic knowledge of Linux command-line operations.
  • Sudo privileges for managing network settings.

Understanding the Basics of nmcli

nmcli is a command-line client for NetworkManager. It allows you to perform all network management tasks that the graphical interface can, and often much more efficiently. The basic syntax of nmcli is:

nmcli [OPTIONS] OBJECT { COMMAND | help }
  • OBJECT refers to the network component you want to manage, such as device, connection, or general.
  • COMMAND is the action you want to perform, such as show, up, down, add, etc.

Commonly Used Objects in nmcli

  • general: Provides overall status and information about NetworkManager.
  • device: Manages network interfaces.
  • connection: Manages network connections (Wi-Fi, Ethernet, VPN, etc.).

Viewing Network Status

To begin, it’s useful to see the current state of your network devices and connections.

Display Overall Network Status

To get a general overview of the network status, including connectivity status and overall configuration:

nmcli general status

List All Network Devices

To see a list of all network interfaces (e.g., Ethernet, Wi-Fi, etc.) on your system:

nmcli device status

This command displays the device name, type, state, and the associated connection if any.

Show Detailed Information for a Specific Device

If you need more detailed information about a specific network interface (e.g., eth0 or wlp2s0), use:

nmcli device show eth0

Replace eth0 with the actual device name you want to inspect.

Managing Network Interfaces

Bringing a Network Interface Up or Down

To activate (bring up) a network interface, use:

nmcli device connect eth0

To deactivate (bring down) a network interface, use:

nmcli device disconnect eth0

Replace eth0 with the appropriate device name.

Managing Network Connections

Connections in NetworkManager are saved configurations for network interfaces. These can be wired, wireless, VPN, etc.

Listing Network Connections

To list all saved network connections:

nmcli connection show

This will list all connections along with their UUIDs, types, and device associations.

Adding a New Ethernet Connection

If you need to add a new Ethernet connection:

nmcli connection add type ethernet ifname eth0 con-name "Wired Connection"
  • type: The type of connection (ethernet in this case).
  • ifname: The interface name to use.
  • con-name: A name for the connection.

Connecting to a Wi-Fi Network

To connect to a Wi-Fi network, use:

nmcli device wifi list

This command will list all available Wi-Fi networks. Then, connect to a specific network with:

nmcli device wifi connect "SSID_name" password "your_password"

Replace "SSID_name" with the network’s SSID and "your_password" with the Wi-Fi password.

Deleting a Network Connection

If you want to remove a saved network connection:

nmcli connection delete "Wired Connection"

Replace "Wired Connection" with the name or UUID of the connection you wish to delete.

Advanced nmcli Usage

Editing a Connection

If you need to modify an existing connection, you can use the edit command:

nmcli connection edit "Wired Connection"

This command opens an interactive session where you can modify connection parameters.

Setting Static IP Addresses

To configure a static IP address for an Ethernet connection:

nmcli connection modify "Wired Connection" ipv4.addresses "192.168.1.100/24"
nmcli connection modify "Wired Connection" ipv4.gateway "192.168.1.1"
nmcli connection modify "Wired Connection" ipv4.dns "8.8.8.8 8.8.4.4"
nmcli connection modify "Wired Connection" ipv4.method manual

Finally, bring the connection up with:

nmcli connection up "Wired Connection"

Creating a Wi-Fi Hotspot

To create a Wi-Fi hotspot on your system:

nmcli device wifi hotspot ifname wlp2s0 ssid MyHotspot password "mypassword"

This command sets up a Wi-Fi hotspot with the given SSID and password. Replace wlp2s0 with your Wi-Fi interface name.

Troubleshooting with nmcli

Checking Connection Logs

To view detailed logs and troubleshoot network issues, use:

nmcli connection show --active

Then, for more detailed logs, you can use the journalctl command, which integrates with NetworkManager:

journalctl -u NetworkManager -f

Resetting Network Settings

If you’re encountering persistent issues, resetting the NetworkManager configuration can help:

sudo systemctl restart NetworkManager

Or, reload the configuration without restarting:

nmcli networking off
nmcli networking on

Conclusion

nmcli is a versatile and powerful tool for managing network interfaces and settings on Ubuntu 24.04. Whether you’re configuring a server, setting up a Wi-Fi connection, or troubleshooting network issues, mastering nmcli will give you greater control and efficiency in managing your network. With the commands and tips provided in this guide, you should be well-equipped to handle a variety of networking tasks directly from the command line.