If you're like me, you may have an old desktop computer sitting around your home office somewhere serving as a Linux machine. I use my old desktop as a Centos server acting as an exact copy of my production machine. That way I'm able to easily try out updates to web apps I'm creating before deploying them to an online production server.
I don't have a UTP-connection where I work at the moment so I had to find a way to connect the Linux server to my Wifi network. There's a lot of information floating around the internet, but sometimes when it comes to Linux, it's hard to find that right piece of information for a specific use case. I spent about an afternoon researching and figuring out how to set up my wifi connection reliably on my Linux box.
I hope this article saves you those hours. A lot of credit goes to Paul, who wrote this fine tutorial that helped me a lot in understanding how CentOS connects to wireless networks. I used the information from his article to get started but found that it missed some key information for connecting to a wireless network with hidden SSID.
On top of that, the article didn't provide a reliable way to enter a WEP key and to remember the key on reboot. This article may be well suited for people in the same situation as me, wanting to connect to a Wifi network with a hidden SSID and a WEP key. I tested the following configuration on CentOS 6.3 but it will probably work on other versions as well.
Detect your wireless card
Issue the following command to get started:
This will show all your network cards, even the ones that are down. If you don't see "wlan0" then there is no wireless card detected. You'll need to look for drivers first in order to continue. If your device is detected, you should see a network interface named "wlan0". My cheap Medion system didn't need additional drivers.
Build a network card configuration file
Once you have identified your wireless interface card, you need to create a file called
/etc/sysconfig/network. The part in the filename that says "wlan0" needs to correspond with the name of your wireless interface card, usually this will be "wlan0". If your router has DHCP set up and you want it to give your server an available IP address, you can use the following configuration:
TYPE=Wireless DEVICE=wlan0 BOOTPROTO=dhcp BROADCAST=192.168.0.255 HWADDR=00:12:BF:64:A0:E4 NETMASK=255.255.255.0 ONBOOT=yes ONHOTPLUG=yes PEERDNS=no USERCTL=yes IPV6INIT=no ESSID=yourssid CHANNEL=11 MODE=Managed rate=150Mb/s
A few notes:
- HWADDR=00:12:BF:64:A0:E4 # This can be found by issuing the
- ONBOOT=yes # this will start your connection after booting
- ESSID=yourssid # this is the SSID name of your wifi network
- CHANNEL=11 # the channel can be found in your router configuration
I personally like the convenience of having a fixed IP, so that I can always connect to my server by using the same IP or by setting a hostname for it. If you want to use a static IP like me, you can adjust the configuration mentioned above with the following settings:
- IPADDR=192.168.1.9 # pick an IP in a valid range
- GATEWAY=192.168.1.1 # this is your router IP address
Of course, keep in mind that your router may have other IP ranges set than the defaults I used above. Additional information about all available parameters you can use in the Centos network scripts can be found here.
Setting a WEP key
Setting a WEP key is the part that took me the most time to figure out. You need to create a file called
/etc/sysconfig/network. Inside that file, put your WEP key like this:
That file will be read when your WLAN0 network interface comes online. Now reboot your system or restart your network service by executing the
service network restart command. This should give you an IP adress. Check to see if that worked by checking the output of the
Additional testing with wireless-tools
To make troubleshooting easier, you can install the "wireless-tools" package. This makes it a bit easier to see whether you network interface actually works or not. You might face the problem of not having an active internet connection yet. Of course, you won't be able to install packages through yum without an internet connection. One way to work around this is to connect your server directly to your router with a UTP cable. Alternatively, what I did was connect the CentOS server over UTP with my Mac. You can then enable internet connection sharing on your mac (or Windows) system to be able to install those two packages.
Installing the wireless-tools
If you didn't do so already, install the necessary tools we talked about above by issuing the install command. Do this as root or use the sudo command.
yum install wireless-tools
You can now use the following command to see whether your Wifi card detects any networks around you:
iwlist wlan0 scan
That command will display all networks your Wifi card can see. If you can't see your Wifi network because its SSID is hidden, you can try to add it manually by executing the following command:
iwconfig wlan0 essid yourssid key "YOURWEPKEY"
Replace "yourssid" and "YOURWEBKEY" with your own SSID and WEP key. Make sure you put the WEP key between quotes, so that the command knows it's a string.
If all goes well, your CentOS server will now connect to your wireless network. The only problem I was still facing, was that my wireless connection appeared to be dropped after some time of inactivity. The only "workaround" for this problem I found so far is to issue a PING command to e.g. google.com. That seems to keep the Wifi adaptor alive but it's not really a clean solution. You can put the ping command in the file called
/etc/rc.local which will be called after booting. Add the following command at the end of that file:
ping www.google.com >> /dev/null &
This will ping google and send the output to /dev/null which means it will be discarded. The ampersand just tells the command to run it in background.
If anyone has a better idea to resolve that issue, please share it in the comments. UPDATE: after updating to Centos 6.4, I started getting messages like:
phy0 - rt2500usb_set_device_state: Error - Device failed to enter state 3 (-16). phy0 - rt2x00lib_autowakeup: Error - device failed to wakeup.
This can be solved by entering the following command, which basically turns of power management for your wifi card.
iwconfig wlan0 power off
Add that command to your
/etc/rc.local file and you should be all set. I hope this saves you all some time. Good luck getting your Centos machine up and running on that Wifi connection!