If you’re using Linux and need to connect to Wi-Fi from the terminal, nmcli is one of the most powerful tools you can use. nmcli is a command-line client for NetworkManager, allowing users to manage network connections, check the status of devices, and even configure Wi-Fi connections without the need for a graphical interface.
In this blog post, we’ll walk you through the steps to connect to Wi-Fi from the Linux terminal using nmcli. We’ll explain everything in simple terms, so even if you’re new to Linux, you’ll be able to follow along easily.
Why Use nmcli to Connect to Wi-Fi?
There are several reasons why you might want to use nmcli instead of a graphical tool:
- Remote Access: If you’re working on a headless server or remote machine without a GUI, nmcli is essential.
- Quick Setup: Connecting to Wi-Fi via the terminal can be faster and more efficient in certain situations.
- Automation: nmcli can be integrated into scripts for automated network configuration.
Step-by-Step Guide to Connect Wi-Fi from Linux Terminal Using nmcli
Step 1: Verify Network Interface
Before you connect to a Wi-Fi network, it’s important to verify that your wireless interface is detected and active.
- Open your terminal.
- Run the following command to check for available network interfaces:
nmcli device status
The output will look something like this:
DEVICE TYPE STATE CONNECTION
wlp2s0 wifi disconnected --
enp3s0 ethernet unavailable --
lo loopback unmanaged --
In this example, wlp2s0 is the wireless interface. We’ll use this interface to connect to Wi-Fi in the next steps.
Step 2: List Available Wi-Fi Networks
To view all the Wi-Fi networks available to your device, use the following command:
nmcli device wifi list
You’ll see a list of available Wi-Fi networks, similar to this:
IN-USE SSID MODE CHAN RATE SIGNAL BARS SECURITY
MyWiFi Infra 1 54 Mbit/s 80 ▂▄▆_ WPA2
OpenNetwork Infra 6 54 Mbit/s 65 ▂▄▆_ --
OtherWiFi Infra 11 130 Mbit/s 55 ▂▄__ WPA2
Take note of the SSID of the Wi-Fi network you want to connect to. In this case, we’ll connect to MyWiFi.
Step 3: Connect to Wi-Fi Network
To connect to the Wi-Fi network, run the following command, replacing MyWiFi with the SSID of your network and entering the correct password:
nmcli device wifi connect "MyWiFi" password "your_wifi_password"
If the connection is successful, you’ll see output like this:
Device 'wlp2s0' successfully activated with 'UUID-1234-5678-ABCD-9876'.
You are now connected to the Wi-Fi network.
Step 4: Check Connection Status
After connecting, you can verify your connection status with:
nmcli connection show
This will display a list of all active network connections:
NAME UUID TYPE DEVICE
MyWiFi UUID-1234-5678-ABCD-9876 wifi wlp2s0
Alternatively, to view detailed network information (such as your IP address), use:
nmcli device show wlp2s0
Step 5: Disconnect from Wi-Fi
If you need to disconnect from the Wi-Fi network, you can do so with the following command:
nmcli device disconnect wlp2s0
You’ll see the following output if the disconnect is successful:
Device 'wlp2s0' successfully disconnected.
Example: Automating Wi-Fi Connection
If you need to connect to a specific Wi-Fi network automatically when your machine starts, you can create a connection profile with nmcli.
To create a Wi-Fi connection profile:
nmcli connection add type wifi con-name MyWiFi ifname wlp2s0 ssid MyWiFi
This command creates a connection profile named MyWiFi for the network interface wlp2s0 with the SSID MyWiFi. Now, whenever the system starts, NetworkManager will automatically connect to this network if it’s available.
To connect automatically with a saved password:
nmcli connection modify MyWiFi wifi-sec.key-mgmt wpa-psk wifi-sec.psk "your_wifi_password"
You can also start the connection manually using the profile:
nmcli connection up MyWiFi
Common nmcli Commands for Wi-Fi Management
Here are some additional nmcli commands you might find useful for managing Wi-Fi connections:
- View all saved connections:
nmcli connection show
- Delete a saved Wi-Fi connection:
nmcli connection delete MyWiFi
- View detailed Wi-Fi device information:
nmcli device show wlp2s0
- Restart NetworkManager (if you encounter issues):
sudo systemctl restart NetworkManager
Connecting to Wi-Fi from the Linux terminal using nmcli is a powerful and efficient way to manage your network connections, especially if you’re working on a headless server or prefer terminal-based tools. With nmcli, you can easily scan for available networks, connect to Wi-Fi, and manage your network profiles all from the command line.
By following the steps in this guide, you should now be able to connect to Wi-Fi networks using nmcli on Linux, whether you’re using Ubuntu or any other Linux distribution that supports NetworkManager.