Sunday, June 9, 2013

Belajar Linux: How to Connect to Wi-Fi from Command Line

We will talk about how to connect to Non-Secured Networks, as well as WEP and WPA protected networks. This tutorial will not cover how to install the drivers for your wireless card. Most old cards will be automatically detected by your system, but in the unfortunate event that it won’t be detected, you will have to do a lot of digging.

To follow this tutorial you need the following packages already installed on your system:

  1. iwconfig

  2. iwlist

  3. wpa_passphrase


In Arch Linux, these are all part of the wireless_tools pack (if it’s of any help… probably not… unless you use Arch)

With that said, let’s get started, these commands need to be run as root, or with root privileges using the sudo command.

Use this line to check if your OS has loaded the card driver:
lspci | grep -i net

Now, run this command to make sure the driver has created an interface for the kernel to use:
iwconfig

If you only have one wireless card, it will probably be named “wlan0″. First of all we need to turn it on, so we will do just that:
ip link set wlan0 up

Now, connecting to any wireless network requires 2 things. The name of the network (a.k.a. the ESSID) and the password if it’s a password protected network (like WPA or WEP). If you don’t know the ESSID of your network, you can issue the following to get a list of all the networks in range.
iwlist wlan0 scan

Now, the following instructions are for networks with WPA security ONLY. If you have WEP security or No Security at all (a.k.a. No Password), skip these steps. WPA security requires that the ESSID and the password be stored in a file  on your system, this file is called “wpa_supplicant.conf”. First and foremost we will make a backup of the old file in case we break something:
 mv /etc/wpa_supplicant.conf /etc/wpa_supplicant.conf.original

And then we create a new file to match our needs:
wpa_passphrase network_name “secret_passkey” > /etc/wpa_supplicant.conf

The file stores everything in plain text format, therfore it’s a good idea to make this file readable only from the root account. To do this run the following command.
chmod 0600 /etc/wpa_supplicant.conf

Everybody follow along from this point. We will connect the computer to the wireless router. Use the command in the table below that best suits your situation:























EncryptionCommand
No Encryptioniwconfig wlan0 essid "linksys"
WEP w/ Hex Keyiwconfig wlan0 essid "linksys" key "0241baf34c"
WEP w/ ASCII passphraseiwconfig wlan0 essid "linksys" key "s:pass1"
WPAwpa_supplicant -B -Dwext -i wlan0 -c /etc/wpa_supplicant.conf

WARNING! In my situation, not sure why, it was necessary to run the same command a second time to properly connect to the router, otherwise it wouldn’t work. So the way I do it is connect to the router, wait 3 seconds, than do the same thing again and it all works fine. To test if you are connected to the router just issue:
iwconfig wlan0

Now, in most cases wirless access points also double as DHCP servers so the following command will allow your system to request an IP address from the router and set itself up with the received information:
dhcpcd wlan0

If you are one of the rare cases in which you have to assign your own ip address, this line will do the trick nicely:
ifconfig wlan0 192.168.9.100 netmask 255.255.255.0

Now, just run a ping to test if the connection is working:
ping -c 3 www.google.com

And that should be it! Ready to surf. If anyone runs into any problems or has a better way of doing this don’t be hateful, be helpful instead and lave a comment with your solution. That’s what the Linux community is all about isn’t it?

0 comments:

Post a Comment