Automatically start Wifi on Ubuntu Gnu/Linux at startup

My problem, making sure the wifi on a headless linux box connects at boot.

This should work the same on a server install as on a desktop install, with network-manager removed (because it conflicted with this approach).

I haven’t much familiarity with the network manager to speak to that.

So, I had an Ubuntu-Desktop installation that I switched to an Ubuntu-Server installation, and I’ve placed it quite a bit away from the nearest router.

Rather than running cables, I plugged in an old USB wifi dongle. My installation is on a headless machine (no keyboard, no monitor) so getting this right was important as I would have no other way to connect into the box and debug/fix it.

I started off by testing out wpa_supplicant as the router needed a WPA paraphrase.

sudo apt-get install wpa_supplicant

Then, to get the paraphrase into a key, I copy the output from wpa_passphrase into a config file and test :

wpa_passphrase network password

wpa_passphrase network password > wpa-test-config.cfg

Before testing I checked the WPA support and driver info:

hwinfo --netcard

Pay attention to the lines about Driver* and WLAN* – the latter will give you info about what is supported. I didn’t do much with the driver info describing which module controls my card, though that could be useful for debugging.

The WLAN encryption modes my device supported are :

WLAN encryption modes: WEP40 WEP104 TKIP CCMP

which equates to wep 64, wep 128, wpa tkip and wpa aes I think (thanks to http://bernaerts.dyndns.org/linux/230-ubuntu-setup-wifi-commandline )

Now to test:
sudo wpa_supplicant -iwlan0 -cwpa-test-config.cfg -Dwext

I noticed initially that my device was connecting to the node, but kept getting disconnected, and reconnecting. I saw lots of messages like this….


and, after finding lots of possibilities ranging from unsupported hardware to buggy-as-heck drivers, I found that for me this was simply because something called network manager was running. (ubuntu server 12.04, was originally an Ubuntu desktop install that I changed to server mode)

Removing Network Maager

So as this was interfering with my wifi config it had to go; unfortunately I did an

sudo apt-get remove

on that, and then the box lost network connection ! 😦

While I was setting up the box I was internet-connected via a LAN cable, so just borrowing a keyboard/mouse/monitor and adding my LAN port (eth0 for me) to auto network configuration on boot brought me back to a place where I can ssh into the box and continue.

I updated /etc/network/interfaces to :

auto lo
iface lo inet loopback

# added to bring up eth0 (my lan port) and set it up to get an address automatically
# ifconfig can help you find which ethx / wlanx you need to add here.
auto eth0
iface eth0 inet dhcp
Testing again

Now that my network works on LAN without network manager, I retest the wireless connection with wpa_supplicant per above, and it connects fine.
Mannually calling dhclient wlan0 gets me an ip-address, so I’m happy out.

Safely checking out network config changes on remote or headless machines

I update /etc/network/interfaces to include the wlan0 interface in auto start and getting an ipaddress via DHCP, but it also needs the above call to wpa_supplicant to get connected to the wireless base station first.

Having been burnt earlier removing network manager, I backup the known-good version of /etc/network/interfaces and write a script to let me test changes to it temporarily, and then revert to the know good. The script works by backing up the config, applying the test config, and then reverting after a specified timeout.

If the test-changes have worked, I can permanently use that config.

The script :

cat test-network.sh
# first make sure /home/me/network/interfaces.safe is a known-good working config!!
# copy /network/interfaces to /home/me/network/interfaces.test and edit,
# then run this script to test.

# copy over test config and restart nw to test
cp /home/me/network/interfaces.test /etc/network/interfaces
/etc/init.d/networking restart

# let the new config stay for a while for checking
sh -c "sleep 30"

# revert for safety
# replace the live config with the backed-up known-good config and restart nw to activate
cp /home/me/network/interfaces.safe /etc/network/interfaces
/etc/init.d/networking restart

# if it didn't work, edit the test conf and run again
# if it did, consider replacing the known good version(.safe) with the tested-good version(.test)

The working config to auto start Wireless on boot up

My known-good version of the interfaces config, with the now tested-good wireless config, looks like this :

auto lo
iface lo inet loopback

auto eth0
iface eth0 inet dhcp

auto wlan0
iface wlan0 inet dhcp
pre-up wpa_supplicant -B -Dwext -iwlan0 -c/etc/wpa-supplicant.conf

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s