Configure Network Settings Using Network Manager in Linux | Baeldung on Linux
If you have a few years of experience in the Linux ecosystem, and you’re interested in sharing that experience with the community, have a look at our Contribution Guidelines .
Mục Lục
1. Overview
In this tutorial, we’ll discuss the Network Manager package. Initially, we begin with an overview of the package and how we can install it. After that, we focus on the nmcli command. In practice, this is the Network Manager command-line interface.
2. Network Manager Components
Network Manager is a set of tools for configuring the network devices of a Linux machine. Crucially, it features the automatic configuration of network devices in a Linux machine through the Network Manager service. Of course, we can also manually configure in many ways.
First, there’s nmcli, the command-line interface of the Network Manager. Next, there’s a text-based user interface that runs in the terminal. Moreover, the package has a graphical user interface. Finally, there’s an API for third-party access.
3. Installation and Execution
To begin with, let’s install Network Manager. Of course, some Linux distributions come with the Network Manager package pre-installed. It’s usually in the form of a daemon that runs on startup.
3.1. Installation
In any event, we can install the Network Manager package via a package manager like apt or yum. For example, we can simply run the following command in Ubuntu:
$ sudo apt-get install network-manager
Note that the package name in Ubuntu is currently network-manager. Of course, it may differ between Linux distributions. For instance, the package is called NetworkManager in Red Hat Linux.
3.2. Service Execution
Importantly, we must ensure the service is up and running. Therefore, we may check with the systemctl status command under Ubuntu:
$ systemctl status NetworkManager
● NetworkManager.service - Network Manager
Loaded: loaded (/lib/systemd/system/NetworkManager.service; enabled; vendor preset: enabled)
Active: active (running) since Wed 2022-05-25 15:18:22 EEST; 42min ago
As a result, we see the active (running) status in the command’s output.
3.3. Relation With Other Network Services
Indeed, Network Manager may coexist with other network tools in a Linux system. However, we may not enable its service while other such services like networkd are running. Because of this, it’s best to stop or disable them beforehand to avoid conflicts.
3.4. Configuration Files
The configuration files of Network Manager reside in /etc/NetworkManager. Further, connection settings are stored in the system-connections subfolder. Most often, we’ll use the nmcli command to configure our network devices. Since it keeps the changes in configuration files, chances are we won’t have to edit any manually.
4. Displaying the Networking Status of a Linux Machine
It’s helpful to get a list of the network devices, connections, and the general networking status. For instance, network devices consist primarily of network cards, while connections are configurations assigned to a network device.
In practice, a connection deals with IP and link-layer details. For example, there are IP addresses, DNS servers, and gateway settings.
4.1. Displaying the List of All Network Devices
Let’s get the list of all network devices in our Linux machine with nmcli device:
$ nmcli device
DEVICE TYPE STATE CONNECTION
wlp5s0 wifi connected MY_WIRELESS_CONN
enp0s3 ethernet unavailable --
lo loopback unmanaged --
Here, we see three devices in the output. The first one is an up and running Wi-Fi network adapter. The second one is an inactive ethernet adapter. On the last line, we see the loopback network interface.
4.2. Displaying the IP Address of a Device
The command nmcli device show prints properties like the IP Address of a device:
$ nmcli device show wlp5s0
GENERAL.DEVICE: wlp5s0
GENERAL.TYPE: wifi
GENERAL.HWADDR: 00:18:DE:CE:4A:02
GENERAL.MTU: 0
GENERAL.STATE: 100 (connected)
GENERAL.CONNECTION: MY_WIRELESS_CONN 1
GENERAL.CON-PATH: /org/freedesktop/NetworkManager/ActiveConnection/0
IP4.ADDRESS[1]: 192.168.2.8/24
IP4.GATEWAY: 192.168.2.1
IP4.ROUTE[1]: dst = 169.254.0.0/16, nh = 0.0.0.0, mt = 1000
IP4.DNS[1]: 192.168.2.1
IP4.DOMAIN[1]: station
IP6.ADDRESS[1]: fe80::50d0:5ec7:546f:d9f0/64
IP6.GATEWAY:
Here, we’ve printed data about the device wlp5s0. In addition to the IP address, the command prints many other properties. Examples are the DNS server, MAC address, and gateway.
4.3. Displaying the List of Existing Connections
We can get the existing network connections with nmcli connection:
$ nmcli connection
NAME UUID TYPE DEVICE
MY_WIRELLESS_CONN db1479d2-3ddf-42de-8d54-754e17a4d93b 802-11-wireless wlp5s0
The command outputs one connection paired with the wireless adapter wlp5s0.
4.4. Displaying the Properties of a Connection
We can also print the properties of a connection:
$ nmcli connection show id my-ethernet
connection.id: my-ethernet
connection.uuid: b07d9672-0b01-4e8e-a7ff-b196ee8d95c0
connection.stable-id: --
connection.type: 802-3-ethernet
connection.interface-name: --
connection.autoconnect: yes
connection.autoconnect-priority: 0
connection.autoconnect-retries: -1 (default)
connection.multi-connect: 0 (default)
connection.auth-retries: -1
connection.timestamp: 0
...
We used id my-ethernet to get the properties of that connection. For brevity, we exclude part of the output. Note that we can print all connections if we don’t set a specific id.
4.5. Displaying the Status of Wireless Adapters
The nmcli radio command prints the status of wireless adapters:
$ nmcli radio
WIFI-HW WIFI WWAN-HW WWAN
enabled enabled enabled enabled
As shown above, we observe that the wireless WAN (WWAN) and Wi-Fi adapters are enabled.
5. Configuring an Ethernet Adapter
Whereas WiFi is widely-used at home, ethernet adapters continue to be popular at work. Also, our computer may receive its network settings via DHCP from a work server. On the other hand, if there is no DHCP server, we have to set the network details like IP address, DNS Server, and default gateway on our own.
In all cases, we use the connection (shortened to con or just c) object of the nmcli command.
5.1. Configuring an Ethernet Adapter With a Static IP Address
We configure an ethernet adapter by creating a new connection with the nmcli connection add command:
$ nmcli connection add type ethernet ifname enp7s8 con-name my_ethernet ip4 192.168.2.138/24 gw4 192.168.2.1
Connection 'my_ethernet' (514c394d-0fe2-4c3c-80f5-693ba65b1db3) successfully added.
In the above example, we set several options.
First, we set the connection type to ethernet. Second, we set the interface name after ifname. Third, we set con-name to the name of our new connection. Next, ip4 specifies the IP address. Finally, the default IPv4 gateway is after gw4. Alternatively, we may set IPv6 details with the ip6 and gw6 options.
5.2. Configuring an Ethernet Adapter With a Dynamic IP Address
If a DHCP server is in place, we may create a new connection with a dynamic IP. Like in the previous section, we do it with the nmcli connection add command, but with fewer details:
$ sudo nmcli connection add type ethernet ifname enp7s8 con-name my_ethernet
Connection 'my_ethernet' (cd4ff4e0-ade3-47a4-8e1b-16df2ce9f366) successfully added.
Notice that we didn’t set any IP address or default gateway.
5.3. Modifying a Connection
We use the modify sub-command to update the properties of a connection:
$ sudo nmcli connection modify my_ethernet ipv4.DNS 192.168.2.1
Here, we set the DNS server to the value 192.168.2.1. We may use the command nmcli connection show to get the list of available settings.
5.4. Enabling and Disabling Connections
Enabling and disabling connections is an important task of network configuration. So, let’s disable the connection we created:
$ nmcli connection down my_ethernet
Connection 'my_ethernet' successfully deactivated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/4)
As a result, we disabled the connection with the sub-command down. On the other hand, to enable it again, we may use the sub-command up:
$ nmcli connection up my_ethernet
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/5)
As we can see, we enabled the connection with id my_ethernet. The D-Bus active path appears in the command’s output. That is to say, the D-BUS API enables the use of our connection by other applications.
6. Configuring a Wireless Device
Still, many home users connect to the Internet through the Wi-Fi of their DSL router. In fact, the nmcli command is indeed helpful in that setup.
To do that, we can break the process into two tasks:
-
list the wireless networks in our area
-
connect to one of the listed wireless networks
Let’s go through both steps.
6.1. Listing Wireless Networks
First, we list wireless networks in our area with the nmcli device wifi list command:
$ nmcli device wifi list
* SSID MODE CHAN RATE SIGNAL BARS SECURITY
MY_WIRELESS_NET Infra 11 54 Mbit/s 100 ▂▄▆█ WPA1 WPA2
ANOTHER_WIRELLESS_NET Infra 52 54 Mbit/s 100 ▂▄▆█ WPA1 WPA2
YET_ANOTHER_WIR_NET Infra 6 54 Mbit/s 55 ▂▄__ WPA2
As can be seen, we get a list of networks. In it, each record contains the SSID, rate, signal power, and type of security among others.
6.2. Connecting to a Wireless Network
We may then connect to a wireless network with the nmcli device wifi connect command:
$ nmcli device wifi connect MY_WIRELESS_NET password 8ehdxhre5kkhb6g6
Device 'wlp5s0' successfully activated with 'a7c8fbf5-3e7d-456c-921b-d739de0e3c79'.
Here, we connect to the network with SSID MY_WIRELESS_NET.
7. Conclusion
In this article, we talked about the Network Manager package. First, we started with a brief description of the Network Manager. After that, we went through the installation process. Finally, we focused on the command-line interface of Network Manager and showed some common examples.
Authors Bottom
If you have a few years of experience in the Linux ecosystem, and you’re interested in sharing that experience with the community, have a look at our Contribution Guidelines.