How to Connect Wi-Fi from Linux Terminal Using Nmcli

nmcli is a command-line tool for controlling NetworkManager and reporting network status. It can be utilized as a replacement for nm-applet or other graphical clients. nmcli is used to create, display, edit, delete, activate, and deactivate network connections, as well as control and display network device status.

Typical uses include:

  • Scripts: Utilize NetworkManager via nmcli instead of managing network connections manually. nmcli supports a terse output format which is better suited for script processing. Note that NetworkManager can also execute scripts, called “dispatcher scripts”, in response to network events. See NetworkManager(8) for details about these dispatcher scripts.
  • Servers, headless machines, and terminals: nmcli can be used to control NetworkManager without a GUI, including creating, editing, starting and stopping network connections and viewing network status.

Following script provides details about how you can connect to WiFi access point using commands in Linux. We have used nmcli command for scanning and connecting to WiFi.

Note: Edit below script to update with your access point SSID and Password.

$ vim connect_to_wifi.sh 
#!/bin/sh
ACCESS_POINT_SSID=myaccesspoint_name
ACCESS_POINT_PASSWORD=myaccesspoint_password
echo "scanning..."
nmcli d wifi list
 
echo -n "connecting...."
#nmcli d wifi connect $ACCESS_POINT_SSID password $ACCESS_POINT_PASSWORD iface wlan0
nmcli d wifi connect $ACCESS_POINT_SSID password $ACCESS_POINT_PASSWORD
echo "done"
Usage: nmcli [OPTIONS] OBJECT { COMMAND | help }
 
OPTIONS
  -t[erse]                                   terse output
  -p[retty]                                  pretty output
  -m[ode] tabular|multiline                  output mode
  -c[olors] auto|yes|no                      whether to use colors in output
  -f[ields] <field1,field2,...>|all|common   specify fields to output
  -e[scape] yes|no                           escape columns separators in values
  -a[sk]                                     ask for missing parameters
  -s[how-secrets]                            allow displaying passwords
  -w[ait] <seconds>                          set timeout waiting for finishing operations
  -v[ersion]                                 show program version
  -h[elp]                                    print this help
 
OBJECT
  g[eneral]       NetworkManager's general status and operations
  n[etworking]    overall networking control
  r[adio]         NetworkManager radio switches
  c[onnection]    NetworkManager's connections
  d[evice]        devices managed by NetworkManager
  a[gent]         NetworkManager secret agent or polkit agent
  m[onitor]       monitor NetworkManager changes
$ bash connect_to_wifi.sh

scanning...
* SSID MODE CHAN RATE SIGNAL BARS SECURITY
myaccesspoint_name Infra 1 54 Mbit/s 62 ▂▄▆_ WPA1 WPA2
-- Infra 8 54 Mbit/s 27 ▂___ WPA2
Device 'wlan0' successfully activated with 'some-UUID'.
done

Leave a Comment