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 Wi-Fi from Linux Terminal Using Nmcli Command
There are several command-line tools for managing a wireless network interface in Linux systems. A number of these can be used to simply view the wireless network interface status (whether it is up or down, or if it is connected to any network), such as iw, iwlist, ip, ifconfig and others.
And some are used to connect to a wireless network, and these include: nmcli, is a command-line tool used to create, show, edit, delete, enable, and disable network connections, as well as control and display network device status.
First start by checking the name of your network device using the following command. From the output of this command, the device name/interface is wlp1s0 as shown.
$ iw dev phy#0 Interface wlp1s0 ifindex 3 wdev 0x1 addr 38:b1:db:7c:78:c7 type managed
Next, check the Wi-Fi device connection status using the following command.
iw wlp2s0 link Not connected.
From the output above the device is not connected to any network, run the following command to scan available Wi-Fi networks.
sudo iw wlp2s0 scan command failed: Network is down (-100)
Considering the output of the above command, the network device/interface is DOWN, you can turn it On (UP) with the ip command as shown.
$ sudo ip link set wlp1s0 up
If you get the following error, that means your Wifi is hard blocked on Laptop or Computer.
RTNETLINK answers: Operation not possible due to RF-kill
To remove or unblock you need to run the following command to solve the error.
$ echo "blacklist hp_wmi" | sudo tee /etc/modprobe.d/hp.conf $ sudo rfkill unblock all
Then try to turn ON the network device once more, and it should work this time around.
$ sudo ip link set wlp1s0 up
If you know the ESSID of the Wi-Fi network you wish to connect to, move to the next step, otherwise issue the command below to scan available Wi-Fi networks again.
And lastly, connect to the wi-fi network using following command, where Hackernet (Wi-Fi network SSID) and localhost22 (password/pre-shared key).
$ nmcli dev wifi connect Hackernet password localhost22
Once connected, verify your connectivity by doing a ping to an external machine and analyze the output of the ping as shown.
$ ping 8.8.8.8 PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data. 64 bytes from 8.8.8.8: icmp_seq=1 ttl=48 time=61.7 ms 64 bytes from 8.8.8.8: icmp_seq=2 ttl=48 time=61.5 ms 64 bytes from 8.8.8.8: icmp_seq=3 ttl=48 time=61.6 ms 64 bytes from 8.8.8.8: icmp_seq=4 ttl=48 time=61.3 ms 64 bytes from 8.8.8.8: icmp_seq=5 ttl=48 time=63.9 ms ^C --- 8.8.8.8 ping statistics --- 5 packets transmitted, 5 received, 0% packet loss, time 4006ms rtt min/avg/max/mdev = 61.338/62.047/63.928/0.950 ms
That’s It! I hope this article helped you to setup your Wi-Fi network from the Linux command line. As always, if you found this article useful, share your thoughts in the comment section below.
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!