How to Connect to Wi-Fi via the Terminal in Debian: 3 Proven Methods

Connecting to a wireless network via a terminal in Debian or its derivatives (for example, Ubuntu Server, Raspberry Pi OS) may seem like a daunting task for beginners. However, this skill is indispensable when a graphical interface is not available—for example, on a server without GUI, in a restored system or when configuring remotely via SSHUnlike Windows or macOS, where connecting to Wi-Fi is intuitive, Linux requires knowledge of specific commands and utilities.

In this article we will look at three main methods: using NetworkManager (through nmcli), manual tuning with wpa_supplicant and low-level commands iwconfig/ifconfigEach method has its advantages: nmcli easier for everyday tasks, wpa_supplicant more reliable for servers, and manual commands will help in emergency situations. We will also cover common errors (for example, "Device not managed") and ways to eliminate them.

Before you begin, make sure your Wi-Fi adapter is supported by the Linux kernel. To do this, run the command lspci | grep -i wireless (for PCI devices) or lsusb (for USB adapters). If the adapter is not detected, you may need to install proprietary drivers (for example, for chips) Broadcom).

📊 Which Wi-Fi connection method do you use most often?
Through the graphical interface
NetworkManager (nmcli)
wpa_supplicant
Manual commands (iwconfig)
I don't know what this is

1. Connecting via NetworkManager (nmcli)

NetworkManager — a standard network management tool in most Debian distributions. The utility nmcli allows you to manage connections from a terminal without a graphical interface. This method is suitable for desktop systems and servers with NetworkManager.

To connect to Wi-Fi:

  1. Check NetworkManager status:
    sudo systemctl status NetworkManager

    If the service is not active, start it:

    sudo systemctl start NetworkManager
  2. Get a list of available networks:
    nmcli device wifi list

    Or refresh the list if the networks are not displayed:

    nmcli device wifi rescan
  3. Connect to the network:
    nmcli device wifi connect "NETWORK_NAME" password "PASSWORD"

    Replace NETWORK_NAME And PASSWORD for up-to-date data. For hidden networks, add a flag hidden yes.

If the connection was successful, check the status:

nmcli connection show

Or look at the IP address:

ip a show wlan0

Adapter enabled (rfkill unblock wifi)|NetworkManager service active|Networks displayed in nmcli device wifi list|Password entered correctly-->

⚠️ Attention: If the team nmcli device wifi list returns an error "Error: Device 'wlan0' is not active", check if the adapter is disabled programmatically by the command rfkill listUnlock it with rfkill unblock wifi.

2. Manual connection with wpa_supplicant

Utility wpa_supplicant — a more flexible and reliable way to connect to Wi-Fi, especially on servers without NetworkManagerIt supports modern safety standards (WPA3, 802.1X) and allows you to save configurations in a file /etc/wpa_supplicant/wpa_supplicant.conf.

Setup instructions:

  1. Install wpa_supplicant (if not installed):
    sudo apt update && sudo apt install wpa-supplicant
  2. Generate a configuration file:
    wpa_passphrase "NETWORK_NAME" "PASSWORD" | sudo tee /etc/wpa_supplicant/wpa_supplicant.conf

    This command will create an encrypted file with settings.

  3. Connect to the network:
    sudo wpa_supplicant -B -i wlan0 -c /etc/wpa_supplicant/wpa_supplicant.conf

    Flag -B runs the process in the background.

  4. Obtain an IP address via DHCP:
    sudo dhclient wlan0

To make the connection automatically at boot, add to /etc/network/interfaces:

auto wlan0

iface wlan0 inet dhcp

wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf

What to do if wpa_supplicant does not connect?

If after launch wpa_supplicant The connection is not established, check the logs:

journalctl -u wpa_supplicant -fCommon causes:

- Incorrect password or network name (check the case!).

- Unsupported security standard (eg. WPA3-Enterprise requires additional parameters in the config).

- Adapter lock (rfkill list).

Error Possible cause Solution
Failed to initialize driver The Wi-Fi adapter driver is not loaded. Check it out dmesg | grep wlan, install proprietary drivers
No network configuration found File wpa_supplicant.conf empty or missing Generate the config again with wpa_passphrase
Association with AP failed Incorrect password or security standard Check your password, add key_mgmt=WPA-PSK in the config
DHCP discovery failed IP address is not assigned Launch dhclient wlan0 manually or check your router settings

3. Low-level connection with iwconfig and ifconfig

If NetworkManager And wpa_supplicant are not available, you can connect to an open network or a network with WEP- encryption (not recommended!) using basic utilities iwconfig And ifconfigThis method is appropriate for minimalistic systems or for diagnostics.

Warning: WEP encryption is outdated and insecure. Use this method only for testing or connecting to open networks.

  1. Check the wireless interface name:
    iwconfig

    Usually it is wlan0 or wlp3s0.

  2. Connect to an open network:
    sudo iwconfig wlan0 essid "NETWORK_NAME" key off
  3. Get an IP address:
    sudo dhclient wlan0
  4. For WEP network (not recommended!):
    sudo iwconfig wlan0 essid "NETWORK_NAME" key "PASSWORD_IN_HEX"

    The password must be in hex format (For example, 123456789031323334353637383930).

⚠️ Attention: Teams ifconfig And iwconfig are considered obsolete in newer distributions. It is recommended to use them instead. ip And iwFor example, to view networks:
sudo iw dev wlan0 scan | grep SSID
sudo apt install wireless-tools

However, for modern systems it is better to use iw from the package iw (already pre-installed in most distributions).-->

4. Diagnosing connection problems

If your Wi-Fi isn't connecting, start by checking the following:

  • 🔍 Is the adapter turned on? Check with rfkill listIf the adapter is blocked, unlock it:
    rfkill unblock wifi
  • 📡 Does the system see networks? Perform a scan:
    sudo iw dev wlan0 scan | grep SSID

    If there are no networks, the problem may be in the driver or antenna.

  • 🔑 Is the password correct? Make sure your password is entered correctly (case-sensitive!). To test, create a test config:
    wpa_passphrase "NETWORK_NAME" "PASSWORD"
  • 🔧 Is the driver loaded? Check kernel logs:
    dmesg | grep wlan

    If there are errors of the type "firmware missing", install proprietary drivers (for example, for Broadcom).

If the problem persists, check the logs. wpa_supplicant:

journalctl -u wpa_supplicant -b

Or check the network status:

ip link show wlan0

1) Lack of/incorrect installation of drivers.

2) Adapter lock (rfkill).

3) Errors in configuration files (wpa_supplicant.conf or /etc/network/interfaces).-->

5. Automatic connection on boot

To connect Wi-Fi automatically after a reboot, set up autostart wpa_supplicant And dhclient:

  1. Edit /etc/network/interfaces:
    auto wlan0
    

    iface wlan0 inet dhcp

    wpa-ssid NETWORK_NAME

    wpa-psk PASSWORD

    Note: The password must be in hex format (generate it using wpa_passphrase).
  2. Alternatively, for systemd:

    Create a service /etc/systemd/system/wpa_supplicant.service:

    [Unit]
    

    Description=WPA supplicant

    After=sys-subsystem-net-devices-wlan0.device

    [Service]

    Type=simple

    ExecStart=/sbin/wpa_supplicant -B -i wlan0 -c /etc/wpa_supplicant/wpa_supplicant.conf -D nl80211,wext

    ExecStop=/sbin/killall wpa_supplicant

    [Install]

    WantedBy=multi-user.target

    Then activate it:

    sudo systemctl enable wpa_supplicant

For NetworkManager it is enough to mark the connection as automatic:

nmcli connection modify "CONNECTION_NAME" connection.autoconnect yes

6. Security: How to protect your connection

When setting up Wi-Fi via a terminal, it is important to ensure the security of configuration files:

  • 🔒 Access rights: File /etc/wpa_supplicant/wpa_supplicant.conf should be accessible only by root:
    sudo chmod 600 /etc/wpa_supplicant/wpa_supplicant.conf
  • 🛡️ Use modern standards: Avoid WEP And WPA — set up WPA2-PSK or WPA3. In the config wpa_supplicant please specify:
    proto=RSN
    

    key_mgmt=WPA-PSK

    pairwise=CCMP

  • 🔄 Update your software regularly: Drivers and utilities (eg wpa_supplicant) may contain vulnerabilities. Update them with the command:
    sudo apt update && sudo apt upgrade
  • 📛 Hide SSID (optional): If the network should not be visible to everyone, add the following setting to the router configuration hide_ssid=1 and connect with the flag scan_ssid=1 V wpa_supplicant.

Warning: Never store passwords in cleartext in scripts or configuration files with broad access rights. Use wpa_passphrase to generate hashed versions.

FAQ: Frequently Asked Questions

My Wi-Fi adapter isn't detected. What should I do?

First, check if the adapter is detected by the system:

lspci -knn | grep -iA3 net

If the adapter is listed but does not work, install the driver. For Broadcom:

sudo apt install firmware-b43-installer

For Realtek a driver from the repository may be required non-freeAdd it to /etc/apt/sources.list:

deb http://deb.debian.org/debian bookworm main contrib non-free
How to connect to Wi-Fi without a password (open network)?

For an open network with wpa_supplicant create a config:

network={

ssid="NETWORK_NAME"

key_mgmt=NONE

}

Then connect:

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

sudo dhclient wlan0

Through nmcli:

nmcli device wifi connect "NETWORK_NAME"
Why doesn't Wi-Fi connect automatically after a reboot?

The reasons may be as follows:

  1. NetworkManager Doesn't save settings. Check:
    nmcli connection show

    If the connection is missing, recreate it with the flag autoconnect yes.

  2. Service wpa_supplicant won't start. Check:
    systemctl status wpa_supplicant

    Activate autostart:

    sudo systemctl enable wpa_supplicant
  3. IN /etc/network/interfaces Incorrect settings. Example of a correct configuration:
    auto wlan0
    

    iface wlan0 inet dhcp

    wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf

Is it possible to connect to Wi-Fi via Terminal on Android (Termux)?

IN Termux Wi-Fi connectivity via the terminal is limited due to Android security. However, you can:

  1. Use termux-wifi-scaninfo to browse networks (requires permission).
  2. Connect via adb from PC:
    adb shell svc wifi enable
    

    adb shell am start -a android.settings.WIFI_SETTINGS

  3. Install tsu (Termux SU) to work with wpa_supplicant on rooted devices.

Important: On non-rooted devices, direct access to Wi-Fi via the terminal is blocked.

How to change the MAC address of a Wi-Fi adapter before connecting?

To change the MAC address (spoofing), run:

  1. Disable the interface:
    sudo ip link set wlan0 down
  2. Change MAC (for example to 00:11:22:33:44:55):
    sudo ip link set wlan0 address 00:11:22:33:44:55
  3. Enable the interface:
    sudo ip link set wlan0 up

Warning: MAC address spoofing can violate network usage rules (for example, on corporate or public Wi-Fi).