Baeldung Pro – Linux – NPI EA (cat = Baeldung on Linux)
announcement - icon

Learn through the super-clean Baeldung Pro experience:

>> Membership and Baeldung Pro.

No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.

Partner – Orkes – NPI EA (tag=Kubernetes)
announcement - icon

Modern software architecture is often broken. Slow delivery leads to missed opportunities, innovation is stalled due to architectural complexities, and engineering resources are exceedingly expensive.

Orkes is the leading workflow orchestration platform built to enable teams to transform the way they develop, connect, and deploy applications, microservices, AI agents, and more.

With Orkes Conductor managed through Orkes Cloud, developers can focus on building mission critical applications without worrying about infrastructure maintenance to meet goals and, simply put, taking new products live faster and reducing total cost of ownership.

Try a 14-Day Free Trial of Orkes Conductor today.

1. Overview

In this tutorial, we’ll discuss two ways to change the Wi-Fi network connection priority order in Linux.

2. Using nmcli

nmcli is a popular command-line tool in Linux. It’s used to manage various network connections, including WiFi networks. Additionally, we can use it to handle VPN connections, configure IP addresses, monitor networks, and modify route information.

2.1. Installation

Generally, nmcli is a pre-installed tool in Linux. However, if we encounter any error while opening it, we can re-install it. Let’s see how to install the nmcli tool in different Linux environments.

In Debian-based systems, we can install the NetworkManager package, which includes the nmcli tool:

$ sudo apt-get install network-manager

Furthermore, to install the nmcli tool in Arch Linux-based systems, we need to install the NetworkManager package using the pacman command:

$ sudo pacman -S networkmanager

After the successful execution of the command, let’s verify the installation status of the nmcli tool:

$ nmcli --version
nmcli tool, version 1.44.2

Hence, as we can see, we’ve installed the tool in our system.

2.2. Usage

First, let’s list all the available Wi-Fi connections using the nmcli tool:

$ nmcli connection show
NAME                UUID                          TYPE      DEVICE  
Mi 10i      946a8742-2434-4123-9f1f-1f87a93a9f6f  wifi      wlp3s0 
HomeNetwork 12345678-1234-4123-9f1f-1f87a93a9f6f  wifi      wlp3s0
eduwifi     2567854-1234-4123-9f1f-1f87a93a9f6f   wifi      wlp3s0
Openwifi    58475212-1234-4123-9f1f-1f87a93a9f6f  wifi      wlp3s0

Here, the output shows four Wi-Fi connections.

Now, we pick one Wi-Fi connection: eduwifi. The objective is to assign a high-priority value for this connection so that the system connects to eduwifi automatically.

Moreover, before we assign a high-priority value, let’s check some details of the eduwifi connection:

$ nmcli connection show eduwifi
connection.id:                          eduwifi
connection.uuid:                        2567854-1234-4123-9f1f-1f87a93a9f6f
connection.llmnr:                       -1 (default)
connection.dns-over-tls:                -1 (default)
connection.mptcp-flags:                 0x0 (default)
ipv4.route-metric:                      -1
ipv4.route-table:                       0 (unspec)
...output truncated...

Now, in order to change the priority value, we need to look for ipv4.route-metric. Let’s change the default value to 100:

$ nmcli connection modify eduwifi ipv4.route-metric 100

After the execution of the command, the priority value for the eduwifi connection is set to 100. We can verify it by running the previous command:

$ nmcli connection show eduwifi 
connection.id: eduwifi 
connection.uuid: 2567854-1234-4123-9f1f-1f87a93a9f6f  
ipv4.route-metric: 100 
...output truncated...

Furthermore, let’s activate the connection with the updated priority value:

$ nmcli connection up eduwifi

Finally, we restart the NetworkManager tool:

$ sudo systemctl restart NetworkManager

Now, we successfully set a higher priority value for the eduwifi connection. As a result, the system will connect to eduwifi automatically due to a higher priority value.

3. Using wpa_supplicant

wpa_supplicant is a Linux tool that manages wireless connections. It provides the necessary functions for the system to authenticate and connect to wireless networks securely. Additionally, it allows users to manage and configure wireless networks that use WPA/WPA2 protocols.

3.1. Installation

wpa_supplicant is a pre-installed Linux tool. However, if we encounter errors while using it, we can manually install the tool.

Let’s first install the wpa_supplicant tool in Debian-based systems using the apt command:

$ sudo apt install wpa_supplicant

Furthermore, we can utilize the pacman command to install it in Arch Linux-based systems:

$ sudo pacman -S wpa_supplicant

After executing the command, let’s verify the installation status of the tool by checking its version:

$ wpa_supplicant -v
wpa_supplicant v2.10
Copyright (c) 2003-2022, Jouni Malinen <[email protected]> and contributors

As we can see, the installation of the wpa_supplicant tool is completed.

3.2. Usage

Now, we’re ready to use the wpa_supplicant tool. First, we go to the wpa_supplicant directory and look for the wpa_supplicant.conf file:

$ ls /etc/wpa_supplicant/wpa_supplicant.conf

If we find the configuration file, we’ll open it using any Linux text editor. The configuration file contains information on all the available connections. Let’s take a look at the data the file contains:

$ sudo nano /etc/wpa_supplicant/wpa_supplicant.conf
ctrl_interface=/var/run/wpa_supplicant
network={
    ssid="Mi 10i"
    uuid=946a8742-2434-4123-9f1f-1f87a93a9f6f
    psk="password_for_Mi 10i"
    priority=1
}
network={
    ssid="HomeNetwork"
    uuid=12345678-1234-4123-9f1f-1f87a93a9f6f
    psk="password_for_HomeNetwork"
    priority=2
}
network={
    ssid="eduwifi"
    uuid=2567854-1234-4123-9f1f-1f87a93a9f6f
    psk="password_for_eduwifi"
    priority=3
}
network={
    ssid="Openwifi"
    uuid=58475212-1234-4123-9f1f-1f87a93a9f6f
    sk="password_for_Openwifi"
    priority=4
}

However, if the configuration file doesn’t exist, we can create one. Using the nmcli command, we can generate the details of the available connections and fill out the configuration file.

Now, based on our requirements, we can set the priority value for the connections in the configuration file. The system will connect to the connection with the highest priority value.

For example, let’s change the priority value of the HomeNetwork connection and set it to 100:

$ sudo nano /etc/wpa_supplicant/wpa_supplicant.conf
ctrl_interface=/var/run/wpa_supplicant
network={
    ssid="HomeNetwork"
    uuid=12345678-1234-4123-9f1f-1f87a93a9f6f
    psk="password_for_HomeNetwork"
    priority=100
}

Finally, we restart wpa_supplicant as well as NetworkManager to save the changes we made using the systemctl command:

$ sudo systemctl restart wpa_supplicant
$ sudo systemctl restart NetworkManager

Thus, we’ve updated the priority value for the HomeNetwork connection, and the system will connect to it automatically.

4. Conclusion

In this article, we explored two ways to change the Wi-Fi network connection priority order in Linux.

The nmcli tool can change the priority value of Wi-Fi connections directly from the terminal. Additionally, it’s a high-level network management tool that handles various networks, including Wi-Fi, Ethernet, and VPN.

On the other hand, the wpa-supplicant tool uses the configuration file to change the priority value of the Wi-Fi connections. Moreover, it’s a low-level network management tool that only supports wireless connections that utilize WPA/WPA2 protocols.