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 you have installed a buggy distribution, the only way to connect to the internet is via the Linux terminal.

With access to the internet from the Linux command line, you can use tools such as wget to download web pages and files. The command line package managers will also be available for your distribution such as apt-get, yum and PacMan. With access to package managers, you have all you need to install a desktop environment should you require one.

Linux Terminal Internet Connection
 Screenshot

This information applies to any desktop Linux distribution with working shell access.

Determine Your Wireless Network Interface

From within the terminal enter the following command:

iwconfig

You will see a list of 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 the Wireless Interface On

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

sudo ifconfig wlan0 up

Replace the wlan0 with the name of your network interface.

Scan for Wireless Access Points

Search for networks to connect to. Type the following command:

sudo iwlist scan | more

A list of available wireless access points will appear. The results will 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

It all looks fairly confusing but you only need a couple of bits of information.

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.

Create A WPA Supplicant Configuration File

The most common tool used to connect to wireless networks which require a WPA security key 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 to use 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 command line. 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 be 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).

Basically, this command is running wpa_supplicant with the driver specified using the network interface specified and the configuration created in the section “Create a WPA Supplicant Configuration File."

The -B switch runs the command in the background so you get access to the terminal back.

Now you need to run this one final command:

sudo dhclient

That is it. You should now have an internet connection.