How to Connect to WiFi using Command Line on Ubuntu

A. This Part is for WEP Wireless Network

Check wireless adapter:

$ iwconfig

You will see wireless adapter as wlan0 or ath0 which depends on what driver is installed.
Execute the following command to apply ESSID, network key, channel, and mode:

$ sudo iwconfig wlan0 essid yantoknuk-singtel
$ sudo iwconfig wlan0 channel 6
$ sudo iwconfig wlan0 key ngumbahkucing
$ sudo iwconfig wlan0 mode managed

After that, bring up the interface:

$ sudo ip link set wlan0 up

Use DHCP to get IP address from access point:

$ sudo dhclient wlan0

Check whether the WiFi interface is connected to access point:

$ ifconfig
wlan0 Link encap:Ethernet HWaddr 00:16:cf:33:54:9b
inet addr:192.168.1.50 Bcast:192.168.1.255 Mask:255.255.255.0
inet6 addr: fe80::216:cfff:fe33:549b/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:9188 errors:0 dropped:0 overruns:0 frame:0
TX packets:2504 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:2423370 (2.4 MB) TX bytes:1265327 (1.2 MB)

Finish.

 

B. This Part is for WPA Wireless Network

Install wpa supplicant package:

$ sudo apt-get install wpasupplicant

Configure network ESSID and passphrase.
In this example I use ESSID ‘yantoknuk-singtel’ and passphrase ‘ngumbahkucing’.

$ wpa_passphrase yantoknuk-singtel
# reading passphrase from stdin

After that, type in your passphrase right below the text ‘# reading passphrase from stdin’.

# reading passphrase from stdin
ngumbahkucing
network={
ssid="yantoknuk-singtel"
#psk="ngumbahkucing"
psk=1e6eb493adf24740e2d3e98759b8b985e69f0bb939931c19400ceeecb61788e1
}

Edit or create /etc/wpa_supplicant.conf:

$ sudo nano /etc/wpa_supplicant

And then type in the following lines which is produced by wpa_passphrase before:

network={
ssid="yantoknuk-singtel"
#psk="ngumbahkucing"
psk=1e6eb493adf24740e2d3e98759b8b985e69f0bb939931c19400ceeecb61788e1
}

Save and exit nano.
To test the configuration, execute this:

$ sudo wpa_supplicant -iwlan0 -c/etc/wpa_supplicant.conf -Dwext

If it’s successful you should see something like the following:

Trying to associate with 00:ff:00:1e:a7:7d (SSID='yantoknuk-singtel' freq=0 MHz)
Associated with 00:ff:00:1e:a7:7d
WPA: Key negotiation completed with 00:ff:00:1e:a7:7d [PTK=TKIP GTK=TKIP]

It’s finish. Your computer is now associated / connected to the access point.
To make it autorun on boot, add in these lines to /etc/network/interfaces:

$ sudo nano /etc/network/interfaces

And add the following lines:

auto wlan0
iface wlan0 inet dhcp
wpa-driver wext
wpa-conf /etc/wpa_supplicant.conf

That’s all. The complete information can be read here