Command Line: Connect to WiFi

Command Jul 7, 2023

Assume the most common case of WPA/WPA2 WiFi with password.

This is harder than the WEP or no password.

If you don’t know the terms, you are using WPA/WPA2.

Step 1: Check Device and Driver

Assume your network device is pci, now we check

  1. device is known
  2. driver is available and loaded
$ lspci -k...
Network controller: Realtek Semiconductor Co., Ltd.
Kernel driver in use: rtl8821ae
Kernel modules: rtl8821ae

Step 2: Check Interface

There are many devices and also many way to connect to the devices called “interface”.

This step we will switch the interface on.

$ ip addr...
wlp3s0: <BROADCAST,MULTICAST> ...$ sudo ip link set wlp3s0 up
$ ip addr
wlp3s0: <BROADCAST,MULTICAST,UP,LOWER_UP> ...

It’s important to note that interface up doesn’t mean you are online.

Note: in my case interface name is wlp3s0 this might vary e.g. in your case you might get wlan0 instead or wlp with different number

Tips: wlp3s0 means wl = wireless, p3 = pci #3, s0 = socket #0

Step 3: Scan for WiFi

Now we are looking for WiFi to connect just like in the drop-down list in the GUI menu.

$ iw dev wlp3s0 scan
Looking for SSID

...

Tips: use less if you cannot scroll

Step 4: Connect with WPA

Supposed that my SSID is Huawei-2G with password 123456

The command line to generate passphrase will be below and we save the output to file wpa.conf

$ wpa_passphrase Huawei-2G 123456 > wpa.conf

Then we will use this to connect

$ sudo wpa_supplicant -c wpa.conf -i wlp3s0 -B
***You need sudo for wpa_supplicant

Step 5: Obtain IP from DHCP

Now you have connect to your router but not yet have ip assigned

$ ip addr
// looking if inet section appears$ sudo dhclient
// obtain ip$ ip addr
// inet section should appears

If you already have inet section this will force to obtain new ip.

***You also need sudo for dhclient

Step 6: ping google.com

$ ping google.com
PING google.com ...56(84) bytes of data.
64 bytes from ... time=52.8 ms
64 bytes from ... time=28.6 ms

Here you go.

Now we have a connection. Enjoy!

Tags

TeamCMD

We are CODEMONDAY team and provide a variety of content about Business , technology, and Programming. Let's enjoy it with us.