Connecting to a WiFi network on Linux at startup without GUI

Many embedded Linux platforms are expected to connect to a WiFi network at startup automatically. And the Linux system is booted into multi-user model, i.e., no GUI, X window, or desktop environment. A lot of the solutions, e.g., making a connection available for all users, floating on the Internet are for Linux systems that boot into a desktop environment. In those cases, the 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/NetworkManager

Since 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 $SSID
For example,
nmcli device wifi connect Haha
where 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/system
with 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

No comments: