Saturday, 14 May 2016

How to connect to the WiFi network with WPA/WPA2 encryption from shell?

Most of the modern distros uses GUI tools to manage network connections. For example Ubuntu contains Network Manager, but sometimes there is need to connect to the WiFi network manually. Unfortunately, an old and simple method which uses iwconfig will work on the WEP encrypted networks only which are obsolete now. On the WPA/WPA2 encrypted networks you should use another simple method, which uses the wpa_supplicant tool.

First, turn on the WiFi interface:

ifconfig wlan0 up

Than generate wpa_supplicant config file. Type:

wpa_passphrase SSID >> /etc/wpa.conf

where SSID is your WiFi network name. Than put your WiFi password and hit enter. Next, connect to the WiFi network:

wpa_supplicant -B -D wext -i wlan0 -c /etc/wpa.conf

and get your IP number via DHCP:

dhclient wlan0

Now everything should work fine on the common setups.