connecting to wireless networks from command line
December 16, 2016 - Reading time: 4 minutes
You can create a wireless network configuration that will be managed by Network Manager by creating files in /etc/NetworkManager/system-connections. Look at existing files to see how the format looks like.
Once the connection is created, you can use the nmcli command to manage NetworkManager from the command line, doing things such as enabling, disabling and querying connections.
Incidentally, the System Testing tool (checkbox) has a script that does exactly this: creates a connection and enables it, with parameters that you supply on the command line.
For instance, this creates a connection to the open "duck" network:
sudo /usr/share/checkbox/scripts/create_connection duck
This will create a connection to a network using WPA2 security, with "wings" password:
sudo /usr/share/checkbox/scripts/create_connection -S wpa -K wings duck
The script is written in Python so it should be easy for you to look at and adapt to your needs.
The script's help says this:
Usage: create_connection [options] SSID
Options:
-h, --help show this help message and exit
-S SECURITY, --security=SECURITY
The type of security to be used by the connection.
One of wpa and wep. No security will be used if
nothing is specified.
-K KEY, --key=KEY The encryption key required by the router.
-U UUID, --uuid=UUID The uuid to assign to the connection for use by
NetworkManager. One will be generated if not
specified here.
-R RETRIES, --retries=RETRIES
The number of times to attempt bringing up the
connection until it is confirmed as active.
-I INTERVAL, --interval=INTERVAL
The time to wait between attempts to detect the
registration of the connection.
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