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:
ifconfig -a
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(Buat File wlan0)
Once you have identified your wireless interface card, you need to create a file called
ifcfg-wlan0
in /etc/sysconfig/network-scripts
. 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="ANDROMEDA" CHANNEL=11 MODE=Managed rate=150Mb/s
A few notes:
- HWADDR=00:12:BF:64:A0:E4 # This can be found by issuing the
ifconfig -a
command - 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:
- BOOTPROTO=static
- DHCPCLASS=
- 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(Buat file password WIFI)
Setting a WEP key is the part that took me the most time to figure out. You need to create a file called
keys-wlan0
in /etc/sysconfig/network-scripts
. Inside that file, put your WEP key like this:WPA_PSK="Gr34t$Peopl3"
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 ifconfig
command.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.
Final thoughts
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!
To run the
ifconfig
... command , you should install the net-tools
package.
Because the
net-tools
is deprecated there is the ip
and iw
commands which answer your question:How to turn WiFi on/off?
$ ip link set <interface> up
$ ip link set <interface> down
How to get list of available WiFi connections?
$ iw dev <interface> scan | grep SSID
How to connect WiFi that i want to connect with?
Create a
wpa_supplicant
configuration file with the following content:ctrl_interface=/run/wpa_supplicant
update_config=1
ap_scan=1
To add the SSID and the password, run:
$ wpa_passphrase "YOUR-SSID" YOUR-PASSWD >> /etc/wpa_supplicant/wpa_supplicant.conf
To connect run:
$ wpa_supplicant -i <interface> -c /etc/wpa_supplicant/wpa_supplicant.conf -B
dhclient <interface>
- +1 for mentioning net-tools deprecation. – Isius Aug 27 at 15:24
2
You should be able to see if the drivers for the card are installed by using
lspci -k
And then to know if your interface is correctly detected and named you can use
ip link
You can then try to bring up the interface with ifup
If the interface does not come up correctly it might be because you're missing a configuration file like
/etc/sysconfig/network-scripts/ifcfg-<interface-name>
Another alternative solution if it's something you only want to bring up once you can input the following commands :
ip link set dev <interface-name> up
wpa_supplicant -B -i <interface-name> -c /etc/wpa_supplicant/wpa_supplicant.conf
dhcpcd <interface-name>
1
If you are running CentOS 7: You may want to check if NetworkManager is already there available for you to use:
chkconfig NetworkManager on
service NetworkManager start
Issue command: nmtui
Go directly to "Active a connection" to check if you already can see your wireless network/wi-fi. If so, input your password.
You can also Edit a profile for the wi-fi you want to connect.
NOTE: if you cannot active a wi-fi, try the following: After above steps: /etc/init.d/network restart OR REBOOT/RESTART your box.
0
Here is my experience, for the following reasons:
- If you got connected to Wifi network when you have installed CentOS 7.
- After CentOS 7 entering into sleep mode OR accidental shutdown due to power drainage OR you have restarted & logged into CentOS 7
and if your wifi not working.
Don't install extra kernel module/do any tweaks/install any new packages
Just shutdown and boot again in CentOS 7. Your wifi will start working again.
This is what I realised after 6 hours of tweaks and tried everything out.