How to Connect to the Internet Using the Linux Command Line

Troubleshoot a broken network install through a series of shell commands

If you installed a headless distribution—a distribution that doesn’t run a graphical desktop—then you won’t have network management tools to help you connect to the internet using a Wi-Fi network. Likewise, if you accidentally deleted key components from your desktop or installed a buggy distribution, the only way to connect to the internet requires using the Linux terminal.

The instructions in this article apply generally to all Linux distributions, however, it's possible that some distributions won't act exactly as described here.

An abstract design that looks like a current of energy flowing in a cricle
zf L / Getty Images 

Determine Your Wireless Network Interface

From within the terminal enter the following command:

iwconfig

The command outputs a list of active network interfaces.

The most common wireless network interface is wlan0 but can be other things as well, depending on the nature of your hardware and how well optimized your distributions wireless drivers are.

Turn On the Wireless Interface

Ensure that the wireless interface is turned on. Use the following command:

sudo ifconfig wlan0 up

Replace the wlan0 with the name of your network interface.

Scan for Wireless Access Points

Search for networks. Type the following command:

sudo iwlist scan | more

A list of available wireless access points appears. The results look something like this:

Cell 02 - Address: 98:E7:F5:B8:58:B1
Channel:6
Frequency:2.437 GHz (Channel 6)
Quality=68/70 Signal level=-42 dBm
Encryption key:on
ESSID:"HONOR_PLK_E2CF"
Bit Rates:1 Mb/s; 2 Mb/s; 5.5 Mb/s; 11 Mb/s; 18 Mb/s
24 Mb/s; 36 Mb/s; 54 Mb/s
Bit Rates:6 Mb/s; 9 Mb/s; 12 Mb/s; 48 Mb/s
Mode:Master
Extra:tsf=000000008e18b46e
Extra: Last beacon: 4ms ago
IE: Unknown: 000E484F4E4F525F504C4B5F45324346
IE: Unknown: 010882848B962430486C
IE: Unknown: 030106
IE: Unknown: 0706434E20010D14
IE: Unknown: 200100
IE: Unknown: 23021200
IE: Unknown: 2A0100
IE: Unknown: 2F0100
IE: IEEE 802.11i/WPA2 Version 1
Group Cipher : CCMP
Pairwise Ciphers (1) : CCMP
Authentication Suites (1) : PSK
IE: Unknown: 32040C121860
IE: Unknown: 2D1A2D1117FF00000000000000000000000000000000000000000000
IE: Unknown: 3D1606081100000000000000000000000000000000000000
IE: Unknown: 7F080400000000000040
IE: Unknown: DD090010180200001C0000
IE: Unknown: DD180050F2020101800003A4000027A4000042435E0062322F00

Look at the ESSID, which is the name of a wireless network. Find open networks by looking for items that have the Encryption Key set to off.

It's best to look for a trusted Wi-Fi network ESSID that you know the password to, rather than looking for open ones when possible. Open wi-fi networks generally have no encryption and data transferred could be transmitted insecurely.

Create a WPA Supplicant Configuration File

The most common tool for connecting to WPA-secured wireless networks is WPA Supplicant. Most distributions come with this tool installed by default. Type the following into the shell:

wpa_passphrase

If you see an error that the command cannot be found, then it isn’t installed. You are now in a chicken-and-egg scenario whereby you need this tool to connect to the internet but can’t connect to the internet because you don’t have this tool.

Use an Ethernet connection instead to install wpasupplicant. Without this tool, you cannot proceed.

To create the configuration file for wpa_supplicant, run the following command:

wpa_passphrase ESSID > /etc/wpa_supplicant/wpa_supplicant.conf

The ESSID will be the ESSID you noted from the iwlist scan command. This command stops without coming back to the shell prompt. Enter the security required for the network and press Enter. To check that the command worked, open the .config folder using the cd and tail commands:

cd /etc/wpa_supplicant

Type the following:

tail wpa_supplicant.conf

You should see something like this:

network={
ssid="yournetwork"
#psk="yourpassword"
psk=388961f3638a28fd6f68sdd1fe41d1c75f0124ad34536a3f0747fe417432d888888
}

Find the Name of Your Wireless Driver

There is one more piece of information you need before connecting to the internet—the driver for your wireless network card.

To find it, run the following command:

wpa_supplicant –help | more

The output lists a section called drivers. The list will look something like this:

drivers:
nl80211 = Linux nl80211/cfg80211
wext = Linux wireless extensions (generic)
wired = Wired Ethernet driver
none = no driver (RADIUS server/WPS ER)

Generally, wext is a catchall driver if nothing else is available. In this sample listing, the appropriate driver is the nl80211. The listing appears in priority order.

Connect to the Internet

The first step to getting connected is running the wpa_supplicant command:

sudo wpa_supplicant -B -D driver -i interface -c /etc/wpa_supplicant/wpa_supplicant.conf

Replace driver with the driver that you found in the previous section and interface with the device name (e.g., wlan0).

This command runs wpa_supplicant with the specified driver, using the targeted network interface and the configuration file. The -B switch runs the command in the background so the shell prompt returns to the foreground.

Run this one final command:

sudo dhclient

The DCHP client—dhclient—establishes networking routing on the local network. When it's active, you're free to use the internet as normal.