This tutorial briefly explains how to connect to Wifi from the command line on Linux Debian and Debian based distributions in 3 different ways: using nmcli, nmtui and wpa_supplicant. Additionally to nmcli, nmtui and wpa_supplicant, the commands iwconfig, iwlist and dhclient are used and briefly explained in this tutorial.

Discovering networks using Iwconfig:

Before starting we need to know the essid or name of the network we want to connect to.

This tutorial shows how to use the Iwconfig command to scan available networks. Iwconfig is similar to the command ifconfig but to manage wireless interfaces. This command allows the user to edit the frequency or channel, to change the network device mode (Ad-hoc, Managed, Master, Repeater, Monitor, Secondary), to set an ESSID, etc.

NOTE: SSID/ESSID are network or router names or identifiers.

To check if the wifi card is properly detected first execute the command iwconfig as shown in the following screenshot:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/05/echo/word-image-438.png" data-lazy- height="369" src="data:image/svg xml,” width=”828″>

As you can see the output shows the loopback (lo) interface,the ethernet card (enp2s0) and the wifi card wlp3s0 which we’ll use to scan available networks using the command Iwlist.

The first line shows support for 802.11 standards and reveals the device isn’t connected. The second line shows the wifi card is in managed mode, not associated with an Access Point.

The third line includes Retry Short Limit which determines the number of attempts after a failed transmission and RTS Threshold defining the number of confirmations prior to a connection, Fragment Thr shows the maximum packet size your card will send.

Last two lines show there is no authentication and Power Management is off.

The command Iwlist provides additional information on wireless devices, including those which aren’t part of our computer. In this case we will scan available networks adding the argument scan. The output will display a list of Access Points with some information such as ESSID, signal quality, channel, mode, etc.

Run the following command to print a list of available networks:

NOTE: Replace wlp3s0 for your wireless card displayed when the command iwconfig was executed.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/05/echo/word-image-439.png" data-lazy- height="704" src="data:image/svg xml,” width=”938″>

As you can see the output shows several networks including the LinuxHint Access Point, yet the format isn’t user friendly. If you only want to print the ESSID or names of available networks omitting the rest, run:

sudo iwlist wlp3s0 scan | grep ESSID

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/05/echo/word-image-440.png" data-lazy- height="325" src="data:image/svg xml,” width=”718″>

As you can see now the output is clear and limited to access point names (essid).

Connecting to wifi from the command line using nmcli:

NMCLI is a command line interface for the NetworkManager which can be used as an alternative to graphical managers. NMCLI allows the user to create, to edit and remove or to enable and disable connections as well as display device status.

The following syntax shows how to connect to the LinuxHint network (discovered with the previously used command Iwlist) by executing nmcli with the following arguments:

nmcli d wifi connect LinuxHint password morochita

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/05/echo/word-image-441.png" data-lazy- height="286" src="data:image/svg xml,” width=”848″>

Where d wifi specifies the wireless device, connect LinuxHint specifies the essid and password morochita the password.

NOTE: Replace LinuxHint for your router ssid and morochita for your actual password.

You can get additional information on nmcli at https://linux.die.net/man/1/nmcli

Connecting to wifi from the console using nmtui:

Nmtui is an interactive curses-based alternative to nmcli and Network Manager, on the console run:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/05/echo/word-image-442.png" data-lazy- height="381" src="data:image/svg xml,” width=”738″>

The first screen allows us to edit an existing connection, to activate a new connection and to edit our hostname. Choose the second option Activate a connection and press ENTER.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/05/echo/word-image-443.png" data-lazy- height="381" src="data:image/svg xml,” width=”738″>

Nmtui will show both wired and wireless available networks. Select your Access Point and press ENTER.

Note: for this example the ESSID was changed from LinuxHint to LinuxH1nt to show the password request step.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/05/echo/word-image-444.png" data-lazy- height="381" src="data:image/svg xml,” width=”738″>

The next screen will ask for the password, fill it and press ENTER to continue.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/05/echo/word-image-445.png" data-lazy- height="381" src="data:image/svg xml,” width=”738″>

And you’ll be connected.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/05/echo/word-image-446.png" data-lazy- height="381" src="data:image/svg xml,” width=”738″>

You can get additional information on Nmtui on it’s man page at https://www.mankier.com/1/nmtui

Connect to wifi using wpa_supplicant:

Wpa_supplicant is a supplicant which allows the negotiation in the authentication process. Contrary to nmcli and nmtui, wpa_supplicant isn’t installed by default on Debian.

To install wpa_supplicant on Debian based systems run:

sudo apt install wpasupplicant

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/05/echo/word-image-447.png" data-lazy- height="611" src="data:image/svg xml,” width=”718″>

You need to edit the file /etc/wpa_supplicant.conf adding your Access Point essid and password, you can achieve it executing the following command:

wpa_passphrase LinuxHint morochita | sudo tee /etc/wpa_supplicant.conf

NOTE: Replace LinuxHint for your essid and morochita for your actual password. You can use quotation marks for network names containing spaces.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/05/echo/word-image-449.png" data-lazy- height="400" src="data:image/svg xml,” width=”968″>

Once wpa_supplicant.conf is edited, you can connect by executing the following command where -c specifies the configuration file and -i specifies the network interface:

sudo wpa_supplicant -c /etc/wpa_supplicant.conf -i wlp3s0

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/05/echo/word-image-450.png" data-lazy- height="381" src="data:image/svg xml,” width=”968″>

As you can see using iwconfig, now your wireless card is associated with the access point.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/05/echo/word-image-452.png" data-lazy- height="343" src="data:image/svg xml,” width=”828″>

To connect, run the command dhclient as shown in the following screenshot to get a dynamic IP address:

NOTE: Replace wlp3s0 for your wireless card.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/05/echo/word-image-453.png" data-lazy- height="248" src="data:image/svg xml,” width=”738″>

After executing dhclient, you should be connected to the internet through the associated access point.

You can get additional information on wpa_supplicant at https://linux.die.net/man/8/wpa_supplicant

I hope you found this tutorial useful to connect to wifi from the command line on Debian based systems. Keep following LinuxHint for more Linux tutorials and tips.

About the author

<img alt="" data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/05/echo/linuxinstitute_icono.png" height="112" src="data:image/svg xml,” width=”112″>

Ivan Vanney

Ivan Vanney has over 2 years as writer for LinuxHint, he is co-founder of the freelance services marketplace GIGopen.com where he works as a sysadmin.