211

Other posts seems to be addressing more complicated network connection issues from the command line.

The Unity panel Network indicator/button doesn't respond too well sometimes - it keeps trying to connect to a network even when i click on "disconnect", stuff like that.

So I want to go command line for the control. I don't like GUIs anyway.

Is there not some simple command line tool which can do something like the following?

wifi connect MyNetworkNameA
wifi disconnect
wifi connect MyNetworkNameB
cmo
  • 6,338
  • 5
  • 16
  • 14
  • 4
    Depends , what wireless security ? This is covered in detail here - https://help.ubuntu.com/community/NetworkConfigurationCommandLine/Automatic#Wireless – Panther May 06 '14 at 15:27
  • On Ubuntu 16.04 and on 18.04, still experiencing wifi disconnects. A reconnect command would be a good hack. – AlikElzin-kilaka Jul 20 '19 at 03:42

4 Answers4

265

I think you want to keep using managed interface (by NetworkManager). nmcli is a command‐line tool for controlling NetworkManager.

  • To see list of saved connections, use (<SavedWiFiConn>)

    nmcli c
    
  • To see list of available WiFi hotspots (<WiFiSSID>)

    nmcli d wifi list
    

    or:

    sudo iwlist <WifiInterface> scanning
    
  • To see list of interfaces (<WifiInterface>)

    ifconfig -a
    

Just change <WifiInterface>, <WiFiSSID>, <WiFiPassword> in the following commands to reflect your setup. If WiFi info already saved, easier way using <SavedWiFiConn> name of connection as it was saved in NetworkManager.

Ubuntu 16.04

##disconnect
nmcli d disconnect <WifiInterface>

##connect
nmcli d connect <WifiInterface>

Another way:

##disconnect
nmcli c down <SavedWiFiConn>

##connect
nmcli c up <SavedWiFiConn>

Ubuntu 15.10 & previous

##disconnect
nmcli d disconnect iface <WifiInterface>

##connect
nmcli d wifi connect <WiFiSSID> password <WiFiPassword> iface <WifiInterface>

Another way:

##disconnect:
nmcli c down id <SavedWiFiConn>

##connect:
nmcli c up id <SavedWiFiConn>

If your password isn't automatically recognized type this:

nmcli -a c up <SavedWiFiConn>

Reference: man nmcli

user.dz
  • 48,105
  • 1
    Works like a charm! And, as suspected, the command line responds immediately, while the GUI can be sluggish, uncooperative, and unresponsive sometimes. – cmo May 06 '14 at 15:55
  • 1
    Saved my day when I lost my Unity dash and taskbar, and the Network Settings dialog kept on crashing upon any kind of network connection establishment. – Janaka Bandara Nov 27 '14 at 01:21
  • is there a way to automate it so it connects whenever the wifi is available ? – kirill_igum Dec 01 '15 at 19:44
  • 2
    @kirill_igum , sorry to be late quiet busy, You may use cron for scheduling wifi scan using iwlist wlan0 scanning or nmcli d wifi list then nmcli to connect to the corresponding wifi network depending on your needs. – user.dz Jan 11 '16 at 21:59
  • 1
    On Ubuntu 14.04 this connect command returns Error: Device 'wlan0' is not an Wi-Fi device. I believe this answer is incomplete. – Luís de Sousa Mar 07 '16 at 16:19
  • This is invalid syntax. Unknown parameter: iface Unknown parameter: wlan0 – Cerin Apr 18 '16 at 05:56
  • I have no idea how this works for me. I am using blue tooth tether. bnep0 but I do not know what to type or how to get the ssid. I googled it and almost all of the commands did not do anything or were invalid or returned an empty line. – Bhikkhu Subhuti May 18 '16 at 14:40
  • @BhikkhuSubhuti, Try using sudo iwlist wlan0 scanning or nmcli d wifi list to get the available networks ssid's. – user.dz May 18 '16 at 14:44
  • "iwlist wlan0" says invalid command but iwlist alone is a command that works but just give a list of params to use. nmcli d wifi list gives and empty header. I am using blue tooth tether. – Bhikkhu Subhuti May 19 '16 at 00:25
  • 1
    Can someone give me an example of what follows the parameter "ID", as in the command nmcli c up id <WiFiConn>. What is a "<WiFiConn>"? – L. D. James Jun 15 '16 at 21:06
  • @L.D.James, the name used to save the connection settings, 1st column in this output nmcli c which list all saved connection setups – user.dz Jun 15 '16 at 21:11
  • @sneetsher Ok. I see. I believe you have to replace the word id with the information in first column, otherwise you get an error. It wasn't initially clear in the answer. – L. D. James Jun 15 '16 at 21:34
  • @L.D.James, It is already mentioned: <WiFiConn> name of connection as it was save in NetworkManager (Check nmcli c). Or it is not clear enough. – user.dz Jun 15 '16 at 21:40
  • 1
    The command that I was issuing was giving an error. I was issuing this command: nmcli c up id "Apollo III Connect" It gave an error. When I leave out the word id and typed this it connects without error: nmcli c "Apollo III Connect". I understand how to connect it now. You're saying id as the parameter and saying replace the word id with the name of the connection, which you are naming . Thanks! – L. D. James Jun 15 '16 at 22:48
  • @L.D.James, Are you using 16.04 release ? Could you tell your output of nmcli -v? – user.dz Jun 15 '16 at 23:45
  • 1
    mcli tool, version 1.2.0 – L. D. James Jun 15 '16 at 23:48
  • 1
    How to save a connection setup? If it's the first time I connect to a wifi! – Cirelli94 Apr 13 '17 at 11:06
  • 6
    To save a connection setup: sudo nmcli dev wifi con "SSID_NAME" password PASSWORD "CONFIG_NAME" To find auto your saved connection setups: nmcli c – Cirelli94 Apr 13 '17 at 13:05
  • @Cirelli94 Good you seem already found it, just connecting to new wifi makes nm save it on new entry, no extra. – user.dz Apr 13 '17 at 14:50
  • nmcli doesnt seem to be installed by default on server installs. How do we install it (particularly if we have no working network connection)? Alternatively, how do we perform these steps with iw, which is installed by default, but seems a lot harder to use? – Jules Mar 08 '18 at 18:47
  • when password is wrong, nmcli pops up a GUI input box (for retyping the correct password). this can't be scripted - is there a way to disable GUI ? – Berry Tsakala Dec 27 '19 at 12:50
  • @BerryTsakala you may check beforehand if the SSID has the Security field with a value different than none. – ignacio Jul 14 '20 at 13:51
147

nmtui ncurses solution

Great interactive ncurses network manager option:

nmtui

If for some reason it is not installed, the Debian package is:

sudo apt install network-manager

Comes in the same package as nm-applet (the default top bar icon thing) and nm-cli, and is therefore widely available.

Screenshot:

enter image description here

Tested in Ubuntu 17.10.

  • Thanks! For some reason, my Ubuntu refused to connect to Wifi, so I plugged in the Ethernet cable and clicked Activate on Wired Connection and BAM! – asgs Nov 02 '17 at 15:12
  • @Downvoters please explain, I never retaliate ;-) – Ciro Santilli OurBigBook.com Nov 13 '17 at 10:24
  • 3
    works on Ubuntu 16.01 as well, cool solution! – Jan Sila Dec 01 '17 at 08:57
  • 8
    This is a much more intuitive solution than what is above IMO. Unless you're trying to learn how Linux/Unix connects to WiFi, this seems like the ideal select and press enter solution. Works well on Ubuntu 18 – jamzsabb Jun 18 '18 at 18:56
  • 3
    This "just worked " on Ubuntu 18.04 – Ryre Dec 09 '19 at 03:32
  • 1
    and Jetson Nano ... and usefully it kept eth0 up too (I'm running headless and didn't lose the ssh session) – P.Windridge Sep 28 '20 at 19:49
  • 1
    F***ING THANK YOU! Over the years I've had to rescue a laptop several times, and I always have to look up the spells required for wifi. nmtui is the best solution and just works! – Sarke Dec 28 '22 at 01:12
27

If your wi-fi access point is saved, it will auto-connect. Turn wireless on or off with a simpler command:

nmcli nm wifi on
nmcli nm wifi off

on newer version:

nmcli radio wifi on
nmcli radio wifi off

For reference, see man nmcli.

user.dz
  • 48,105
  • 2
    How do you make NM enable wifi automatically during boot? Every time I reboot, Network Manager starts with wifi disabled. – Cerin Jul 25 '15 at 21:46
  • @Cerin, you may try adding this command to startup applications, see https://askubuntu.com/questions/243139/how-to-add-application-to-startup-application-menu – user.dz Jan 11 '16 at 21:47
  • And how do in Ubuntu > 14.04. – monitor35 Apr 18 '16 at 08:35
14

If you want to connect to a network called PrettyFlyForAWiFi-5G

nmcli -a d wifi connect PrettyFlyForAWiFi-5G

-a (or --ask) means it will ask you for the password. The connection will be saved and should connect automatically if you restart your computer.

You could append password <your password> to the end (the literal word password followed by the actual password)

nmcli d wifi connect PrettyFlyForAWiFi-5G password 12345678

but maybe run unset HISTFILE beforehand, so that your WiFi password isn't saved to your ~/.bash_history file.


To see all the WiFi networks around you (--rescan yes is optional, the list of networks might be up to 30 seconds old without it)

nmcli d wifi list --rescan yes

which will output something like

IN-USE  SSID                         MODE   CHAN  RATE        SIGNAL  BARS  SECURITY
        PrettyFlyForAWiFi-5G         Infra  44    405 Mbit/s  59      ▂▄▆_  WPA2
        PrettyFlyForAWiFi            Infra  6     195 Mbit/s  41      ▂▄__  WPA1 WPA2

To forget a connection (that was saved after you ran nmcli -a d wifi connect <name>)

nmcli c delete PrettyFlyForAWiFi-5G

To see a list of all saved connections

nmcli c

You can use nmcli connection instead of nmcli c <whatever> and nmcli device instead of nmcli d

nmcli is the command line interface for NetworkManager (which is part of GNOME, Ubuntu's default desktop environment) and is already installed on Ubuntu.

Don't forget to set up your country code for using the perfect regulations:

sudo iw reg set <2 letter country code>

sudo nano /etc/default/crda 
  • 2
    When using nmcli, there is a pop up to reask the password if it was miss-typed. Would you know whether there is a possibility of disabling this feature? Quite annoying when trying to use nmcli in a non-interactive script... – borizzzzz Oct 18 '19 at 12:24