146

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?

1

9 Answers 9

135

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).

Example:

network={
    ssid="ssid_name"
    psk="password"
}

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.
11
  • 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? Oct 10, 2013 at 22:58
  • 1
    This solution gives me the error "wlan0: Unsupported driver 'ext'".
    – Cerin
    Jul 25, 2015 at 21:05
  • 1
    @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<driver> for your specific hardware. For me, it ended up being "nl80211".
    – Cerin
    Jul 26, 2015 at 14:37
  • 1
    I just completely omit the -D option and it works! Thanks! :)
    – Ray
    Jun 29, 2017 at 22:25
  • 1
    Does the command 'sudo dhclient wlan0' return? By me it blocks. Is it ok?
    – ka3ak
    Jul 3, 2017 at 17:01
90

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

Or (for Fedora):

# 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:

network={
    ssid="myrouter"
    #psk="mypassphrase"
    psk=8ada1f8dbea59704ac379538b4d9191f6a72390581b4cd7a72864cea685b1a7f
}

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:

$ iwlist scan

You might have to run that as root to force a refresh.

Next, you will need to know three pieces of information:

  1. 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.
  2. 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.
  3. 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):

# dhclient -r

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):

# dhclient eth1

You now have an IP, in theory at least. Happy surfing!

7
  • 1
    FYI for unexperienced people, most wireless card interface names will be wlan<number>, so for example the first wireless card in your configuration will be wlan0 instead of eth1
    – xorinzor
    Dec 24, 2013 at 16:31
  • Thanks, the wpa_passphrase command helped. I tried the accepted answer but the file was wrong, wpa_passphrase helped with that and then I just passed the second command from the accepted answer(I already knew for sure that it is called wlan0)... May 15, 2014 at 20:52
  • thanks that's was really great, can u give me link cover how setup static and routing table ... :)
    – Yassine
    Jun 30, 2015 at 20:22
  • 1
    @AlexandreSchmidt +1 for being comprehensive! Aug 15, 2016 at 11:54
  • 1
    @JohnStrood strictly you should thank whoever wrote the website since it's a copy from there
    – icc97
    Nov 13, 2018 at 9:39
15

Debian and other distros have wpa_suplicant running as a service by default in order to manage the wifi networks. wpa_suplicant can be handled by different clients/front-ends such as the network manager GUI. This is better explained in this debian wiki.

wpa_cli is the command line wpa_suplicant client to manage the wifi networks.

Edit: I have just found this post explaining how to use nmcli and it is much better than wpa_cli since it is compatible with the GUI Network Manager and their settings and saved wifi networks.

Example of use of wpa_cli:

Check that I already have a wifi enabled network interface:

# iwconfig
wlan0     IEEE 802.11bgn  ESSID:off/any  
          Mode:Managed  Access Point: Not-Associated   Tx-Power=22 dBm   
          Retry short limit:7   RTS thr:off   Fragment thr:off
          Encryption key:off
          Power Management:on

Check if wpa_suplicant process is running:

# ps -e | grep wpa
1881 ?        00:00:07 wpa_supplicant

Enter in wpa client interactive mode:

# wpa_cli

List available access points:

> scan
> scan_results

... and you get something like this:

bssid / frequency / signal level / flags / ssid
e0:60:66:7c:81:7f       2417    -66     [WPA2-PSK-CCMP][ESS]    vodafone817E
e0:60:66:61:83:4b       2452    -76     [WPA2-PSK-CCMP][WPS][ESS]       vodafone834A
f8:8e:85:c5:65:c2       2462    -76     [WPA-PSK-CCMP+TKIP][WPS][ESS]   MOVISTAR_65C1
a8:d3:f7:46:0c:be       2472    -83     [WPA-PSK-CCMP+TKIP][WPA2-PSK-CCMP+TKIP][WPS][ESS]       Orange-0CBC
...

Add your AP:

> add_network
> set_network 0 ssid "vodafone817E"
> set_network 0 psk "my-pass-phrase"

Select it as current:

> enable_network 0

Connect to it:

> reconnect

Check the status:

> status

Exit wpa_cli:

> quit

From the shell, request DHCP for an IP and net settings:

# dhclient -r
# dhclient wlan0
2
  • Why you talk about wpa_cli and then you write wpa_client? On Kubuntu 18.10 wpa_client seems to don't exist as a command Dec 22, 2018 at 15:13
  • You are right... fixed.
    – David
    Dec 26, 2018 at 19:39
3

All the answers suggesting wpa_supplicant are wrong. Yes, it may connect you to the WPA network, but that's a bad solution in the long run because it'll be very difficult to maintain and won't play nicely with a wired connection. Read this answer and make your life easier by using Network Manager from the command line. I wasted hours trying to configure wpa_supplicant, then tried nmcli and it "just worked".

1
  • 4
    The problem with things the 'just work' is that they can easily 'just not work' and then where are you. For CLI people, the direct wpa_supplicant approach is better because it exposes all its parts. The great virtue of NM is that all its parts are hidden away. So it is wrong to say that the wpa_supplicant approach is wrong. You like things that 'just work', fine. Your preference is your right, but it is impolite to tell people they are wrong just because their preference is not yours. This is why I down-ticked your answer. Mar 2, 2018 at 16:52
2

There's actually a way to do it using NetworkManager, if you have the checkbox package installed.

sudo /usr/share/checkbox/create_connection SSID --security=wpa -key=WPA-KEY

(Credit: bug 923836, which came up in my searches on the topic.)

2

You can use VSWM - Very Simple Wireless Manager. You place your known APs on a simple cfg file (/etc/vswm.cfg) and when you run vswm it scan the networks available and connect your card with the first that appears on /etc/vswm.cfg.

VSWM is available at https://github.com/dmelo/vswm . Here is a pick of how the vswm.cfg config file looks like:

[global]

dev = wlan0
dns = ["4.2.2.1","8.8.8.8"]

[wlan0-0]

ssid = Network1
psk = netpassword
protocol = WPA
net = dhcp

[wlan0-1]

ssid = Network2
protocol = WEP
net = static
address = 192.168.0.15/24

Under the hoods, it uses the standard command line tools: iwlist, wpa_supplicant, iwconfig, dhclient... Only it automates the job.

2

The nmcli is the command line version of the default network manager app on Ubuntu.

Some of the advantages are that it uses the configuration you already have from the graphical interface and it doesn't need root access.

You can run nmcli to show the available options.

Example to connect to a wifi named MYESSID:

    nmcli c up MYESSID   
1
  • 1
    nmcli, nm-applet, and yeah your answer is the best but I don't know why no one vote it
    – SdSaati
    Aug 29, 2019 at 19:05
1

First brig your card up if it's not runnig:

ifconfig wlan0 up

Set the parameters acording to your network

iwlist wlan0 scan
iwconfig wlan0 essid NETWORK_ID key WIRELESS_KEY
6
  • 16
    I believe that this is for WEP only. No WPA ;)
    – Rinzwind
    May 17, 2012 at 15:57
  • Rinzwind is right. This is for WEP. Sorry for te mistake.
    – LnxSlck
    May 17, 2012 at 15:59
  • 2
    no problem. Leave it here and someone will someday benefit from your answer ;)
    – Rinzwind
    May 17, 2012 at 16:00
  • will this work even without a key like an open network? Oct 26, 2012 at 18:44
  • 1
    I hope no one ever benefits from a WEP specific answer :'(
    – bot47
    Mar 28, 2016 at 15:21
1

I had trouble using wpa_supplicant directly to get on a wireless network. My network ID and password generated errors when I used the wpa_supplicant.conf file on the wpa_supplicant command line, e.g.

sudo wpa_supplicant -iwlan0 -c/etc/wpa_supplicant.conf -B

I was able to work around it with wpa_cli. The command sequence I had to use, since I am using a Windows driver with Linux ndiswrapper, is:

sudo modprobe ndiswrapper
sudo iwconfig wlan0 essid "<My Network ID>" mode managed
sudo wpa_cli identity "<My Network ID>" password "<My password>"

I had the network set up by adding it in the wireless section of networking in the GUI, so that I could click on the wired or wireless network icon and select the wireless provider (which was sometimes necessary to complete the login).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .