How to connect to wifi from terminal

Whenever you install new Linux distribution on your computer, it’s very recommended that you connect to a network whether it’s via wired connection or a wifi network. Now let’s we assume you don’t have any wired connection and only wifi connection is available. The only way is connect to a wifi network whether it’s from GUI or from terminal/command line. But as the title of this post says, I will use the basic way by using a programs and utilities that included as a default packages of any Linux distribution and an obvious reason why this tutorial can potentially be reproduced on any Linux computer.

“iw” is a programs and utilities for show / manipulate wireless devices and their configuration, you can use “iw” without any GUI or in other words just using terminal/command line. So how do we connect to wifi from terminal ? Just follow these instructions

I will assume that you have working wireless LAN card, if you have trouble with your WLAN card you should take a look at the Wiki and documentation for your Linux distribution.

STEP 1

Check Wireless Interface Support

First to connect to your wifi, you must check which interface supports wireless by using this command :

$ iwconfig

How to connect to a wifi network from terminal 1

In general, the interface that support wireless is called wlan0.

STEP 2

Up the interface

Before you connecting to your wifi don’t forget to make sure that the interface is up by using this command :

$ sudo ip link set wlan0 up

STEP 3

Scan wireless network

Once your interface is up, you should scan nearby wireless network. To scan wireless network use the following command

$ sudo iw wlan0 scan | less

How to connect to a wifi network from terminal 2

From the output, you can get some information about the wifi network, for example it’s SSID and BSSID, it’s signal power, and what security it uses.

STEP 4

Connect to Wifi Network

In this step we will try to connect to your wifi whether it’s an open network or encrypted network.

OPEN

If the network you want to connect is not encrypted (OPEN), you can use this following

$ sudo iw wlan0 connect [network SSID]

WEP

Now if the network is uses WEP encryption, just use this simple command :

$ sudo iw wlan0 connect [network SSID] key 0:[WEP Key]

WPA/WPA2

Now what if the network uses WPA or WPA2 encryption ?, in this tutorial I will try connect to my “Tutorial Network”, and it’s security is uses WPA/WPA2. Before connecting you must use the utility called wpa_supplicant then you have to modify the file, to modify wpa_supplicant use this command :

$ sudo nano /etc/wpa_supplicant/wpa_supplicant.conf

and add this line :

network={
ssid="[network ssid]"
psk="[the passphrase]"
priority=1
}

Change the SSID to your network SSID and change the passphrase. Be careful both SSID and passphare is case sensitive. Now after configuration is completed, save the configuration file and launch this command in the background :

STEP 5

Run DHCP

Finally, whether you connected to open or secure network. you have to get an IP address, to get an IP address, simply use this following command :

$ sudo dhcpcd wlan0

If everything is fine, you should get a new local IP address from DHCP and If you want to check that you are connected, you can always check again with :

$ iwconfig

How to connect to a wifi network from terminal 3

Conclusion

There are a lot of ways to connect to a network whether it’s via wired connection or wireless, and there are a lot of ways to manage your connection (e.g., NetworkManager, netcfg, wifi, wicd), But you never know when you cannot access a wired connection, or you cannot access your GUI, getting ready now seems very important, so get used to the most basic way.

How do you think about this tutorial ? Do you know other ways to connect to wifi from terminal ? Please let us know in the comments. Thank You !