How to connect to a Wi-Fi network via command line in Linux

The most easiest way to connect to a wireless network in Linux is by using Network Manager’s GUI, but in this tutorial we will learn how to connect via CLI, root privileges are required, tested on Debian 9.6.

1. Install the software

apt-get install wireless-tools wpasupplicant

2. Make sure your network card was detected, in this example my wireless card is wlan0.
If your wireless card is not detected please check this guide -> help.ubuntu.com.

iwconfig
wlan0     unassociated  ESSID:""  Nickname:"<WIFI@REALTEK>"
          Mode:Managed  Frequency=2.412 GHz  Access Point: Not-Associated   
          Sensitivity:0/0  
          Retry:off   RTS thr:off   Fragment thr:off
          Encryption key:off
          Power Management:off
          Link Quality:0  Signal level:0  Noise level:0
          Rx invalid nwid:0  Rx invalid crypt:0  Rx invalid frag:0
          Tx excessive retries:0  Invalid misc:0   Missed beacon:0

lo        no wireless extensions.

eth0      no wireless extensions.

3. Create wpa_supplicant.conf based on your SSID and password.

wpa_passphrase YourSSID >> /etc/wpa_supplicant.conf

Enter your password and press Enter, passphrase must be 8 – 63 characters.

my_dummy_password

4. Authenticate to your network

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

5. Check if you are authenticated, you should have an Access Point associated.

iwconfig wlan0
wlan0     IEEE 802.11bgn  ESSID:"YourSSID"  Nickname:"<WIFI@REALTEK>"
          Mode:Managed  Frequency:2.432 GHz  Access Point: 11:22:33:AA:BB:CC   
          Bit Rate:72.2 Mb/s   Sensitivity:0/0  
          Retry:off   RTS thr:off   Fragment thr:off
          Encryption key:****-****-****-****-****-****-****-****   Security mode:open
          Power Management:off
          Link Quality=100/100  Signal level=100/100  Noise level=0/100
          Rx invalid nwid:0  Rx invalid crypt:0  Rx invalid frag:0
          Tx excessive retries:0  Invalid misc:0   Missed beacon:0

Note: If you have issues while authenticating please check syslog.

6. Request or set IP address.
Now that you are authenticated to communicate with your network you must have an IP address, you must request it from a DHCP server or manually set.
Note: If you are using netplan please skip this step and check https://netplan.io/examples.

a) DHCP

dhclient -nw wlan0

b) Set IP address, default gateway and DNS server

ip addr add dev wlan0 192.168.1.100/24
ip route add default via 192.168.1.1
echo "nameserver 8.8.8.8" > /etc/resolv.conf

Leave a Reply

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