How to connect to WiFi network using console or terminal on GNU / Linux

Sometimes its useful to connect to Wireless Networks using console . The reasons for that might be many, one possible reason is to be able to debug, Wireless connection failures or simply omit the use of the many available GUI wifi connection programs.

As a first step before connecting in terminal is to look up for the wifi networks available for connection, this is done with cmd:

linux:~# iwlist wlan0 scanning
wlan0 Scan completed :
Cell 01 - Address: 00:24:01:90:8F:38
Frequency:2.442 GHz (Channel 7)
Quality=70/70 Signal level=-39 dBm
Encryption key:on
Bit Rates:1 Mb/s; 2 Mb/s; 5.5 Mb/s; 11 Mb/s
Bit Rates:6 Mb/s; 9 Mb/s; 12 Mb/s; 48 Mb/s; 18 Mb/s
24 Mb/s; 36 Mb/s; 54 Mb/s
Extra: Last beacon: 68ms ago
IE: Unknown: 00086D616764616E6F7A
IE: Unknown: 010482848B96
IE: Unknown: 030107
IE: Unknown: 32080C1218602430486C
IE: Unknown: CC0700CC020000018A
IE: Unknown: CC0700CC0300000100
IE: WPA Version 1
Group Cipher : TKIP
Pairwise Ciphers (2) : TKIP CCMP
Authentication Suites (1) : PSK
IE: IEEE 802.11i/WPA2 Version 1
Group Cipher : TKIP
Pairwise Ciphers (2) : TKIP CCMP
Authentication Suites (1) : PSK
Cell 02 - Address: 00:1E:2A:60:5E:DC

To just list the ESSID s of the wifi networks:

linux:~# iwlist wlan0 scanning|grep -i 'essid'

1. Connecting to Open Wireless Network

Now from the above output it is clear 6 wifi networks are available for connection. The default wifi network from the list is an Open network (e.g. without pass). To connect to it I use cmd:

linux:~# /sbin/iwconfig wlan0 essid 'default'
linux:~# /sbin/iwconfig wlan0 key open

After connected to configure IP, Gateway and DNS from a DHCP server running on the WIFI router, dhclient cmd is used:

linux:~# /sbin/dhclient wlan0

2. Connecting to WEP 64bit / 128bit encrypted network

linux:~# /sbin/iwconfig wlan0 key 1234-5678-9101-1213 3. Connecting to WPA / WPA2 encrypted wifi network

To connect to WPA or WPA2 encrypted network its necessery to have installed wpasupplicant package. The name of the package might vary in different distributions on Debian and Ubuntu, the name of the package is wpasupplicant, on Fedora, CentOS and RHEL the package that has to be in is wpa_supplicant :
After having installed the wpa_supplicant to connect to the network with ESSID name magdanoz , wpa_passphrase is used first:

linux:~# /usr/bin/wpa_passphrase magdanoz Secret_Wifi_Password | tee -a /etc/wpa_supplicant.conf

As you see in above command the secret password key is generated printed on the screen and then added to /etc/wpa_supplicant.conf , necessery to establish the wireless connection with wpa_supplicant with cmd:

linux:~# /sbin/wpa_supplicant wpa_supplicant -d wext -i wlan0 -c /etc/wpa_supplicant.conf -B

-d wext instructs wpa_supplicant to use (Linux wireless extension driver).
-B tells wpa_supplicant to background the connection to prevent the wireless connection to drop off, if the console / terminal from which it is launched gets closed.

In case of succesful connection with wpa_supplicant , once again IP, Gateway and DNS is configured fetching the settings from the wifi hotspot dhcp server:

linux:~# /sbin/dhclient wlan0

General information about the wireless network and info related to the established connection can be obtained with /usr/bin/iwconfig :

linux:~# /sbin/iwconfig
lo no wireless extensions.
eth0 no wireless extensions.
wlan0 IEEE 802.11abg ESSID:"magdanoz"
Mode:Managed Frequency:2.442 GHz Access Point: 00:24:00:90:8F:38
Bit Rate=54 Mb/s Tx-Power=15 dBm
Retry long limit:7 RTS thr:off Fragment thr:off
Encryption key:off
Power Management:off
Link Quality=70/70 Signal level=-39 dBm
Rx invalid nwid:0 Rx invalid crypt:0 Rx invalid frag:0
Tx excessive retries:0 Invalid misc:0 Missed beacon:0

To configure the exact channel over which the wireless connection will be established again is done with iwconfig, for instance to configure wlan0 wifi connection established to be on wifi channel 7:

linux:~# /sbin/iwconfig wlan0 channel 11

By default iwconfig is set to automatically set the channel based on connected network ESSID , if the channel is modified to some specific number to revert it back use:

linux:~# /sbin/iwconfig wlan0 channel auto

Share this on

Download PDFDownload PDF

Tags: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,

9 Responses to “How to connect to WiFi network using console or terminal on GNU / Linux”

  1. wypadkek odszkodowanie says:
    Firefox 3.0.14 Firefox 3.0.14 Windows XP Windows XP
    Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv: Gecko/2009082707 Firefox/3.0.14 (.NET CLR 3.5.30729)

    Very useful tips…

    View Comment
  2. Moises Cutwright says:
    Firefox 3.0.14 Firefox 3.0.14 Windows XP Windows XP
    Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv: Gecko/2009082707 Firefox/3.0.14 (.NET CLR 3.5.30729)

    How do i get rid of undefined in blog posts?

    View Comment
  3. me says:
    Google Chrome 22.0.1229.79 Google Chrome 22.0.1229.79 Windows 7 x64 Edition Windows 7 x64 Edition
    Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.79 Safari/537.4

    dunno if this is new or old, but -d is not debug, -D is driver

    View Comment
  4. Hai Chapparo says:
    Firefox 3.5.3 Firefox 3.5.3 Windows XP Windows XP
    Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv: Gecko/20090824 Firefox/3.5.3 (.NET CLR 3.5.30729)

    I love this so much. I think it is good that people get to hear about this.

    View Comment
  5. psn code generator qui fonctionne says:
    Google Chrome 28.0.1500.71 Google Chrome 28.0.1500.71 Mac OS X  10.8.3 Mac OS X 10.8.3
    Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.71 Safari/537.36

    Wow that was odd. I just wrote an really long
    comment but after I clicked submit my comment didn’t appear.
    Grrrr… well I’m not writing all that over again.
    Anyhow, just wanted to say wonderful blog!

    View Comment
  6. Guoan says:
    Chrome 36.0.1985.49 Chrome 36.0.1985.49 iPad iOS 7.1.2 iPad iOS 7.1.2
    Mozilla/5.0 (iPad; CPU OS 7_1_2 like Mac OS X) AppleWebKit/537.51.2 (KHTML, like Gecko) CriOS/36.0.1985.49 Mobile/11D257 Safari/9537.53

    My x server was broken today, so I entered into command line mode. Thanks to this blog I connected to my wifi router under cli and updated my x server. Thanks bro! 

    View Comment
  7. Raspberry Pi med RetroPie, del 3- Modifiera inställningar och lägga in spel över SFTP | says:
    WordPress 4.1.1 WordPress 4.1.1
    The Incutio XML-RPC PHP Library -- WordPress/4.1.1

    […] Istället ska vi gå igenom hur man kan ansluta med SFTP, eller FTP över SSH, över nätverk och den vägen både ladda upp filer och göra ändringar i inställningsfiler. Jag antar att du anslutit din Pi till nätverket med en nätverkskabel med DHCP (t.ex. en router). Det går såklart att använda WiFi, men hur man konfigurerar detta går jag inte in på här men kan tipsa om följande sida där man går igenom hur man gör. […]

    View Comment
  8. Lamar Cannington says:
    Opera 9.64 Opera 9.64 Windows XP Windows XP
    Opera/9.64(Windows NT 5.1; U; en) Presto/2.1.1

    Oh my goodness! an amazing article dude. Thanks Nevertheless I’m experiencing subject with ur rss . Don’t know why Unable to subscribe to it. Is there anyone getting similar rss drawback? Anybody who is aware of kindly respond. Thnkx

    View Comment
  9. Btc Adsl Root Password | Great P2P Currency says:
    WordPress 4.2.5 WordPress 4.2.5
    The Incutio XML-RPC PHP Library -- WordPress/4.2.5

    […] How to connect to WiFi network using console or terminal … – More helpful Articles. How to SSH client Login to server with password provided from command line as a script argument – Running same commands to many Linux servers […]

    View Comment

Leave a Reply

CommentLuv badge