Wi-Fi on the Command Line
More people than ever are using wireless networks as their primary networking medium. Great programs are available under X11 that give users a graphical interface to their wireless cards. Both GNOME and KDE include network management utilities, and a desktop-environment-agnostic utility called wicd also offers great functionality. But, what if you aren't running X11 and want to manage your wireless card? I don't cover how to install and activate your card here (for that, take a look at projects like madwifi or ndiswrapper). I assume your card is installed and configured properly, and that it is called wlan0. Most of the utilities mentioned below need to talk directly to your wireless card (or at least the card driver), so they need to be run with root privileges (just remember to use sudo).
The first step is to see what wireless networks are available in your area. A utility called iwlist provides all sorts of information about your wireless environment. To scan your environment for available networks, do the following:
sudo iwlist wlan0 scan
You'll see output resembling:
Cell 01 - Address: 00:11:22:33:44:55 ESSID:"network-essid" Mode:Master Channel:11 Frequency:2.462 GHz (Channel 11) Quality=100/100 Signal level:-47dBm Noise level=-100dBm Encryption key:off . . .
The details (address and essid) have been changed to protect the guilty. Also, the ... represents extra output that may or may not be available, depending on your hardware. You will get a separate cell entry for each access point within your wireless card's range. For each access point, you can find the hardware address, the essid and the channel on which it's operating. Also, you can learn in what mode the access point is operating (whether master or ad hoc). Usually, you will be most interested in the essid and what encryption is being used.
Once you know what's available in your immediate environment, configure your wireless card to use one of these access points using the iwconfig utility to set the parameters for your wireless card. First, set the essid, which identifies the network access point you want:
sudo iwconfig wlan0 essid network-essid
Depending on your card and its driver, you may have the option to set the essid to the special value “any”. In this case, your card will pick the first available access point. This is called promiscuous mode.
You also may need to set the mode to be used by your wireless card. This depends on your network topology. You may have a central access point to which all of the other devices connect, or you may have an ad hoc wireless network, where all of the devices communicate as peers. You may want to have your computer act as an access point. If so, you can set the mode to master using iwconfig. Or, you simply may want to sniff what's happening around you. You can do so by setting the mode to monitor and passively monitor all packets on the frequency to which your card is set. You can set the frequency, or channel, by running:
sudo iwconfig wlan0 freq 2.422G
Or by running:
sudo iwconfig wlan0 channel 3
You can set other parameters, but you should consider doing so only if you have a really good reason. One option is the sensitivity threshold, which defines how sensitive the card is to noise and signal strength, and you can set the behavior of the retry mechanism for the wireless card. You may need to play with this in very noisy environments. Set the maximum number of retries with:
sudo iwconfig wlan0 retry 16
Or, set the maximum lifetime to keep retrying to 300 milliseconds with:
sudo iwconfig wlan0 retry lifetime 300m
In a very noisy environment, you also may need to play with packet fragmentation. If entire packets can't make it from point to point without corruption, your wireless card may have to break down packets into smaller chunks to avoid this. You can tell the card what to use as a maximum fragment size with:
sudo iwconfig wlan0 frag 512
This value can be anything less than the size of a packet. Some cards may not apply these settings changes immediately. In that case, run this command to flush all pending changes to the card and apply them:
sudo iwconfig wlan0 commit
Two other useful commands are iwspy and iwpriv. If your card supports it, you can collect wireless statistics by using:
sudo iwspy wlan0
The second command gives you access to optional parameters for your particular card. iwconfig is used for the generic options available. If you run it without any parameters (sudo iwpriv wlan0), it lists all available options for the card. If no extra options exist, you will see output like this:
wlan0 no private ioctls
To set one of these private options, run:
sudo iwpriv wlan0 private-command [private parameters]
Now that your card is configured and connected to the wireless network, you need to configure your networking options to use it. If you are using DHCP on the network, you simply can run dhclient to query the DHCP server and get your IP address and other network settings. If you want to set these options manually, use the ifconfig command (see the man page for more information).