Your Search Bar For Shrewd Tips

How To Add Nmcli Connection


How To Add Nmcli Connection

If you're managing Linux networks, understanding how to add and configure network connections using nmcli—NetworkManager's command-line interface—is essential. Whether you're setting up a wired connection, Wi-Fi, VPN, or other network types, mastering nmcli allows you to efficiently manage network settings directly from the terminal. This guide will walk you through the process of adding a network connection using nmcli, covering various connection types, essential commands, and best practices to ensure seamless network configuration.

Understanding nmcli and NetworkManager

nmcli is a command-line tool for controlling NetworkManager, a service that manages network connections on many Linux distributions. It provides a straightforward way to create, modify, delete, and view network connections without using graphical interfaces. This makes it ideal for server environments, scripting, and advanced users who prefer command-line control.

Before adding new connections, ensure that NetworkManager is active and running on your system. You can verify this with the following command:

systemctl status NetworkManager

If it’s not active, start and enable it with:

sudo systemctl start NetworkManager
sudo systemctl enable NetworkManager

Basic Syntax for Adding Connections

The general syntax for adding a new connection with nmcli is:

nmcli connection add [options]

Options vary depending on the connection type (e.g., ethernet, wifi, vpn). The key parameters include:

  • type: The connection type (e.g., ethernet, wifi, vpn)
  • ifname: Interface name (e.g., eth0, wlan0)
  • con-name: A descriptive name for the connection
  • ssid: The Wi-Fi network name (for wireless connections)
  • password: The Wi-Fi password (if applicable)

Adding a Wired Ethernet Connection

To add a wired connection, follow these steps:

  1. Identify your Ethernet interface name using:
  2. nmcli device status
  3. Assuming your interface is eth0, run the following command:
  4. sudo nmcli connection add type ethernet ifname eth0 con-name "Wired Connection"

    This creates a new wired connection named "Wired Connection". For most wired networks, no additional configuration is necessary unless you need static IP settings.

    Adding a Wired Connection with Static IP

    If you want to assign a static IP address, use:

    sudo nmcli connection modify "Wired Connection" ipv4.addresses 192.168.1.100/24 ipv4.gateway 192.168.1.1 ipv4.dns 8.8.8.8 ipv4.method manual
    sudo nmcli connection up "Wired Connection"

    This configures a static IP, gateway, and DNS for the connection.

    Adding a Wi-Fi Connection

    To add a Wi-Fi connection, follow these steps:

    1. Identify your wireless interface name with:
    2. nmcli device status
    3. Scan for available Wi-Fi networks:
    4. nmcli device wifi list
    5. Connect to a Wi-Fi network by specifying the SSID and password:
    6. sudo nmcli device wifi connect "Your_SSID" password "Your_Password" con-name "My WiFi Connection"

      This command creates and activates a new Wi-Fi connection named "My WiFi Connection" connecting to the specified SSID with the provided password.

      Adding a Wi-Fi Connection with Static IP

      If needed, modify the connection to use static IP settings:

      sudo nmcli connection modify "My WiFi Connection" ipv4.addresses 192.168.1.50/24 ipv4.gateway 192.168.1.1 ipv4.dns 8.8.8.8 ipv4.method manual
      sudo nmcli connection up "My WiFi Connection"

      Adding VPN Connections

      For VPNs, nmcli supports various VPN plugins like OpenVPN, PPTP, and IPsec. Here's a basic example for adding an OpenVPN connection:

      sudo nmcli connection import type openvpn file /path/to/your/openvpn-config.ovpn

      You can also create a VPN connection manually:

      sudo nmcli connection add type vpn vpn-type openvpn con-name "My VPN" -- \
          --config /path/to/your/openvpn-config.ovpn

      Replace the path with your actual VPN configuration file. You can modify VPN credentials through nmcli as needed.

      Adding Custom Connections with Specific Parameters

      Sometimes, you need to define custom parameters for your connection. Here's an example for a Wi-Fi connection with specific security settings:

      sudo nmcli connection add type wifi ifname wlan0 con-name "Secure WiFi" ssid "Your_SSID"
      sudo nmcli connection modify "Secure WiFi" wifi-sec.key-mgmt wpa-psk
      sudo nmcli connection modify "Secure WiFi" wifi-sec.psk "Your_Password"
      sudo nmcli connection up "Secure WiFi"

      This process creates a secure Wi-Fi connection with WPA-PSK security.

      Managing and Verifying Connections

      After adding connections, it's important to verify and manage them:

      • List all available connections:
      nmcli connection show
    7. Activate a specific connection:
    8. nmcli connection up "Connection_Name"
    9. Deactivate a connection:
    10. nmcli connection down "Connection_Name"
    11. Delete a connection:
    12. nmcli connection delete "Connection_Name"

      Checking connection status can be done with:

      nmcli device status

      Best Practices and Troubleshooting

      When adding and managing connections via nmcli, keep these best practices in mind:

      • Always verify your interface names and network details before executing commands.
      • Use descriptive connection names for easier management.
      • Back up your connection configurations before making significant changes:
      nmcli connection export "Connection_Name" backup-file.nma
    13. If you encounter issues, check the status of NetworkManager and logs:
    14. systemctl status NetworkManager
      journalctl -u NetworkManager

      Conclusion

      Mastering nmcli for adding and managing network connections provides Linux users with powerful control over their network environment. Whether configuring wired connections, Wi-Fi networks, or VPNs, nmcli offers a flexible, scriptable, and efficient way to handle network setups directly from the terminal. By understanding key commands and best practices, you can ensure reliable and optimized network connectivity in any Linux setting. With a little practice, managing network connections via nmcli becomes an intuitive and invaluable skill for system administrators, developers, and power users alike.


Disclaimer: Articles are written by Humans, AI or Both. Verify Important information.

Shrewdnia

Shrewdnia

Shrewdnia is a destination for curious minds seeking clarity, knowledge, and informed perspectives. Through insightful articles and practical guides our passionate team explores a wide range of topics designed to help readers understand the world around them, make smarter decisions, and stay informed in an ever-changing landscape.


💡 Every question sparks discovery, and every perspective enriches the conversation. Share your thoughts and insights in the comments 👇

Back to blog

Leave a comment

JOIN THE SHREWDNIA COMMUNITY FORUM

What do you think?

Have an opinion, experience, or question about this topic? Join the Shrewdnia Forum and share your thoughts with other readers.

Join the Forum →