Connect to WiFi from command line

What’s the worst thing that can happen when you install a new Linux distribution on your system? I think it’s staring at the cursor on the command line and not knowing what to do next!!

This happened when I installed Debian on my system recently. After installing it all I got was a command line. And even if I wanted to install something or search something, I needed an internet connection. I have a wifi connection at home so I had to connect to WPA/WPA2 wifi using just a command line.
Now how would you do that?

Step 1
We will find out the name of your wireless adapter.

/sbin/iw dev

This will generate an output like this:

phy#0
Interface wlan0
ifindex 3

type managed
channel 6 (2437 MHz), width: 40 MHz, center1: 2447 Mhz

From this, you can understand that your device name is wlan0. Type managed means the device is a WiFi station or client that connects to an access point. You will get Interface index as per connected ports.

Step 2
Check if the wireless device wlan0 is up.
Run the following command:

ip link show wlan0

and in the output check, if you can see UP. You mostly will get an output like this. As you can see there’s no UP in those brackets.

wlan0: <BROADCAST,MULTICAST> …

Step 3
If your wireless device is not up, execute this command to bring it up.

sudo ip link set wlan0 up  

and you can verify if it is up using the command from step 2.

ip link show wlan0

which should give you and output like this:

3: wlan0: <BROADCAST,MULTICAST,UP,LOWER_UP>

As you can see now the wireless device is UP.

Step 4
Now let’s check if you are connected to any network

/sbin/iw wlan0 link
Not Connected

This means that you are not connected to any network as of now. First, we will have to see what all networks are available. To do that execute the following command.

sudo /sbin/iw wlan0 scan

This will list out all the available networks for you. You will see something like this:

BSS 6c:72:20:6b:31:8d(on wlan0)

freq: 2417
beacon interval: 100 TUs
Information elements from Probe Response frame:
SSID: Manish
Supported rates: 1.0* 2.0* 5.5* 11.0* 6.0 9.0 12.0 18.0
Extended supported rates: 24.0 36.0 48.0 54.0

RSN: * Version: 1
* Group cipher: CCMP
* Pairwise ciphers: CCMP
* Authentication suites: PSK
* Capabilities: 1-PTKSA-RC 1-GTKSA-RC (0x0000)
WPS: * Version: 1.0There are two things to look out here.
1. SSID
Manish
2. Security Protocol
RSN i.e. WPA2

Now let’s connect to your network.
Execute the following command:

sudo su
wpa_passphrase Manish >> /etc/wpa_supplicant.conf
<enter your wifi password>
cat /etc/wpa_supplicant.conf 

wpa_passphrase will create necessary configuration entries based on your input. Now to connect to your network execute the following command:

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

-B means run wpa_supplicant in the background.
-D specifies the wireless driver. wext is the generic driver
-c specifies the path for the configuration file.

Step 5
execute this of you are connected to the network you selected

/sbin/iw wlan0 link

Output will be

Connected to 98:de:d0:27:b0:40 (on wlan0)
SSID: Manish
freq: 2437
RX: 11068520 bytes (58070 packets)
TX: 1355722 bytes (7569 packets)
signal: -36 dBm
tx bitrate: 150.0 MBit/s MCS 7 40MHz short GIbss flags: short-preamble short-slot-time
dtim period: 0
beacon int: 100

Step 6
Obtain IP address:

sudo dhclient wlan0

verify if IP address was set

ip addr show wlan0
wlan0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 4c:bb:58:41:ac:45 brd ff:ff:ff:ff:ff:ff
inet 192.168.0.105/24 brd 192.168.0.255 scope global dynamic wlan0
valid_lft 7086sec preferred_lft 7086sec
inet6 fe80::4ebb:58ff:fe41:ac45/64 scope link
valid_lft forever preferred_lft forever

To add a default routeing rule to pass all other traffic through wlan0 execute the following command:

sudo ip route add default via 192.168.0.105 dev wlan0

Now we can check if we can access the internet using a simple ping command.

Ping 8.8.8.8
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=1 ttl=59 time=6.91 ms
64 bytes from 8.8.8.8: icmp_seq=2 ttl=59 time=7.17 ms
64 bytes from 8.8.8.8: icmp_seq=3 ttl=59 time=7.40 ms
64 bytes from 8.8.8.8: icmp_seq=4 ttl=59 time=6.72 ms
64 bytes from 8.8.8.8: icmp_seq=5 ttl=59 time=7.99 ms
64 bytes from 8.8.8.8: icmp_seq=6 ttl=59 time=6.89 ms
64 bytes from 8.8.8.8: icmp_seq=7 ttl=59 time=6.86 ms
^C
— 8.8.8.8 ping statistics —
7 packets transmitted, 7 received, 0% packet loss, time 6010ms
rtt min/avg/max/mdev = 6.725/7.137/7.996/0.419 ms

Leave a Reply

Your email address will not be published. Required fields are marked *

fourteen + 16 =