Managing Network Connections with `nmcli connection` (with examples)

Managing Network Connections with `nmcli connection` (with examples)

NetworkManager is a powerful tool for managing network interfaces on Linux systems, and its command-line tool, nmcli, offers a robust way to handle your network configurations. One of the core functionalities of nmcli is the connection subcommand, which facilitates seamless management of network connections. This article explores various use cases of nmcli connection, illustrating how it can be utilized to manage your network connections effectively and efficiently.

Use case 1: List all NetworkManager connections

Code:

nmcli connection

Motivation:
Listing all active and inactive network connections on a system is crucial for system administrators to monitor and manage network configurations effectively. Knowing the available connections, along with their properties like UUID, type, and associated device, allows for better decision-making and troubleshooting.

Explanation:

  • nmcli: The main command-line interface for NetworkManager.
  • connection: A subcommand to manage network connections.

Example Output:

NAME                  UUID                                  TYPE      DEVICE
Wired connection 1    12345678-1234-1234-1234-123456789abc  ethernet  eth0
Wi-Fi Network         87654321-4321-4321-4321-abc123456789  wifi      wlan0

Use case 2: Activate a connection

Code:

nmcli connection up uuid 12345678-1234-1234-1234-123456789abc

Motivation:
Activating a specific network connection is essential when you want to switch from one network to another or re-establish a previously disconnected network for accessing resources or services. This capability helps maintain network connectivity, especially in dynamic environments where network conditions change frequently.

Explanation:

  • nmcli: The primary tool for network management tasks.
  • connection up: Command to bring a network connection to an active state.
  • uuid: Specifies the unique identifier for the connection you want to activate.
  • 12345678-1234-1234-1234-123456789abc: An example UUID representing the specific connection to activate.

Example Output:

Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/1)

Use case 3: Deactivate a connection

Code:

nmcli connection down uuid 12345678-1234-1234-1234-123456789abc

Motivation:
Deactivating a connection is necessary when you need to disconnect from a network for security reasons, to reduce network load, or to troubleshoot networking issues without affecting other connections. It is particularly useful in multi-network environments where disconnection might mitigate interference or data leakage.

Explanation:

  • nmcli connection down: Used to deactivate or disconnect a network connection.
  • uuid: Indicates the unique identifier of the connection you wish to deactivate.
  • 12345678-1234-1234-1234-123456789abc: The specific UUID of the connection to bring down.

Example Output:

Connection 'Wired connection 1' successfully deactivated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/1)

Use case 4: Create an auto-configured dual stack connection

Code:

nmcli connection add ifname eth0 type ethernet ipv4.method auto ipv6.method auto

Motivation:
Creating an automatically configured connection is particularly advantageous in environments where network settings like IP address, gateway, and DNS need to be dynamically allocated. This is common in enterprise and cloud environments that leverage DHCP for both IPv4 and IPv6, facilitating hassle-free network configuration and deployment.

Explanation:

  • nmcli connection add: Initiates the creation of a new network connection.
  • ifname: Specifies the network interface name.
  • eth0: The name of the network interface to configure.
  • type ethernet: Defines the type of network connection.
  • ipv4.method auto: Specifies that the IPv4 configuration should be automatically obtained.
  • ipv6.method auto: Specifies that the IPv6 configuration should also be automatically obtained.

Example Output:

Connection 'ethernet-eth0' (UUID e321bc4a-5281-447a-8522-488503c0e5b3) successfully added.

Use case 5: Create a static IPv6-only connection

Code:

nmcli connection add ifname eth0 type ethernet ip6 2001:db8::2/64 gw6 2001:db8::1 ipv6.dns 2001:db8::1 ipv4.method ignore

Motivation:
Creating a static IPv6 network connection is imperative in networks where stable IP addresses are necessary for services that require consistent identity on the network or for security reasons where predictability of network assignments is crucial. This is a common requirement in servers and network hardware configuration.

Explanation:

  • nmcli connection add: Creates a new network connection profile.
  • ifname: Specifies the interface to which the connection belongs.
  • eth0: The relevant network interface.
  • type ethernet: Designates the connection type.
  • ip6 2001:db8::2/64: Assigns a static IPv6 address to the connection.
  • gw6 2001:db8::1: Defines the gateway for the IPv6 network.
  • ipv6.dns 2001:db8::1: Sets the DNS server for the IPv6 configuration.
  • ipv4.method ignore: Instructs the system to ignore any IPv4 configuration.

Example Output:

Connection 'static-ipv6-eth0' (UUID 9f4a1c72-9e9f-11eb-a8b3-0242ac130003) successfully added.

Use case 6: Create a static IPv4-only connection

Code:

nmcli connection add ifname eth0 type ethernet ip4 10.0.0.7/8 gw4 10.0.0.1 ipv4.dns 10.0.0.1 ipv6.method ignore

Motivation:
Similar to IPv6 static configurations, static IPv4 settings are crucial for infrastructure that requires a consistent IP address for effective routing, firewall rules, and service access. This is especially significant in legacy systems or isolated network environments where IPv4 is more commonly used.

Explanation:

  • nmcli connection add: Initializes a new connection configuration.
  • ifname eth0: Specifies the interface to bind the connection to.
  • type ethernet: Sets the type as Ethernet.
  • ip4 10.0.0.7/8: Defines a static IPv4 address and subnet mask.
  • gw4 10.0.0.1: Specifies the IPv4 gateway.
  • ipv4.dns 10.0.0.1: Assigns the DNS server for the connection.
  • ipv6.method ignore: Disregards any IPv6 configurations.

Example Output:

Connection 'static-ipv4-eth0' (UUID c839d78c-3f78-4a6e-ba28-4e1b432f429e) successfully added.

Use case 7: Create a VPN connection using OpenVPN from an OVPN file

Code:

nmcli connection import type openvpn file path/to/vpn_config.ovpn

Motivation:
Virtual Private Networks (VPNs) are essential for securing communications over potentially insecure networks. Using OpenVPN via nmcli allows system administrators to easily integrate secure, encrypted connections into their network management practices, facilitating remote work and data protection.

Explanation:

  • nmcli connection import: Used to import a configuration for a new connection.
  • type openvpn: Specifies the VPN type as OpenVPN.
  • file path/to/vpn_config.ovpn: Path to the .ovpn configuration file to be used for creating the connection.

Example Output:

Connection 'myVPN' (UUID 23456789-abcd-1234-efgh-567890abcdef) successfully added.

Conclusion:

Managing network connections using nmcli connection offers a powerful way to administer a system’s network interfaces, whether dealing with simple configurations or complex VPN setups. Each of its use cases allows for efficient, precise control over network configurations, improving both the security and functionality of networked environments. Understanding and leveraging these capabilities can significantly enhance both individual and enterprise-level network management.

Related Posts

How to Use the Command 'ppmtoterm' (with examples)

How to Use the Command 'ppmtoterm' (with examples)

The ‘ppmtoterm’ command is a versatile utility within the Netpbm library that allows users to convert portable pixmap (PPM) images into ANSI ISO 6429 ASCII images.

Read More
How to use the command 'printenv' (with examples)

How to use the command 'printenv' (with examples)

The printenv command is a useful tool for interacting with environment variables in Unix-based systems.

Read More
Exploring the 'dotnet ef' Command (with examples)

Exploring the 'dotnet ef' Command (with examples)

The dotnet ef command is a powerful tool provided with the Entity Framework Core, allowing developers to perform design-time development tasks efficiently.

Read More