Connecting to wired/wireless networks via command line

tagged under   linux  


If you use a window manager like DWM, i3, Xmonad, etc, you are more than likely to get rid of the usual panel/system tray you would normally find in xfwm, lxde, etc. So here is how you can use the command line to connect to wired/wireless networks in Linux.

NOTE: The following tutorial uses the 'ip' command instead of the ifconfig command where possible.

Pre-requisites

If you want to connect to wireless networks secured with WPA, you will need to install wpasupplicant.

$ sudo apt-get install wpasupplicant

If you want to connect to a PPP network (Ex: Reliance netconnect+ broadband), you will need to install wvdial.

$ sudo apt-get install wvdial

Connecting to a wired network

Assuming your wired network interface is named eth0,

$ sudo ip link set dev eth0 up

$ sudo dhclient eth0

Connecting to a wireless (WEP) network

Assuming the essid of the wireless network you are trying to connect to is XXXXXXX and the hex key is YYYYYYY, and the your wireless network interface is named wlan0,

$ sudo ip link set dev wlan0 up

$ sudo iwconfig wlan0 essid "XXXXXXX"

$ sudo iwconfig wlan0 key YYYYYYY

$ sudo dhclient wlan0

Connecting to a wireless (WPA) network

Assuming the essid of the wireless network you are trying to connect to is XXXXXXX and the hex key is YYYYYYY, and your wireless network interface is named wlan0, first create a config file under /etc as shown below.

$ sudo touch /etc/wpa_supplicant_xxxx.conf

NOTE: you can use any file name above. One file required per wireless network that you want to connect to.

Enter the following contents into the above file and save it (root permission required).

ap_scan=1
ctrl_interface=/var/run/wpa_supplicant

network={
    ssid="XXXXXXX"
    scan_ssid=0
    proto=WPA
    key_mgmt=WPA-PSK
    psk="YYYYYYY"
    pairwise=TKIP
    group=TKIP
}

Now, to connect to the above wireless network,

$ sudo ip link set dev wlan0 up

$ sudo wpa_supplicant -Dwext -iwlan0 -c/etc/wpa_supplicant_xxxx.conf -B

$ sudo dhclient wlan0

BONUS : Connecting to wireless (PEAP) network

PEAP encrypted networks are usually used in corporate environments. Here's how you can connect to it.

Assuming the essid of the wireless network you are trying to connect to is XXXXXXX and the hex key is YYYYYYY, and your wireless network interface is named wlan0, first create a config file under /etc as shown below.

$ sudo touch /etc/wpa_supplicant_xxxx.conf

NOTE: you can use any file name above. One file required per wireless network that you want to connect to.

Enter the following contents into the above file and save it (root permission required).

network={
    ssid="XXXXXXX"
    key_mgmt=WPA-EAP
    eap=PEAP
    identity="username@company.com"
    password="YYYYYYY"
    pairwise=CCMP TKIP
    group=CCMP TKIP
    phase2="auth=MSCHAPV2"
}

Now, to connect to the above wireless network,

$ sudo ip link set dev wlan0 up

$ sudo wpa_supplicant -Dwext -iwlan0 -c/etc/wpa_supplicant_xxxx.conf -B

$ sudo dhclient wlan0

BONUS : Connecting to a PPP network (Mobile Broadband)

First, create the following file under /etc

$ sudo touch /etc/wvdial.conf

Enter the following contents into the above file (for reliance netconnect+ broadband)

[Dialer Defaults]
Phone =
Username =
Password =
New PPPD = yes

[Dialer NAME_OF_YOUR_NETWORK]
Phone = #777
Username = <username>
Password = <password>
Stupid Mode = 1
Idle Seconds = 0
Dial Attempts = 0
Modem = /dev/ttyUSB0

Now, to connect to the mobile broadband network above,

$ sudo wvdial NAME_OF_YOUR_NETWORK

BONUS : Listing available wireless networks

To list the available wireless networks (i.e, their essid's) much like Network Manager does,

$ sudo ip link set dev wlan0 up

$ sudo iwlist wlan0 scan | grep ESSID | awk -F\" '{print $2}'

« older posts newer posts »



other articles you might like
  • Configuring Lenovo T440s touchpad in Linux
  • Alternatives to Truecrypt on Linux
  • Android reverse USB tethering
  • Linux command line tools and tricks - Part 3
  • Basic dwm setup in (L)ubuntu
  • Basic Xmonad setup in (L)ubuntu
  • EOM a.k.a End of Mail a.k.a Emacs + offlineimap + mu4e
  • First attempt at developing a Firefox addon
  • Bash script to perform super-fast file transfer using netcat and tar
  • Jekyll : Handling Github page build failure and Jekyll plugins on Github
  • Emacs newbie cheat sheet -- from a vim convert
  • ncmpcpp : ncurses based music player for Linux
  • How to create a custom CWM recovery flash-able android apps zip file in Linux via command line
  • Task Warrior - The best CLI task manager for Linux
  • Custom kernel in Ubuntu (also generate .deb package)
  • Linux command line tools and tricks - Part 2
  • Superb Linux command line tools and tricks for Linux geeks
  • RSA encryption using Tcl



  • Share this post ⇒  





    blog comments powered by Disqus