How to Connect to WiFi from a Root Shell in Ubuntu

5 min read
bash

How to Connect to WiFi from a Root Shell in Ubuntu

Introduction

Sometimes, you may find yourself needing to connect to a WiFi network from a root shell, especially when troubleshooting or working in a minimal environment without a graphical interface. This guide provides step-by-step instructions on how to connect to WiFi from a root shell using the nmcli and wpa_supplicant commands in Ubuntu.

Step 1: Identify Available WiFi Networks

Before connecting to a WiFi network, you need to identify the available networks:

    • Use nmcli to list available WiFi networks:

nmcli dev wifi list
   

This command lists all available WiFi networks, showing details such as SSID (network name), signal strength, and security type.

    • Note the SSID of the network you want to connect to.

Step 2: Connect to the WiFi Network Using nmcli

nmcli is a command-line tool for managing NetworkManager, which controls network connections in Ubuntu.

    • Connect to the WiFi network:

nmcli dev wifi connect "SSID" password "your_password"
   

Replace "SSID" with the name of your WiFi network and "your_password" with the network’s password.

    • Verify the connection:

After running the command, you should see a message indicating that the device has successfully connected to the network. To verify:

nmcli dev status
   

This command shows the status of your network interfaces, confirming if the WiFi connection is active.

Step 3: Manually Connecting Using wpa_supplicant

If nmcli is not available, or you prefer a more manual approach, you can use wpa_supplicant to connect to WiFi.

    • Create a configuration file for wpa_supplicant:

sudo nano /etc/wpa_supplicant/wpa_supplicant.conf
   

Add the following configuration, replacing your_ssid and your_password with your network’s SSID and password:

network={
       ssid="your_ssid"
       psk="your_password"
   }
   

    • Start wpa_supplicant:

Use the following command to connect to the WiFi network:

sudo wpa_supplicant -B -i wlan0 -c /etc/wpa_supplicant/wpa_supplicant.conf
   

Replace wlan0 with the name of your wireless interface, which can be found using the ip link command.

    • Obtain an IP address:

Once connected, obtain an IP address using dhclient:

sudo dhclient wlan0
   

This command configures the network interface with an IP address, allowing you to access the internet.

Step 4: Troubleshooting Connection Issues

If you encounter issues connecting to WiFi, here are some troubleshooting steps:

    • Check the interface status:

ip link show wlan0
   

Ensure that your wireless interface is up and running.

    • Check the logs:

If the connection fails, check the logs for wpa_supplicant:

sudo journalctl -u wpa_supplicant
   

This command provides detailed logs that can help diagnose the issue.

    • Restart the network interface:

If the connection is unstable, try restarting the network interface:

sudo ip link set wlan0 down
   sudo ip link set wlan0 up
   

Conclusion

Connecting to WiFi from a root shell is a useful skill, especially when working in a non-GUI environment or troubleshooting network issues. By following this guide, you can easily connect to a WiFi network using either nmcli for simplicity or wpa_supplicant for a more manual approach. These methods ensure that you can maintain network connectivity even when operating at the root level.

Discussion (0)

Please sign in to join the discussion.

No comments yet. Be the first to share your thoughts!