Connecting to the Internet Using Command Line in Linux

Many of the times you may use a Linux system that does not have a GUI after install and it needs an internet connection to set up a desktop environment, also you may use Linux servers without a GUI and you need to connect over a wireless network using the command line. Below you will see Steps to connect to a wireless network using the command line.

Determine your Network Interface

The first thing you need to do is determining your Wireless Interface, to do so give the following command:

iwconfig

This will list out all the active network interfaces, most of the time it will be a wlan0 for your wireless network but can be something other, depending on your hardware.

Turn on your Wireless Interface



Now you need to ensure that your network interface is up and working, to do so give the following command.

sudo ifconfig wlan0 up 

wlan0 is your network interface, make sure you change it if your one is different.

Scan for available wireless access points

Now you will need to scan for all the available Access points, to do so give the following command

sudo iwlist scan | more

where more will help you get systematic scroll as the list could be long and you do not want that some entries disappear and you cannot scroll up as you are working in the command-line interface. Look at the ESSID, that is the name of your wireless network. To find an open network just check items that show Encryption Key set to off.

Create a WPA supplicant configuration file

The most common and widely tool used is WPA supplicant, most of the distros have it in default, just give the command

wpa_passphrase

Now if you see any error you are in a deadlock situation as you cannot use this tool or it’s not installed. To create a configuration file for wpa_supplicant, run the following command:



wpa_passphrase ESSID > /etc/wpa_supplicant/wpa_supplicant.conf

Where ESSID will be your Access point name which you have noted from iwlist command, now after running the command your prompt is still not ended, now you need to type the security key of the Access point you need to connect to and press Enter and your prompt ends now.

After creating file check if the command worked, just give command:

cd /etc/wpa_supplicant

Type the following:

tail wpa_supplicant.conf

and you should see something like below:

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

Find name of your wireless driver

Before proper connectivity there is more piece of information you will need which is the name of your driver of wireless network card, just give the command:

wpa_supplicant -help | more

The command will list the section of drivers which will look like this:

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

Now, in this case, my appropriate driver is nl80211, this will be used in further connectivity.

Connect to the internet

The first step is to run the wpa_supplicant command :

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

where “driver” will be your driver(nl80211 in my case) without double quotes and “interface” will be your interface(wlan0 in my case) without double quotes.

Finally run the command:

sudo dhclient

This is for the DCHP client –dhclient– which will establish networking routing on the local Network. Now still to check connectivity you can just ping any website.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.