How to connect to a Wi-Fi network from Linux command line

  1. Scan the list of available Wi-Fi networks through all connected Wi-Fi devices:
khadas@Khadas:~$ nmcli device wifi list
IN-USE  BSSID              SSID             MODE   CHAN  RATE        SIGNAL  BARS  SECURITY    
        00:33:22:AA:FF:77  DEFCON-338LM     Infra  6     130 Mbit/s  100     ▂▄▆█  WPA1 WPA2   
        55:A1:78:14:FF:F1  NeighborsWiFi09  Infra  11    195 Mbit/s  55      ▂▄__  WPA2 802.1X 
        AA:BB:CC:DD:EE:FF  NeighborsWiFi5   Infra  11    195 Mbit/s  55      ▂▄__  WPA2        

IN-USE  BSSID              SSID             MODE   CHAN  RATE        SIGNAL  BARS  SECURITY    
        00:33:22:AA:FF:77  DEFCON-338LM     Infra  6     130 Mbit/s  100     ▂▄▆█  WPA1 WPA2   
        55:A1:78:14:FF:F1  NeighborsWiFi09  Infra  11    195 Mbit/s  55      ▂▄__  WPA2 802.1X 
        AA:BB:CC:DD:EE:FF  NeighborsWiFi5   Infra  11    195 Mbit/s  55      ▂▄__  WPA2        
  1. Choose a Wi-Fi network and connect to it by passing the password:
khadas@Khadas:~$ sudo nmcli device wifi connect DEFCON-338LM password MySup3rS3cur3P455w0rd
Device 'wlan0' successfully activated with '5962209f-4d0b-45ae-85fe-de9c852d2d64'.
  1. Check the status of the connections:
khadas@Khadas:~$ nmcli connection show
NAME                UUID                                  TYPE      DEVICE 
Wired connection 1  0325e9e9-b79f-35ef-b961-313bc9e1546d  ethernet  eth0   
DEFCON-338LM        5962209f-4d0b-45ae-85fe-de9c852d2d64  wifi      wlan0  
Free-Mobile         1b25747a-4887-4d73-b2e8-944ae6cc3697  gsm       --     
Free SIM Card CLI   f59b8ee1-de54-48c6-837c-8001a555ccfa  gsm       --     
  1. Check for Internet access by pinging some website:
khadas@Khadas:~$ ping www.google.com
PING www.google.com(nor05s66-in-x04.1e100.net (5g90:1ff0:0007:825::2004)) 56 data bytes
64 bytes from tshirt90-in-x04.1e100.net (5g90:1ff0:0007:825::2004): icmp_seq=1 ttl=114 time=19.9 ms
64 bytes from tshirt90-in-x04.1e100.net (5g90:1ff0:0007:825::2004): icmp_seq=2 ttl=114 time=21.4 ms
64 bytes from tshirt90-in-x04.1e100.net (5g90:1ff0:0007:825::2004): icmp_seq=3 ttl=114 time=24.2 ms
64 bytes from tshirt90-in-x04.1e100.net (5g90:1ff0:0007:825::2004): icmp_seq=4 ttl=114 time=25.0 ms
^C
--- www.google.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3005ms
rtt min/avg/max/mdev = 19.904/22.647/25.035/2.082 ms

That concludes the connection part. However if a disconnection is needed, this is the command to run:

khadas@Khadas:~$ sudo nmcli device disconnect wlan0
Device 'wlan0' successfully disconnected.