Connecting to WiFi from the Linux Command Line

In the world of Linux, many tasks can be accomplished directly from the command line, including connecting to WiFi networks. This can be particularly useful for headless systems or when you need to automate network connections. Here's a step-by-step guide to help you connect to WiFi using the command line in Linux.

Understanding the Basics
Before we dive into the commands, let's understand some basics. In Linux, network interfaces are usually named after the hardware (like wlan0 for wireless). The WiFi configuration files are typically located in the /etc/NetworkManager/system-connections/ directory. NetworkManager is a service that manages network connections and is usually enabled by default in most Linux distributions.

Identifying Your Network Interface
First, you need to identify your network interface. You can use the ip address command to list all network interfaces and their statuses. Here's how:

ip address
Look for an interface with UP status and BROADCAST,MULTICAST in the flags column. This is usually your wireless interface (like wlan0).
Connecting to a WiFi Network
Now that you've identified your network interface, you can connect to a WiFi network using the nmcli command. nmcli is a command-line tool for interacting with NetworkManager. Here's the basic syntax:

nmcli device wifi connect <SSID> password <PASSWORD>
Replace <SSID> with your WiFi network's name (SSID) and <PASSWORD> with your WiFi password. For example:
nmcli device wifi connect MyNetwork password MyPassword
If your network is secured with WPA2 Enterprise, you might need to use the --wifi-secure flag and provide additional details. For more information, see the Arch Linux NetworkManager guide.
Connecting to a Hidden WiFi Network

If your WiFi network is hidden (the SSID is not broadcasted), you can still connect to it using nmcli. You'll need to scan for available networks first, then connect to the desired network using its BSSID (MAC address). Here's how:
- Scan for available networks:




















nmcli device wifi rescan
nmcli device wifi list
- Identify the BSSID of your hidden network.
- Connect to the network using the BSSID:
nmcli device wifi connect <BSSID> password <PASSWORD>
Automating WiFi Connections
If you want to automate WiFi connections, you can create a script that runs the nmcli command whenever you connect to a new network. Here's a simple bash script that connects to a WiFi network when run:
#!/bin/bash
SSID="MyNetwork"
PASSWORD="MyPassword"
nmcli device wifi connect "$SSID" password "$PASSWORD"
Save this script as a .sh file (e.g., connect_to_wifi.sh), make it executable with chmod +x connect_to_wifi.sh, and run it whenever you need to connect to your WiFi network.
Troubleshooting
If you're having trouble connecting to your WiFi network, here are a few things you can try:
- Check if your network interface is up and running:
ip link show <INTERFACE> - Check if your network is visible:
nmcli device wifi list - Check if your password is correct. If you're sure it is, try resetting your router's password.
- If you're still having trouble, check the
/var/log/syslogor/var/log/messageslogs for any error messages related to NetworkManager or your network interface.
Remember, this guide is for general use. Some distributions or specific setups might require additional steps or different commands. Always consult your distribution's documentation for the most accurate information.