Skip to content

Instantly share code, notes, and snippets.

@bespokoid
Created October 13, 2019 21:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bespokoid/feedbf34c4829a19dfc2b5a43a0a92a3 to your computer and use it in GitHub Desktop.
Save bespokoid/feedbf34c4829a19dfc2b5a43a0a92a3 to your computer and use it in GitHub Desktop.
Connecting to wifi network through command line #linux #cmd

Connecting to wifi network through command line

I am trying to connect to my WEP network just using the command-line (Linux).

I run:

sudo iwconfig wlan0 mode Managed essid 'my_network' key 'xx:xx:... hex key, 26 digits'

Then I try to obtain an IP with

sudo dhclient -v wlan0

or

sudo dhclient wlan0

without success (tried to ping google.com).

I know that the keyword is right, and I also tried with the ASCII key using 's:key', and again, the same result.

I get the message below when running dhclient:

Listening on LPF/wlan0/44:...
Sending on   LPF/wlan0/44:...
Sending on   Socket/fallback
DHCPDISCOVER on wlan0 to 255.255.255.255 port 67 interval 3 

I have no problem connecting with WICD or the standard Ubuntu tool.

Option 1

Just edit /etc/network/interfaces and write:

auto wlan0
iface wlan0 inet dhcp 
                wpa-ssid {ssid}
                wpa-psk  {password}

After that write and close file and use command:

sudo dhclient wlan0

Replace {ssid} and {password} with your respective WiFi SSID and password.

Option 2

Provided you replace your Wireless network card, Wi-Fi Network name, and Wi-FI Password this should also work.

I am using: - Wireless network card is wlan0 - Wireless network is "Wifi2Home" - Wireless network key is ASCII code ABCDE12345

First, get your WiFi card up and running:

sudo ifconfig wlan0 up

Now scan for a list of WiFi networks in range:

sudo iwlist wlan0 scan

This will show you a list of wireless networks, pick yours from the list:

sudo iwconfig wlan0 essid Wifi2Home key s:ABCDE12345

To obtain the IP address, now request it with the Dynamic Host Client:

sudo dhclient wlan0

You should then be connected to the WiFi network. The first option is better, because it will be able to run as a cron job to start up the wifi whenever you need it going. If you need to turn off your WiFi for whatever reason, just type:

sudo ifconfig wlan0 down

FYI I have also seen people using alternative commands. I use Debian, Solaris and OSX, so I'm not 100% sure if they are the same on Ubuntu. But here they are:

sudo ifup wlan0 is the same as sudo ifconfig wlan0 up sudo ifdown wlan0 is the same as sudo ifconfig wlan down

Best option

There is Danijel J's two options are good, but there is also a 3rd option if you have this working via the 'standard Ubuntu tool' using nmcli, which should already be installed at /usr/bin/nmcli.

First, run

nmcli c

This will list your connections, with the first column being the SSID, and the second column being the UUID of the connection.

Copy the UUID of the SSID you want to connect to so you can paste it into the next command.

Next, run

nmcli c up uuid <paste uuid here>

and this will, using the same stuff as the 'standard Ubuntu tool' connect to your wifi!

Second best option

If you have nmcli installed, I think this is the simplest solution.

For a new connection:

nmcli dev wifi connect <mySSID> password <myPassword>

Or if a connection was already set up:

nmcli con up <mySSID>

(or if that does not work, try nmcli con up id )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment