Vous êtes sur la page 1sur 6

Using wireless-tools to connect to wireless internet from the command line

by RogueClown adalia@weaknetlabs.com

ABSTRACT Sooner or later, it is going to happen. Maybe you will be on a computer that does not have a graphical interface installed for the wireless connection. Maybe the graphical interface on the computer you're using has become corrupted or somehow unusable, and you need to get on the internet to figure out how to troubleshoot the problem. Maybe you have an computer with so few system resources that you'd like to avoid installing a GUI at all, or install as few graphical programs as possible. Maybe you are just curious. Whatever your reasons for needing to control your wireless connection from the command line, the good news is that doing it is very easy, and requires learning only a few simple commands. The hardest thing about it is finding all of the information you need in one place, in a form that doesn't assume that you already know how to do it. Before we begin, note that almost all of the necessary commands require root or superuser privileges. Make sure that you have these privileges, at least with respect to networking capabilities. Otherwise, your computer will not recognize the commands, and you will not be able to configure your wireless connection. If you do not have root privileges or wireless networking superuser privileges, consult your system administrator.

Making sure you have wireless-tools installed This tutorial covers basic wireless connection management using wireless-tools, a suite of programs that comes with most Linux distributions. If you are using a Debian-based distribution1, you can check to see if you have the most current version of wireless-tools by opening up a terminal and typing the command: dpkg -l | grep wireless-tools This command looks at the list of all of the packages installed on your computer, pipes the list into grep, and lets it search for wireless-tools. If it does exist on your computer, it returns a line of text containing it, looking something like this: ii wireless-tools Wireless Extens 29-1ubuntu2 Tools for manipulating Linux

This is from an HP Pavilion laptop running Ubuntu 8.04; don't be discouraged if it looks a little different. What matters is off to the left of the line of text: that it says that wireless-tools is installed on your machine. Odds are that it is installed already on your system, and you can skip to the next section and start learning how to use it. However, if it is not, and your computer is currently connected to the internet, install it by typing: sudo apt-get install wireless-tools If you are not using a Debian-based distribution, check your distribution's documentation to verify how to check for an installed program, and to verify whether your distribution has an automated package management system through which you can install wireless-tools. If your distribution does not have an automated package manager, and your computer does not have wireless-tools installed, download the suite at: http://www.hpl.hp.com/personal/Jean_Tourrilhes/Linux/Tools.html#latest Read the included instructions, which explain how to install and compile the software. Finding and turning on your wireless card Before scanning for a wireless network, you need to find the name of your wireless card, and turn it on. To find the name of your wireless card, type the following command into your command line: ~$ sudo lshw -C network lshw produces the list of hardware connected to your computer, and -C network filters the list to show only network devices. Find the entry that says description: Wireless interface; this is your 1 Debian-based distributions include some popular ones like Ubuntu, Xandros, Knoppix, and Damn Small Linux. However, this list is by no means exhaustive; a quick Google search for your distribution should tell you which core it is based on.

wireless card. Look down several lines, and note the logical name of your wireless interface. Knowing the name of your wireless interface is important, because you need it to tell your computer which piece of hardware to configure and connect with. Now, before you start connecting to a new wireless network, you need to make sure that your wireless interface is on and not still trying to connect to an old network from somewhere else. To accomplish this, type the following series of commands: ~$ sudo ifconfig [interface] down ~$ sudo dhclient -r [interface] ~$ sudo ifconfig [interface] up This series of commands uses two command-line network configuration programs: ifconfig and dhclient. ifconfig allows you to configure network interfaces, both wired and wireless; the first command uses this program to turn your wireless card off. dhclient is a command-line program that manages connections to networks that use dynamic host configuration; a protocol discussed in a little more detail later in this article. The second command uses dhclient to release [-r] your wireless card from any IP address it had been bound to before. The last command uses ifconfig again, this time to bring your wireless interface back up. Now you're ready to scan for a network. Scanning for a wireless network wireless-tools has a useful utility called iwlist. According to the man page [accessible by typing man iwlist], it shows wireless networking information that is not accessible from iwconfig. What does this mean to you? Pertinent to getting your wireless working from the command line, its scan mode reveals wireless networks that your card detects. Type the following into your command line: ~$ sudo iwlist [interface] scan | less and you will get a list of the wireless networks available. Make sure to pipe the iwlist scan into less, because usually the list is too long to fit on one screen. Piping it into less makes it a lot easier to scroll through; you can just use your up and down arrows. If you're in a familiar location like home or your favourite coffee shop, then finding the wireless network is easy. Even when you use a graphical program to connect to wireless, it shows the name of the system. That name is called the "ESSID." wireless-tools gives you that same identifier. Look through the entries returned by iwlist, and find the one with the ESSID matching the wireless network to which you normally connect. The block of information with that ESSID is going to have everything you need to know--in fact, a whole lot more than you need to know--for connecting to the wireless network. If you're in an unfamiliar location, don't despair. iwlist gives you plenty of information with which to select a wireless network. First of all, look at the "Encryption key" field; it will either say on or off. If it says on, that means it has a WEP [Wireless Encryption Protocol] key, a password to get on the network. If you're at a coffee house or another public place with WiFi, and the network associated with the location has a password, ask the staff for it. If you cannot get the password for any of the stray wireless networks around, then you can do the same thing from the iwlist results that you

can from a graphical interface: look for unsecured networks. See which networks have "Encryption key:off", and among those, look for the one with the highest Quality [another field on iwlist]. Don't be shy about jotting down your interface name and ESSID, especially if you're new to doing command-line wireless configuration. Until I had memorized the syntax of the commands and the information for my favourite wireless networks, I always had to write that information down. Configuring your connection to the wireless network Most home and public wireless networks use a protocol called DHCP: Dynamic Host Configuration Protocol. Instead of assigning a computer the same IP address to use for all time [as is true when a network uses a static protocol], the network assigns a computer its identifying details, including its IP address, when it attempts to connect. This is good news for you, since not only is it a little more secure than static IP addressing, it makes connecting to the network easier. Take the information that you gathered during your scan, and type the following onto your command line: ~$ sudo iwconfig [interface name] mode managed key [password, or 'off' if no password] essid [E S SID] This command uses the program iwconfig, yet another part of the wireless-tools suite, to configure your wireless card to be ready to connect to the wireless network of your choice. The mode managed section of the command tells your wireless card that it is supposed to associate with a central access point that is offering wireless network services, instead of trying to connect directly with other computers in a network. It is important to remember to always give a key instruction to iwconfig, even if there is no key needed to get onto the wireless system. Otherwise, if there was already a key configured for the wireless card's use, it will start giving that key to the wireless network that doesn't need oneand you will not get on the wireless network. Thus, if you wanted to use a wireless interface called eth1 to connect to a network called Homenet that doesn't use an encryption key, you would type the following: ~$ sudo iwconfig eth1 mode managed key off essid Homenet Know also that if the ESSID has more than one word, you must put it in quotes. However, as shown above, do not put single-word ESSIDs in quotes. All ESSIDs, be they single-word or multiword, are case sensitive. So, if you want to use interface ath0 to connect to a network called Robust Beans Coffee, with the encryption key cf1e94a35b, you would type the following: ~$ sudo iwconfig ath0 mode managed key cf1e94a35b essid Robust Beans Coffee Instead of directly giving a key, which can be difficult to memorize, some systems instead give out an ASCII passphrase. In many cases, the systems administrator comes up with a phrase that the router converts into a hexadecimal code, the network key. Instead of memorizing the key, you can use iwconfig with that passphrase by prefacing the passphrase with s:. To use interface eth1 to connect to a network called allienet, with the passphrase lolrushas2bukkits@home, you would type the following: ~$ sudo iwconfig eth1 mode managed key s:lolrushas2bukkits@home essid

allienet After typing this configuration command, you have told your wireless card what it needs to know in order to find the network and ask it for a connection. You are not, however, connected to the network yet. To do that, you need to get your wireless card to ask the network for a connection. Type: ~$ sudo dhclient [interface] This command should connect you to the wireless network. It will ask the access point to assign your computer an IP address. If it's successful, and the network binds your computer to an IP address, it will say on the screen: bound to [A.B.C.D] A.B.C.D. is your IP address on the network; it will be four numbers separated by periods. To make sure your connection works, go ahead and ping a website you know will be up: ~$ ping www.google.com If the ping starts returning lines like this: 64 bytes from py-in-f147.google.com (64.233.167.147): icmp_seq=5 ttl=242 time=60.9 ms Congratulations! You're on the internet. Hit ctrl-C to stop the pinging, and enjoy using the internet. If your dhclient command makes multiple attempts to connect does not return the bound to message, and instead returns an error, you are not connected to the internet. Go back and make sure that you typed all of the commands correctly, and assigned all of the commands that needed [interface] fields to the correct name of your wireless interface. If you typed any of the commands incorrectly, start from the beginning and try again. If you typed all of the commands in correctly, then it could be a problem with the network. If you have access to any of the other networks shown on iwlist, attempt to connect to it. If you do not, contact the systems administrator. Conclusion Connecting to wireless from the command line using the wireless-tools suite may seem a little complicated at first, especially if you are unaccustomed to using the command line. But, after a few times going through this process, the syntax will come. It's fairly intuitive which programs do what, and it's a useful skill to have in case you need a wireless internet connection from a Linux computer without a graphical wireless program. If you have any questions about the content of this paper, please e-mail me at adalia@weaknetlabs.com.

Vous aimerez peut-être aussi