How do I connect to a WPA wifi network using the command line?
So basically, I’d like to use iwconfig to connect to my wifi network when I’m not inside X. But I just don’t see a way to do it in the man page. Any idea?
9 Answers 9
iw (list/config) can only handle WEP.
You need the wpasupplicant package which provides the wpa_supplicant command, install if necessary through sudo apt-get install wpasupplicant .
You put your SSID and password into /etc/wpa_supplicant.conf (requires sudo).
Assuming your interface is wlan0 you can connect with:
sudo wpa_supplicant -B -i wlan0 -c /etc/wpa_supplicant.conf -D wext sudo dhclient wlan0
«wext» is a driver and that will be specific for each card; refer to wpa_supplicant -h . Examples:
hostap (default) Host AP driver (Intersil Prism2/2.5/3). (this can also be used with Linuxant DriverLoader). hermes Agere Systems Inc. driver (Hermes-I/Hermes-II). madwifi MADWIFI 802.11 support (Atheros, etc.). atmel ATMEL AT76C5XXx (USB, PCMCIA). wext Linux wireless extensions (generic). ndiswrapper Linux ndiswrapper. broadcom Broadcom wl.o driver. ipw Intel ipw2100/2200 driver. wired wpa_supplicant wired Ethernet driver roboswitch wpa_supplicant Broadcom switch driver bsd BSD 802.11 support (Atheros, etc.). ndis Windows NDIS driver.
well thats nice that iwlist/iwconfig can handle only WEP but in my case wpa_supplicant -B -iwlan0 -c/etc/wpa_supplicant.conf -Dwext && dhclient wlan0 did not connected to the wireless network, though by default NetworkManager connects me to the network I want to try the command line way is there some mistake I am doing?
@Rinzwind, Yes. Looking through wpa_supplicant’s manpage shows that the driver may vary, so your command will likely not work as literally given. You have to choose the -D
This link shows it all and worked fine for me: http://linux.icydog.net/wpa.php
I’m copying the contents here, so we have it, in case that site goes offline.
Command line WPA
Sometimes you’ll be at a command line with no access to GUI networking tools — but your access point is secured with WPA. What do you do?
Assuming your wireless card actually works (i.e. iwconfig can see it and interact with it), using wpa_supplicant is actually pretty simple. Installing wpa_supplicant
Most distros nowadays have wpa_supplicant installed by default. If you have the commands wpa_passphrase and wpa_supplicant available, then you’re good to go. Otherwise, you will need to install the package by doing something like (for Ubuntu):
$ sudo apt-get install wpasupplicant
# yum install wpa_supplicant
Or whatever the command is for your distro.
Generating the config file
Now that wpa_supplicant is installed, we will create its configuration file. Once you know the SSID and WPA passphrase, all you have to do is run:
$ wpa_passphrase myrouter mypassphrase > wpa.conf
Of course, replace «myrouter» with the SSID of your router, «mypassphrase» with your WPA passphrase, and «wpa.conf» with whatever file you want to store the configuration in. This filename does not have to follow a particular format or have a particular extension.
Alternatively, to avoid typing the passphrase on the command line (so it doesn’t get saved in the shell’s history), you can specify just the SSID on the command line. wpa_passphrase will wait for you to type in the passphrase followed by enter:
$ wpa_passphrase myrouter > wpa.conf mypassphrase
You should end up with a file looking like this:
Getting connected
Now we will actually run wpa_supplicant to connect to the wireless network. First, if your router broadcasts its SSID (they all do by default), you probably want to make sure your wireless card can actually see it:
You might have to run that as root to force a refresh.
Next, you will need to know three pieces of information:
- Which wpa_supplicant wireless drivers to use for your card. Running wpa_supplicant —help lists the different drivers it has (under «drivers:»). As of 0.5.8, the useful choices are: wext , hostap , madwifi , atmel , ndiswrapper , and ipw (ipw is for old kernels only; >=2.6.13 should use wext). If you don’t see a specific match for your card, try wext, as that’s kind of the catch-all.
- The network device of your card. This is usually eth1 or wlan0, but if you’re unsure you can just run iwconfig . It will report «no wireless extensions» for non-wireless devices and will display some data for any wireless devices.
- The path to the configuration file that you created in the previous step.
Now that you have this data, run (as root):
# wpa_supplicant -D[driver] -i[device] -c[/path/to/config]
There are no spaces between the options and parameters. Don’t include the brackets as I just added those for clarity. For example, for my laptop it looks like this:
# wpa_supplicant -Dwext -ieth1 -c/root/wpa.conf
You can also run it in the background by using the -B option so that it doesn’t take up your console.
Now you’re associated with the network.
Getting online
To actually get online, you’ll have to get an IP somehow. Most people will just want to get a dynamic IP from a DHCP server, probably the one built into the router. (I’m not going to cover setting a static IP and routing table because that’s a beast in itself.)
To get a DHCP lease, first release whatever leases you’re still holding onto (as root):
Then ask for a new lease (of course replacing eth1 with the name of your network device, the same one as you used in the previous section):
You now have an IP, in theory at least. Happy surfing!
Configure WiFi Connections
This section explains how to establish a WiFi connection. It covers creating and modifying connections as well as directly connecting.
Establish a Wireless Connection
This section will show how to establish a wifi connection to the wireless network. Note that directly connecting will implicitly create a connection (that can be seen with “nmcli c”). The naming of such will follow “SSID N” pattern, where N is a number.
First, determine the name of the WiFi interface:
$ nmcli d DEVICE TYPE STATE CONNECTION . wlan0 wifi disconnected --
Make sure the WiFi radio is on (which is its default state):
Then, list the available WiFi networks:
$ nmcli d wifi list * SSID MODE CHAN RATE SIGNAL BARS SECURITY . my_wifi Infra 5 54 Mbit/s 89 ▂▄▆█ WPA2
As an example, to connect to the access point ‘my_wifi’, you would use the following command:
$ nmcli d wifi connect my_wifi password
is the password for the connection which needs to have 8-63 characters or 64 hexadecimal characters to specify a full 256-bit key.
Connect to a Hidden Network
A hidden network is a normal wireless network that simply does not broadcast it’s SSID unless solicited. This means that its name cannot be searched and must be known from some other source.
Issue the following command to create a connection associated with a hidden network :
$ nmcli c add type wifi con-name ifname wlan0 ssid $ nmcli c modify wifi-sec.key-mgmt wpa-psk wifi-sec.psk
Now you can establish a connection by typing:
is an arbitrary name given to the connection and is the password to the network. It needs to have between 8-63 characters or 64 hexadecimal characters in order to specify a full 256-bit key.
Further Information
You will find further information and more detailed examples on following pages:
How to connect to WiFi from the command line?
Other posts seems to be addressing more complicated network connection issues from the command line. The Unity panel Network indicator/button doesn’t respond too well sometimes — it keeps trying to connect to a network even when i click on «disconnect», stuff like that. So I want to go command line for the control. I don’t like GUIs anyway. Is there not some simple command line tool which can do something like the following?
wifi connect MyNetworkNameA wifi disconnect wifi connect MyNetworkNameB
Depends , what wireless security ? This is covered in detail here — help.ubuntu.com/community/NetworkConfigurationCommandLine/…
On Ubuntu 16.04 and on 18.04, still experiencing wifi disconnects. A reconnect command would be a good hack.
4 Answers 4
I think you want to keep using managed interface (by NetworkManager). nmcli is a command‐line tool for controlling NetworkManager.
- To see list of saved connections, use ( )
Just change , , in the following commands to reflect your setup. If WiFi info already saved, easier way using name of connection as it was saved in NetworkManager.
Ubuntu 16.04
##disconnect nmcli d disconnect ##connect nmcli d connect
##disconnect nmcli c down ##connect nmcli c up
Ubuntu 15.10 & previous
##disconnect nmcli d disconnect iface ##connect nmcli d wifi connect password iface
##disconnect: nmcli c down id ##connect: nmcli c up id
If your password isn’t automatically recognized type this:
Works like a charm! And, as suspected, the command line responds immediately, while the GUI can be sluggish, uncooperative, and unresponsive sometimes.
Saved my day when I lost my Unity dash and taskbar, and the Network Settings dialog kept on crashing upon any kind of network connection establishment.
@kirill_igum , sorry to be late quiet busy, You may use cron for scheduling wifi scan using iwlist wlan0 scanning or nmcli d wifi list then nmcli to connect to the corresponding wifi network depending on your needs.
On Ubuntu 14.04 this connect command returns Error: Device ‘wlan0’ is not an Wi-Fi device. I believe this answer is incomplete.
To save a connection setup: sudo nmcli dev wifi con «SSID_NAME» password PASSWORD «CONFIG_NAME» To find auto your saved connection setups: nmcli c
nmtui ncurses solution
Great interactive ncurses network manager option:
If for some reason it is not installed, the Debian package is:
sudo apt install network-manager
Comes in the same package as nm-applet (the default top bar icon thing) and nm-cli , and is therefore widely available.
This is a much more intuitive solution than what is above IMO. Unless you’re trying to learn how Linux/Unix connects to WiFi, this seems like the ideal select and press enter solution. Works well on Ubuntu 18
and Jetson Nano . and usefully it kept eth0 up too (I’m running headless and didn’t lose the ssh session)
F***ING THANK YOU! Over the years I’ve had to rescue a laptop several times, and I always have to look up the spells required for wifi. nmtui is the best solution and just works!
If your wi-fi access point is saved, it will auto-connect. Turn wireless on or off with a simpler command:
nmcli nm wifi on nmcli nm wifi off
nmcli radio wifi on nmcli radio wifi off
How do you make NM enable wifi automatically during boot? Every time I reboot, Network Manager starts with wifi disabled.
@Cerin, you may try adding this command to startup applications, see askubuntu.com/questions/243139/…
If you want to connect to a network called PrettyFlyForAWiFi-5G
nmcli -a d wifi connect PrettyFlyForAWiFi-5G
-a (or —ask ) means it will ask you for the password. The connection will be saved and should connect automatically if you restart your computer.
You could append password to the end (the literal word password followed by the actual password)
nmcli d wifi connect PrettyFlyForAWiFi-5G password 12345678
but maybe run unset HISTFILE beforehand, so that your WiFi password isn’t saved to your ~/.bash_history file.
To see all the WiFi networks around you ( —rescan yes is optional, the list of networks might be up to 30 seconds old without it)
nmcli d wifi list --rescan yes
which will output something like
IN-USE SSID MODE CHAN RATE SIGNAL BARS SECURITY PrettyFlyForAWiFi-5G Infra 44 405 Mbit/s 59 ▂▄▆_ WPA2 PrettyFlyForAWiFi Infra 6 195 Mbit/s 41 ▂▄__ WPA1 WPA2
To forget a connection (that was saved after you ran nmcli -a d wifi connect )
nmcli c delete PrettyFlyForAWiFi-5G
To see a list of all saved connections
You can use nmcli connection instead of nmcli c and nmcli device instead of nmcli d
nmcli is the command line interface for NetworkManager (which is part of GNOME, Ubuntu’s default desktop environment) and is already installed on Ubuntu.
Don’t forget to set up your country code for using the perfect regulations:
sudo iw reg set sudo nano /etc/default/crda