nm-applet
, which has an icon sitting in the tray area of the desktop environment, handles the connection to WiFi. The discussion below applies to Ubuntu 15.04+, which uses
systemd
to manage initialization process. Background
NetworkManager is a daemon that manages networking devices on Linux systems. It has become the de facto network managing program for Linux since its introduction in 2004. See more at Wikipedia: https://en.wikipedia.org/wiki/NetworkManagerSince it is a daemon, users interact with it via other applications. On most modern Linux distributions, you see an icon on the tray area, that once clicked, will give you a list of WiFi network IDs (SSIDs). This application is called
nm-applet
. NetworkManager also has a command line interface called nmcli
.Easy solution: Connect once and remember.
NetworkManager will automatically connect to a WiFi network that was connected to previously. So, you can connect to a WiFi network under a desktop environment using
nm-applet
, or use command line (see below), then boot into no-GUI (e.g., system level 3) forever, and enjoy the same WiFi network forever. If you've connected to multiple WiFi networks before, it's very hard to say which one will be connected to in the future, supposing they will all be available. Most likely, the very last one connected to will be the top candidate. If unsure, see the next two sections.
In case you need to connect to a WiFi network via command line
There are many cases that you want to specify which WiFi network to connect to, e.g., the first time or you want to switch the network. The command to connect to a WiFi SSID uses the following syntax:nmcli device wifi connect $SSIDFor example,
nmcli device wifi connect Hahawhere
H360
is the SSID. Specifying the WiFi network to connect to at startup
As mentioned earlier, if you have connected to multiple WiFi networks before, NetworkManager will use an algorithm, which is unfortunately unclear and thus underterministic to me, to pick one (assuming that they are all still available) the next time. To ensure to connect to a particular network, you can create a startup service for
systemd
, the initialization manager of modern Linux, to be executed at system startup. Let's call the service
mywifi
. Create a file called mywifi.service
under /etc/systemd/systemwith content like this:
[Unit] Description=Connect to Haha [Service] ExecStart=/usr/bin/nmcli device wifi connect [Install] WantedBy=multi-user.target
As you can see, the command to connect to a network called H360 is to be executed at startup, only when the system is booting into multi-user level (see the Red Hat doc for levels.)
Then enable this service and finally reboot
systemctl enable mywifi
By "enable", we mean automatically running this service at startup without logging in.
Troubleshooting: device unavailable
I had some rare cases that a WiFi interface device is unavailable. Simply stop and restart the NetworkManager service may solve the problem:systemctl stop NetworkManager systemctl start NetworkManager
Some examples for using nmcli
forrest@A785GM-M:~$ nmcli device status DEVICE TYPE STATE CONNECTION wlx001478962ea6 wifi connected Haha 4 en2s0 ethernet unavailable -- lo loopback unmanaged -- forrest@A785GM-M:~$ nmcli general status STATE CONNECTIVITY WIFI-HW WIFI WWAN-HW WWAN connected full enabled enabled enabled enabled forrest@A785GM-M:~$ nmcli radio wifi enabled forrest@A785GM-M:~$ nmcli dev wifi * SSID MODE CHAN RATE SIGNAL BARS SECURITY * Haha Infra 1 54 Mbit/s 100 ▂▄▆█ WEP TP-LINK_6DFF Infra 5 54 Mbit/s 100 ▂▄▆█ WPA1 WPA2 -- Infra 10 54 Mbit/s 100 ▂▄▆█ WPA2 * Haha Infra 1 54 Mbit/s 99 ▂▄▆█ WEP
References
- How to write startup script for systemd, https://unix.stackexchange.com/questions/47695/how-to-write-startup-script-for-systemd
- What is the structure of network managers system-connections files? https://askubuntu.com/questions/368560/what-is-the-structure-of-network-managers-system-connections-files
- How to connect and disconnect to a network manually in terminal?https://askubuntu.com/questions/16584/how-to-connect-and-disconnect-to-a-network-manually-in-terminal/
- nmcli-examples -- usage examples of nmcli, https://developer.gnome.org/NetworkManager/stable/nmcli-examples.html
No comments:
Post a Comment