Create Multiple NetworkManager Connection Profiles for the Same Network Interface on Linux
If you have a single network interface on your Linux system that you want to use with different IP configurations for various purposes, NetworkManager makes this process simple. This article will guide you on how to configure multiple NetworkManager connection profiles for the same network interface on Linux and how to switch between them when needed.
Table of Contents
- Finding the Current IP Addressing Information of the Network Interface on Linux
- Creating Multiple NetworkManager Connection Profiles Using Nmcli
- Switching Between NetworkManager Connection Profiles Using Nmcli
- Automatically Activating a NetworkManager Connection Profile Using Nmcli
- Managing Multiple Profiles Efficiently
- Affordable VPS Hosting With Dracula Servers
- Troubleshooting Common Issues
- Best Practices for Network Configuration
- Wrap up
Finding the Current IP Addressing Information of the Network Interface on Linux
Before creating multiple NetworkManager connection profiles, it’s useful to know the current IP addressing information of the network interface. This information helps in setting static IP addresses, DHCP configurations, and DNS server settings for different profiles.
To find the name of the network interface, run:
$ sudo nmcli device
For instance, if the network interface name is ens160
and the active profile is Wired connection 1
, take note of these details.
To find the current IP configuration of ens160
, use:
$ sudo nmcli -f GENERAL.DEVICE,GENERAL.CONNECTION,IP4.ADDRESS,IP4.GATEWAY,IP4.DNS device show ens160
You’ll see information like IP address, subnet mask, gateway, and DNS server addresses.
Creating Multiple NetworkManager Connection Profiles Using Nmcli
You can create a new NetworkManager connection profile by cloning an existing one and modifying the necessary parameters.
To clone an existing profile (Wired connection 1
) and create a new profile (FixedLAN1
), run:
$ sudo nmcli connection clone "Wired connection 1" "FixedLAN1"
To set a static IP for FixedLAN1
, modify the profile:
$ sudo nmcli connection modify "FixedLAN1" ipv4.method manual ipv4.addresses 192.168.1.10/24 ipv4.gateway 192.168.1.2 ipv4.dns 192.168.1.2 connection.autoconnect no
To create another profile (FixedLAN2
) with a different static IP:
$ sudo nmcli connection add con-name "FixedLAN2" ifname ens160 type ethernet autoconnect no ipv4.method manual ipv4.addresses 192.168.1.20/24 ipv4.gateway 192.168.1.2 ipv4.dns 192.168.1.2
For a DHCP profile (DynamicLAN
):
$ sudo nmcli connection add con-name "DynamicLAN" ifname ens160 type ethernet autoconnect no ipv4.method auto
Switching Between NetworkManager Connection Profiles Using Nmcli
To switch between profiles, use the nmcli connection up
command.
To activate FixedLAN2
:
$ sudo nmcli connection up "FixedLAN2"
To verify, check the connection status and IP configuration:
$ sudo nmcli connection
$ ip a
You can similarly activate FixedLAN1
and DynamicLAN
:
$ sudo nmcli connection up "FixedLAN1"
$ sudo nmcli connection up "DynamicLAN"
Automatically Activating a NetworkManager Connection Profile Using Nmcli
To set a profile to auto-connect at boot, modify its autoconnect property.
For FixedLAN1
:
$ sudo nmcli connection modify "FixedLAN1" autoconnect yes
$ sudo nmcli connection modify "FixedLAN2" autoconnect no
$ sudo nmcli connection modify "DynamicLAN" autoconnect no
To enable autoconnect for DynamicLAN
:
$ sudo nmcli connection modify "DynamicLAN" autoconnect yes
$ sudo nmcli connection modify "FixedLAN1" autoconnect no
$ sudo nmcli connection modify "FixedLAN2" autoconnect no
After rebooting, the specified profile should be active:
$ sudo nmcli connection
$ ip a
Managing Multiple Profiles Efficiently
When managing multiple profiles, it’s important to keep track of the profiles and their configurations. Use meaningful names for each profile and document their purposes and configurations.
- Use
nmcli connection show
to list all profiles and their details. - Regularly review and update profiles to match current network requirements.
- Backup profiles using
nmcli connection export
for disaster recovery.
Affordable VPS Hosting With Dracula Servers
Looking for reliable and budget-friendly Virtual Private Server (VPS) hosting? Look no further than Dracula Servers. Dracula Servers offers a range of VPS hosting plans tailored to meet diverse needs. With competitive pricing, robust performance, and a user-friendly interface, it’s an excellent choice for individuals and businesses alike.
Explore the Dracula Servers website to discover hosting solutions that align with your requirements and take your online presence to new heights with their affordable and efficient VPS hosting services.
Visit Dracula Servers and experience reliable VPS hosting without breaking the bank
Troubleshooting Common Issues
When working with multiple NetworkManager connection profiles, you might encounter some common issues. Here are a few potential problems and their solutions:
Connection Profile Not Activating
If a connection profile does not activate as expected, ensure that:
- The profile name is correct.
- The network interface name is correct.
- The profile is configured with the correct IP settings and gateway.
- The profile is not disabled or deleted accidentally.
Solution:
$ sudo nmcli connection show
$ sudo nmcli connection up "ProfileName"
IP Address Conflict
If multiple profiles use the same static IP address, it can cause IP address conflicts.
Solution:
Ensure each profile uses a unique IP address. Check the current IP configuration:
$ ip a
DNS Resolution Issues
If a profile has incorrect DNS settings, it can lead to DNS resolution problems.
Solution:
Verify and update the DNS settings for the profile:
$ sudo nmcli connection modify "ProfileName" ipv4.dns "8.8.8.8 8.8.4.4"
Profile Not Auto-Connecting
If a profile does not auto-connect on boot, ensure the autoconnect property is enabled.
Solution:
$ sudo nmcli connection modify "ProfileName" connection.autoconnect yes
Network Interface Not Recognized
If the network interface is not recognized, ensure it is enabled and properly connected.
Solution:
$ sudo nmcli device status
$ sudo nmcli device connect "InterfaceName"
Advanced Network Configuration
For advanced users, NetworkManager provides the flexibility to configure complex network setups such as VLANs, bridging interfaces, and bonding interfaces. Here’s how you can achieve these configurations:
Configuring VLANs
Virtual LANs (VLANs) allow you to segment network traffic. NetworkManager supports VLAN tagging.
Creating a VLAN:
$ sudo nmcli connection add type vlan con-name VLAN100 ifname ens160 dev ens160 id 100 ip4 192.168.100.1/24
This creates a VLAN with ID 100 on the ens160
interface and assigns it an IP address.
Bridging Interfaces
A network bridge allows you to connect two or more network segments, functioning like a virtual switch.
Creating a Bridge:
$ sudo nmcli connection add type bridge con-name br0 ifname br0
$ sudo nmcli connection add type ethernet con-name ens160-slave ifname ens160 master br0
$ sudo nmcli connection add type ethernet con-name ens192-slave ifname ens192 master br0
This creates a bridge named br0
and adds the ens160
and ens192
interfaces as bridge ports.
Setting Up Bonding Interfaces
Network bonding combines multiple network interfaces into a single logical interface for redundancy or increased throughput.
Creating a Bond:
$ sudo nmcli connection add type bond con-name bond0 ifname bond0 mode active-backup
$ sudo nmcli connection add type ethernet con-name bond0-slave1 ifname ens160 master bond0
$ sudo nmcli connection add type ethernet con-name bond0-slave2 ifname ens192 master bond0
This sets up an active-backup bond named bond0
with ens160
and ens192
as slaves.
Scripting and Automation
Automating network profile switching can save time and reduce manual errors. You can use scripts or cron jobs to manage this process.
Using Shell Scripts
Create a shell script to switch between network profiles based on your requirements.
Example Script to Switch Profiles:
#!/bin/bash
# Switch to FixedLAN1 profile
sudo nmcli connection up "FixedLAN1"
# Verify the active connection
nmcli connection show --active
Save this script as switch_profile.sh
and make it executable:
$ chmod +x switch_profile.sh
Run the script as needed:
$ ./switch_profile.sh
Automating with Cron Jobs
Cron jobs can automate tasks at scheduled times. You can set up a cron job to switch network profiles at specific intervals.
Example Cron Job:
# Edit the crontab
$ crontab -e
# Add a cron job to switch to FixedLAN1 at 8 AM daily
0 8 * * * /path/to/switch_profile.sh
This cron job will execute the switch_profile.sh
script every day at 8 AM.
Using Systemd Timers
Systemd timers provide more flexibility and accuracy compared to cron jobs.
Creating a Systemd Timer:
- Create a service file
/etc/systemd/system/switch_profile.service
:[Unit] Description=Switch Network Profile [Service] ExecStart=/path/to/switch_profile.sh
- Create a timer file
/etc/systemd/system/switch_profile.timer
:[Unit] Description=Run Switch Profile Script at 8 AM Daily [Timer] OnCalendar=*-*-* 08:00:00 Persistent=true [Install] WantedBy=timers.target
- Enable and start the timer:
$ sudo systemctl enable switch_profile.timer $ sudo systemctl start switch_profile.timer
By leveraging scripts, cron jobs, and systemd timers, you can automate the process of switching network profiles, making network management more efficient and less error-prone.
Best Practices for Network Configuration
To effectively manage network configurations, consider the following best practices:
Naming Conventions
Use clear and descriptive names for your connection profiles. This makes it easier to identify and manage them.
- Example:
OfficeLAN
,HomeWiFi
,VPNWork
.
Security Considerations
- Ensure all network profiles have appropriate security settings.
- Use strong passwords and encryption for Wi-Fi connections.
- Regularly update and patch your system to prevent vulnerabilities.
Regular Updates
Keep your NetworkManager and related tools updated to benefit from the latest features and security improvements.
$ sudo apt update
$ sudo apt upgrade
Backup and Restore Profiles
Regularly back up your connection profiles. This can save time in case of system reinstallation or migration.
$ sudo nmcli connection export "ProfileName" ~/backup/
$ sudo nmcli connection import type [type] file ~/backup/ProfileName.nmconnection
Avoiding IP Conflicts
Ensure that static IP addresses assigned to different profiles do not overlap to prevent conflicts.
- Use IP address management tools or document IP allocations.
Documentation
Maintain documentation for each network profile, including its purpose, settings, and any specific configurations. This is useful for troubleshooting and auditing.
By following these best practices, you can ensure a more organized, secure, and efficient network configuration management on your Linux system.
Wrap up
In this article, we demonstrated how to create multiple NetworkManager connection profiles for the same network interface on Linux using the nmcli
tool. We also showed how to switch between profiles and set a profile to auto-connect at boot. By effectively managing network profiles, you can easily switch between different network configurations as needed, enhancing your system’s flexibility and functionality.
Check out More Linux Tutorials Here!