How to manage a WiFi connection from the command line

Whenever you install a new Linux distribution on a computer, it is in general recommended that you connect to the internet via a wired connection. There are two main reasons for this: one, your wireless adapter may not have the right driver loaded; second, if you are installing from the command line, managing WiFi is scary. I always tried to avoid dealing with WiFi over the command line. But in the Linux world, there is no place for fear. If you do not know how to do something, that is the only reason you need to go ahead and learn it. So I forced myself to learn how to manage a WiFi connection from the command line on Linux.

There are of course multiple ways to connect to a WiFi from the command line. But for the sake of this post, and as an advice, I will try to use the most basic way: the one that uses programs and utilities included in the "default packages" of any distribution. Or at least I will try. An obvious reason for this choice is that the process can potentially be reproduced on any Linux computer. The downside is its relative complexity.

First, I will assume that you have the correct drivers loaded for your wireless LAN card. There is no way to start anything without that. And if you don't, you should take a look at the Wiki and documentation for your distribution.

Then you can check which interface supports wireless connections with the command

$ iwconfig

In general, the wireless interface is called wlan0. There are of course exceptions, but for the rest of this tutorial, I will call it that way.

Just in case, you should make sure that the interface is up with:

$ sudo ip link set wlan0 up

Once you know that your interface is operational, you should scan for nearby wireless networks with:

$ sudo iw dev wlan0 scan | less

From the output, you can extract the name of the network (its SSID), its signal power, and which type of security it uses (e.g., WEP, WPA/WPA2). From there, the road splits into two: the nice and easy, and the slightly more complicated case.

If the network you want to connect to is not encrypted, you can connect straight to it with:

$ sudo iw dev wlan0 connect [network SSID]

If the network uses WEP encryption, it is also quite easy:

$ sudo iw dev wlan0 connect [network SSID] key 0:[WEP key]

But everything gets worse if the network uses WPA or WPA2 protocols. In this case, you have to use the utility called wpa_supplicant, which is not always included by default. You then have to modify the file at /etc/wpa_supplicant/wpa_supplicant.conf to add the lines:

network={
	ssid="[network ssid]"
	psk="[the passphrase]"
	priority=1
}

I recommend that you append it at the end of the file, and make sure that the other configurations are commented out. Be careful that both the ssid and the passphrase are case sensitive. You can also technically put the name of the access point as the ssid, and wpa_supplicant will replace it with the proper ssid.

Once the configuration file is completed, launch this command in the background:

$ sudo wpa_supplicant -i wlan0 -c /etc/wpa_supplicant/wpa_supplicant.conf

Finally, whether you connected to an open or a secure network, you have to get an IP address. Simply use:

$ sudo dhcpcd wlan0

If everything goes well, you should get a brand new local IP via DHCP, and the process will fork in the background. If you want to be sure that you are connected, you can always check again with:

$ iwconfig

To conclude, I think that getting over the first step is completely worth it. You never know when your GUI will be down, or when you cannot access a wired connection, so getting ready now seems very important. Also, as mentioned before, there are a lot of ways (e.g., NetworkManager, wicd, netcfg, wifi) to manage a wireless connection. If I try to stick to the most basic way, I know that in some cases, the utilities that I used may not even be available to you, and that you would have to download them prior to that. On the other side of the balance, there are some more advanced programs, which are definitely not included in the "default packages," which will greatly simplify the whole process. But as a general advice, it is good to stick to the basics at first.

What other ways would you recommend to connect via WiFi from the command line? Please let us know in the comments.

Subscribe to Xmodulo

Do you want to receive Linux FAQs, detailed tutorials and tips published at Xmodulo? Enter your email address below, and we will deliver our Linux posts straight to your email box, for free. Delivery powered by Google Feedburner.

The following two tabs change content below.

Adrien Brochard

I am a Linux aficionado from France. After trying multiple distributions, I finally settled for Archlinux. But I am always trying to improve my system by stacking up tips and tricks.

Latest posts by Adrien Brochard (see all)

7 thoughts on “How to manage a WiFi connection from the command line

  1. well dan, i had this problem with kubuntu, i understand Mageia has a similar problem.. cannot support hidden wireless! The workaround is
    1. cmd line
    sudo iwconfig

    you'll see a list of interfaces including 1 which supports a wireless extension. eg wlan0
    2. create a wireless interface. You can use GUI if you want. Call according to the output of iwconfig eg wlan0. I suppose you know the ssid & "codeword" of the base station, so configure it accordingly.
    3. go back to cmd line & say
    sudo iwlist wlan0

  2. Disappointed with all of the flavors of Linux 2014! I downloaded 15 different versions of Linux, during the last week, and not one will connect to my Belkin N600DB router with a Belkin N600dDB USB Wi-Fi? Many of the earlier versions of Linux Distro's usually would connect. I stopped using earlier versions of Fedora b/c the new version wouldn't recognize my Wi-Fi when the old version did? I switched to Mint b/c it connected to my Wi-Fi but now it does not! Why would the vendors change the program that will not automatically connected to Wi-Fi; so I may just add Sid and PW. What happened during 2012-2-14 so that our vendors changed the program that connected to Wi-Fi to one that now doesn't? It is very hard to suggest Linux to friends that are hooked on Windows when, out of the box, it cannot do basic network connections! If you mention Terminal I'll scream! Windows, out of the box, can see my Wi-Fi! I could go on and on about this issue and I'm bound to get hate emails but I say this: It worked with your old version of Linux but not your new version? What the hell were you vendors thinking!!!

    Charles

  3. @C Patterson: Vendors? You are still thinking in Windoze. Linux is free software with unpaid developers who mostly develop for their own use. Things change and occasionally regress. Never used Belkin so I am not sure if they are forthcoming with their wifi driver info...some are still locked on to windows drivers and lock the linux devs out.

    Can't imagine why anyone would want to go through all the manual command line glop when Ceni is available...perhaps not on Arch though as it is primarily developed for Debian and derivatives. It is easy to download or preloaded for the sid distros but even the buntu's can load the immensely useful "smxi" (smxi.org) and add Ceni without having to search for a repository for it. Ceni does it all...ethernet, wifi, security, etc. 'Course if you are seriously command-line adverse, sorry.

  4. Interesting piece - I'm gradually working through terminal fears. I've recently solved a headscratcher with miserable performance of a newly-bought 11n wifi adapter based on the Realtek 8192cu driver, that might be useful to others running current versions of Ubuntu/Mint. The problem is that even Realtek's own official driver does not run correctly on Ubuntu and spin-offs. This ported driver fixes it perfectly:

    https://github.com/pvaret/rtl8192cu-fixes

    As the writer says above, if you don't have the right driver installed, you're defeated before you begin. This shoe might fit the Belkin owner who posted above.

  5. I am currently running Debian Testing (bill@debianLaptop:~$ uname -a
    Linux debianLaptop 3.12-1-686-pae #1 SMP Debian 3.12.6-2 (2013-12-29) i686 GNU/Linux).
    I have tried to follow the cli implementation to connect to a wpa encrypted wifi. My system does not have the file wpa_supplicant.conf. It has:
    bill@debianLaptop:/etc/wpa_supplicant$ ls -l
    total 40
    -rwxr-xr-x 1 root root 945 Nov 7 2012 action_wpa.sh
    -rwxr-xr-x 1 root root 25920 Feb 20 20:25 functions.sh
    -rwxr-xr-x 1 root root 4696 Nov 7 2012 ifupdown.sh
    bill@debianLaptop:/etc/wpa_supplicant$

    Which file should I modify in order to receive the password request so I can log onto the wifi? I know the name of the system and its password--just don't know how to get a wifi conncetion running on my debian
    laptop.

    any thoughts?

    Bill

Leave a comment

Your email address will not be published. Required fields are marked *

Current ye@r *