Featured image of post Configuring Wired and Wireless Wi-Fi Networks with nmcli

Configuring Wired and Wireless Wi-Fi Networks with nmcli

Configuring network connections is crucial for the proper functioning of Linux systems. nmcli (Network Manager Command Line Interface) is a powerful command-line tool that simplifies network management for users...

In Linux systems, configuring network connections is essential for the system to function properly. nmcli (Network Manager Command Line Interface) is a powerful command-line tool that allows users to easily manage network connections. This article will guide you on how to use nmcli to configure network connections, including creating new connections, editing existing ones, and connecting to Wi-Fi networks.

Configuring Wired and Wireless Wi-Fi Networks with nmcli

  1. Installing and Verifying nmcli

Most modern Linux distributions come with NetworkManager and nmcli pre-installed. If it’s not installed, you can use a package manager to install it. For example, on a Debian-based system, you can run the following command:

1
2
sudo apt update  
sudo apt install network-manager

After installation, you can verify that nmcli has been successfully installed by running nmcli --version.

  1. Displaying Network Status

First, you can check the status of the network using nmcli. Run the following command:

1
nmcli device status

This will display the status of all network devices, including Ethernet and Wi-Fi interfaces.

  1. Listing Network Connections

To view the currently configured network connections, you can use the following command:

1
nmcli connection show

This will list all configured network connections, including Ethernet and Wi-Fi connections.

  1. Adding an Ethernet Connection

Suppose you want to add an Ethernet connection; you can use the following command:

1
sudo nmcli connection add type ethernet ifname eth0 con-name MyEthernet ipv4.addresses 192.168.1.100/24 ipv4.gateway 192.168.1.1 ipv4.dns 8.8.8.8 ipv4.method manual

Here is a breakdown of the parameters:

  • type ethernet: Specifies the connection type as Ethernet.
  • ifname eth0: Specifies the interface name as eth0.
  • con-name MyEthernet: Assigns a name to the connection, in this case, MyEthernet.
  • ipv4.addresses 192.168.1.100/24: Sets the IPv4 address and subnet mask.
  • ipv4.gateway 192.168.1.1: Sets the IPv4 gateway.
  • ipv4.dns 8.8.8.8: Sets the IPv4 DNS server.
  • ipv4.method manual: Specifies the IPv4 configuration method as manual.
  1. Adding a Wi-Fi Connection

1. Open Terminal

First, you need to open the terminal. You can use the shortcut Ctrl+Alt+T, or find and open the terminal from the application menu.

2. Scan for Available Wi-Fi Networks

Before connecting to a Wi-Fi network, we need to know what networks are available. By using the nmcli device wifi command, we can rescan and list all available Wi-Fi networks.

1
2
nmcli device wifi rescan    # Rescan for available Wi-Fi networks
nmcli device wifi list      # List the scanned Wi-Fi networks

After running these commands, you will see a list of all available Wi-Fi networks, including their SSIDs, encryption methods, and signal strengths.

3. Choose and Connect to a Wi-Fi Network

From the scan results, select the Wi-Fi network you want to connect to, and use the nmcli device wifi connect command. You will need to provide the Wi-Fi’s SSID and password.

1
nmcli device wifi connect "YourSSID" password "YourPassword"

Replace YourSSID with the name of the Wi-Fi network you wish to connect to, and YourPassword with the password for that network. If the connection is successful, you will see a message indicating that the device has been successfully activated.

4. Set Up Automatic Connection on Boot

If you want the system to automatically connect to this Wi-Fi network on startup, you can use the nmcli connection modify command to set this up. However, you first need to find out the UUID (Universally Unique Identifier) of the connection. You can view all connections and their UUIDs using the command:

1
nmcli connection show

Find the UUID of the Wi-Fi connection you just created, then set it to connect automatically on boot with the following command:

1
nmcli connection modify "YourUUID" connection.autoconnect yes

Replace YourUUID with the UUID of your Wi-Fi connection.

  1. Modifying Connection Settings

To modify the settings of an existing connection, you can use the modify command. For example, to change the DNS server for the Ethernet connection you created earlier, run:

1
sudo nmcli connection modify MyEthernet ipv4.dns 8.8.4.4

This will change the DNS server for the MyEthernet connection to 8.8.4.4.

  1. Enabling/Disabling Connections

To enable or disable a connection, you can use the up and down commands. For example, to enable the MyEthernet connection, run:

1
sudo nmcli connection up MyEthernet

To disable it, you can run:

1
sudo nmcli connection down MyEthernet
  1. Deleting Connections

If you no longer need a connection, you can delete it using the delete command. For example:

1
sudo nmcli connection delete MyEthernet

This will remove the connection named MyEthernet.

  1. Exporting and Importing Connections

NetworkManager also supports exporting and importing connection settings, which is useful for backing up or migrating network settings.

  • To export connection settings:
1
nmcli connection export MyEthernet > myethernet.nmconnection
  • To import connection settings:
1
sudo nmcli connection import myethernet.nmconnection

With this guide, you should now have a foundational understanding of how to use nmcli to configure and manage network connections. Whether you’re adding, modifying, or deleting network connections, nmcli provides a powerful command-line interface to accomplish these tasks.

Licensed under CC BY-NC-SA 4.0