Connect wireless (wifi) via command line (cli) on boot – Ubuntu 14.04 onwards

Needed my home base server to connect to wireless / wifi on boot. Spent quite sometime trying to get it working. Apparently old methods is no longer necessary. Newer Ubuntu from 14.04 onwards just require you to set in /etc/network/interfaces and no longer require you to use traditional methods using wpa_supplicant, etc.

Here is how my /etc/network/interfaces looks like with static IP.

 

Below are the old methods for the sake of reference. Including the links found on related topics for troubleshooting references.

 

Old Methods for Ubuntu lower than 14.04

References:

How To Configure Wireless / WiFi Networking in Ubuntu via the Command Line (CLI)

https://www.linux.com/learn/how-configure-wireless-any-linux-desktop

https://unix.stackexchange.com/questions/97828/driver-d-option-under-wpa-supplicant

https://unix.stackexchange.com/questions/116914/what-driver-is-being-used-by-a-wireless-usb-adapter

https://askubuntu.com/questions/573541/assign-driver-with-wpa-supplicant/588522

https://askubuntu.com/questions/1277/how-do-i-configure-wifi-to-log-in-to-wpa-at-boot-time-regardless-of-user-being

How To Configure Wireless / WiFi Networking in Ubuntu via the Command Line (CLI)

There are a number of tutorials available on-line for sorting out WiFi in Ubuntu via the CLI, but most of them seem quite outdated, so I decided to do my own.

I did this on a minimal install of Ubuntu Lucid, so it is as up-to-date as possible. The PC I was using has no Windows Manager of Graphical Display Manager, just the good old terminal so all this is done via the CLI only. I did this using a USB WiFI dongle, but it should be the same whether you use an internal card or a USB card.

First, you need to install the relevant software. You need to have a wired connection at this point, otherwise this wont work.

If you are connecting to an open network, you wont need wpasupplicant. Next, you need to “bring up” (essentially this means activate) your WiFi interface. So, issue:

Next, to make sure your wireless device is working as it should issue:

and then

This should show you some wireless networks as proof that the WiFi device is working, if something goes wrong here, then there is a problem with your device or driver and you need to get googling.

If you are accessing a secured network and you really should be, you need to access the correct version of your WiFi key. To get your key, issue this command:

This will result in something that looks like this:

You need to make a note of the long phrase after psk= (NOT #psk=) as this your WiFi password in hex format.

Next, you need to edit your interfaces file, so issue:

At the end of this file, you need to add your WiFi configuration. Here are the options you can add.

auto wlan0     #change this to the name of your WiFi interface
iface wlan0 inet dhcp     #this is normally fine, if you want a static IP address replace “dhcp” with “static”
netmask 255.255.255.0     #change this as appropriate for your network, this value is usually right
gateway 192.168.1.1     #change this as appropriate for your network
address 192.168.1.100     #only needed for a static IP address
dns-nameservers 192.168.1.1     #only needed for a static IP address
wpa-driver wext     #you shouldn’t need to change this
wpa-ssid YOURSSID     #just type the name of your SSID here
wpa-ap-scan 1     #if the name of your SSID is hidden usually, type 2 instead of 1
wpa-proto WPA    #if you use WPA1 type WPA, if you use WPA2 type RSN
wpa-pairwise CCMP     #if you use AES type CCMP, if you use TKIP type TKIP
wpa-group CCMP     #if you use AES type CCMP, if you use TKIP type TKIP
wpa-key-mgmt WPA-PSK     #usually WPA-PSK (if you share a key) but sometimes WPA-EAP (for enterprises)
wpa-psk YOURHEXKEYFROMABOVE     #the hex key that you generated earlier

Thus, since I am using a WiFi card that is identified as wlan0 and am connecting to a WPA1 AES encrypted network called MYPLACE that isn’t hidden without a static IP address, this is what I added:

Finally, comment out the top section so it looks like this:

This stops your wired network from working. This helps to ensure there are no conflicts. Remember, if you want your wired network to work again, remove these two comments (the #).

Finally, save the file by pressing CTRL and X and then pressing Y to save to the file. Now, reboot and your network should come up. Yay!

Some people have found that this doesn’t always work, so the next thing to do is to edit the configuration file for the wpasupplicant program. Do this by issuing:

Basically, you add pretty much the same information here as you did to the interfaces file, except without the wpa- part. So, my file looks like this:

Command Line

Believe it or not, this isn’t as challenging as it might seem. I will demonstrate how this is done on a Ubuntu machine. For other distributions you might have to alter the location of scripts or the name.

What you will need, in order to be able to establish this connection, are the following:

  • ifconfig: Enable your wireless device.
  • iwlist: List the available wireless access points.
  • iwconfig: Configure your wireless connection.
  • dhclient: Get your IP Address via dhcp.
  • wpa_supplicant: For use with WPA authentication.

Make sure you have all of the above tools onÂ¬â  your computer before you continue. To test for this tools you can, from within your terminal window, issue the commands:

  • which wpa_supplicant

You should see the path where each tool is installed. If you receive an error that a command is not installed you will need to install it. This should not be the case, since these are standard tools that are required for wireless networking.

Let’s take a look at how this is done when you are connecting to a non-WPA authentication-based wireless network.

Now that you have confirmed they are installed start off with the command:

Where

is the name of your wireless device (this is most often the default). The above command will bring your wireless device up so it is ready to use. The next phase is to scan for any wireless access points with the command:

From the output of the scan you should see a line (or lines) like:

Where NETWORK_NAME is the name of an available wireless network.

Now that you have your network name (and you know it’s available) you can connect to that network with the command:

Where

is the name of the network you want to connect to and

is the security key for that network. NOTE: The

command defaults to HEX values for wireless keys. If you need to use ascii you have to prepend the “s” prefix to your key like so:

With your connection made, you now have to get an IP address for your machine using the dhclient command like:

Simplify the Process

Naturally you do not want to have to issue all of those command in order to bring up a wireless network. You can make this a lot easier by creating a script to handle the task. A possible script might look like this:

Where

and

are unique to the network you are connecting to. Save that file with the name

and give it executable permissions with the command

and you are ready to use that file to bring up your wireless. You can even move that file to

so the command is global. All you would have to do to bring up your wireless connection is issue the command

and you’re ready to go.

WPA

For WPA-based networks you will need to take a different approach

 



Comments

comments

Leave a Reply