Saturday, December 28, 2024
Google search engine
HomeGuest BlogsInstall and Use NetworkManager (NMCLI) on Ubuntu / Debian

Install and Use NetworkManager (NMCLI) on Ubuntu / Debian

For system Admins, their daily routine revolves around configuring(provisioning), maintaining, troubleshooting, monitoring, securing, and managing servers in data centers. This guide answers the question that arises on how to manage or administer Ubuntu/Debian network interfaces using some default tools. Network Manager is a tool designed to manage networks on a machine. It has many tools that are used to manage network configurations on about all Linux distributions. This tool is the default Network managing tool on Ubuntu systems. It provides the following Network managing tools:

  1. Nmtui– this tool is run on a Linux terminal window. It is a text based network manager tool where changes can be made on a network by entering data and menu selections. It is used to perform basic network management tasks, it has many missing features that are available in NMCLI.
  2. Gnome Setting– from the Gnome desktop one can manage the network by using a Gnome network application. It provides basic network manahgement and is easy to perform configurations.
  3. nm-connection-editor– this is a graphical based network manager tool that allows one to access the NetworkManager configuration options
  4. NMCLI-normally referred to Network Command-Line Interface is a networking tool used to manage the NetworkManager and also show the status of a network connection. It is useful when one is unable to access the graphical environment or running a server edition and do network configuration changes

Network Manager is preferred due to the following features:

  • Flexible network visualization
  • Comprehensive network management
  • Built-in root cause analysis capabilities
  • Fully customizable content
  • Built-in device and interface polling capabilities
  • Single-click network troubleshooting
  • Extensive reporting capabilities
  • Multiple integration options
  • Increasingly bigger network discovery

By the end of this guide, you should be able to install and use NetworkManager NMCLI on Ubuntu / Debian System.

Pre-requisites

For this guide, ensure that your Ubuntu|Debian system is updated and upgraded as below.

sudo apt-get update && sudo apt-get upgrade

Step 1 – Install NetworkManager on Ubuntu|Debian System

By default, NetworkManager exists on Ubuntu|Debian systems, but in case you need to install it, I will demonstrate two ways to get NetworkManager installed on your Ubuntu|Debian system.

  1. Using APT
  2. Using Snap

1a) Install NetworkManager on Ubuntu|Debian System Using APT

NetworkManager is available in the default ubuntu|Debian systems’ APT repositories and can be installed as below.

sudo apt-get install network-manager

Start and enable Network Manager as below.

sudo systemctl start NetworkManager.service 
sudo systemctl enable NetworkManager.service

1b) Install NetworkManager on Ubuntu|Debian System using Snap.

NetworkManager is also available in the Snap store and can be installed on Ubuntu|Debian system with snap installed and enabled as below.

sudo apt install snapd
sudo systemctl enable --now snapd.socket
sudo ln -s /var/lib/snapd/snap /snap

Then install NetworkManager using the command:

snap install network-manager

After this installation, all the required plugs and slots will be connected automatically, Verify this as below.

snap connections network-manager

Sample Output:

Interface              Plug                                   Slot                     Notes
dbus                   network-manager:wpa                    -                        -
firewall-control       network-manager:firewall-control       :firewall-control        -
hardware-observe       network-manager:hardware-observe       :hardware-observe        -
login-session-observe  network-manager:login-session-observe  :login-session-observe   -
modem-manager          network-manager:modem-manager          :modem-manager           -
network                network-manager:network                :network                 -
network-manager        -                                      network-manager:service  -
network-manager        network-manager:nmcli                  -                        -
network-observe        network-manager:network-observe        :network-observe         -
network-setup-control  network-manager:network-setup-control  :network-setup-control   -
network-setup-observe  network-manager:network-setup-observe  :network-setup-observe   -
ppp                    network-manager:ppp                    :ppp                     -

Once the installation is complete, Network Manager will be automatically started and running in the background, check its status using the command:

$ sudo systemctl status NetworkManager.service
● NetworkManager.service - Network Manager
     Loaded: loaded (/lib/systemd/system/NetworkManager.service; enabled; vendor preset: enabled)
     Active: active (running) since Tue 2021-09-28 14:50:00 EAT; 1min ago
       Docs: man:NetworkManager(8)
   Main PID: 423 (NetworkManager)
      Tasks: 3 (limit: 2404)
     Memory: 13.6M
        CPU: 303ms
     CGroup: /system.slice/NetworkManager.service
             └─423 /usr/sbin/NetworkManager --no-daemon

Sep 28 14:50:16 debian NetworkManager[423]: <info>  [1632829816.7357] manager: NetworkManager state is now CONNECTED_LOCAL
Sep 28 14:50:16 debian NetworkManager[423]: <info>  [1632829816.7434] manager: NetworkManager state is now CONNECTED_SITE
Sep 28 14:50:16 debian NetworkManager[423]: <info>  [1632829816.7468] policy: set 'Wired connection 1' (enp0s3) as default for IPv4 routing and DNS

Step 2 – Use NetworkManager NMCLI on Ubuntu|Debian System.

NMCLI is a network utility tool that will allow us to manage network configurations. There are a couple of commands one can use to manage a network.

Modify Network Connections

Using the NMCLI to modify network connections requires us to begin with listing network connections, use the command:

nmcli connection show

Sample Output:

AME                UUID                                  TYPE      DEVICE 
Wired connection 1  ac98eab7-1ad3-4cf0-93a0-210bfe3bb7e2  ethernet  enp0s3 

Display an active connection

The above command shows all the connections, but if you want to see the active or up connections you will add the --active flag to the above command;

nmcli connection show --active

Sample Output:

NAME                UUID                                  TYPE      DEVICE 
Wired connection 1  ac98eab7-1ad3-4cf0-93a0-210bfe3bb7e2  ethernet  enp0s3

With the connection profile name, you can view additional information as below.

nmcli connection show "Wired connection 1"

Sample Output:

Install and Use NetworkManager NMCLI on UbuntuDebian System 6

Add and Modify Network Connections using NMCLI

Add a Network connection using NMCLI

Using NMCLI, one can add new connections to an Ubuntu|Debian system using the syntax below.

sudo nmcli connection add type <network-type> ifname <connection-name>

Modify an existing Network connection using NMCLI

With NMCLI, one can make adjustments, configurations to an existing network connection such as switching between static and DHCP configurations using the syntax below.

sudo nmcli connection modify <connection-id> <parameter> <value>

Now let’s see the configurations we can add and modify a connection using NMCLI. We will begin by adding a NetworkManager connection profile for our connection. For me, it’s the Ethernet connection and I will add a connection profile with the name My-New-Connection

nmcli connection add con-name My-New-Connection ifname enp0s3 type ethernet

Sample output:

Connection 'My-New-Connection' (a6b26540-4c06-4a80-a0fa-a2868e4006e5) successfully added.

With the connection profile added, we will make the below adjustments. Remember to set your own IP address, gateway, DNS server e.t.c using a similar syntax to the one I have given.

To Set automatic start of the network connection use:

nmcli connection modify My-New-Connection connection.autoconnect yes

1. Set a static IPv4 address with a /24 subnet mask

For example, we will modify the existing connection to a Static IP_Address and also change the subnet /24 without this specification, it will assign an IP with the default mask that can cause problems later.

sudo nmcli connection modify My-New-Connection ipv4.address 192.168.100.157/24

2. Set a static IPv6 address with a /64 subnet mask

nmcli connection modify My-New-Connection ipv6.addresses 2001:db8:1::1/64

3. Set an IPv4/IPv6 default gateway

##For IPv4
nmcli connection modify My-New-Connection ipv4.gateway 192.168.100.1
##For IPv6
nmcli connection modify My-New-Connection ipv6.gateway 2001:db8:1::fffe

4. Set an IPv4/IPv6 DNS server

##For IPv4
nmcli connection modify My-New-Connection ipv4.dns "192.168.100.200"
##For IPv6
nmcli connection modify My-New-Connection ipv6.dns "2001:db8:1::ffbb"

5. Set the IPv4 and IPv6 connection method to automatic

##For IPv4
nmcli connection modify My-New-Connection ipv4.method auto
##For IPv6
nmcli connection modify My-New-Connection ipv6.method auto

For the changes made to take effect, you need to activate the profile.

$ nmcli connection up My-New-Connection
connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/2)

Now check active connections.

nmcli connection show --active
Install and Use NetworkManager NMCLI on UbuntuDebian System 2

Verify the set IP_Address.

hostname -I
192.168.100.120 192.168.100.157 

As from the output, I have two IP Addresses, a primary IP and the secondary IP added by the new connection profile My-New-Connection. You can check the additional information using the ip a command as below.

Install and Use NetworkManager NMCLI on UbuntuDebian System 1

Manage Devices Using NMCLI

NMCLI utility tool can also be used to manage devices such as:

Displaying device Status

sudo nmcli -p dev status

Sample Output:

Install and Use NetworkManager NMCLI on UbuntuDebian System 1 1

Modifying a device configuration

Before you can modify a device configuration, you will first need to get the device information.

sudo nmcli device show

Sample Output:

Install and Use NetworkManager NMCLI on UbuntuDebian System 4

With the device information, you can modify a device setting using the below syntax.

$ sudo nmcli device modify <interface-name> <parameter> <value>
###OR
$ sudo nmcli dev mod <interface-name> <parameter> <value>

Remember that the above changes are temporal and can be reset to default settings using the command:

sudo nmcli dev reapply interface-name

In case you are stuck when using NMCLI device manager find help using the command:

sudo nmcli device help
Install and Use NetworkManager NMCLI on UbuntuDebian System 5

Conclusion.

That is it! We have gone through how to install and use NetworkManager NMCLI on Ubuntu / Debian System. I hope this guide was of significance to you. I assume you can now manage your network connection and network devices using NetworkManager NMCLI.

See more:

RELATED ARTICLES

Most Popular

Recent Comments