Ubuntu Network Interface IP Address Change Guide

Ubuntu Network Interface IP Address Change Guide

Applicable Environment

  • System: Ubuntu

  • Network Interface: ens160

  • Current IP Address: 192.168.2.7

  • Target IP Address: 192.168.2.19

Installing nmcli (NetworkManager Tool)

nmcli is the command-line tool for NetworkManager and is typically pre-installed on most Ubuntu systems. If it is not installed, follow the steps below:

Step 1: Check if NetworkManager is Installed

Run the following command to check if NetworkManager is installed:

nmcli --version

If it displays a version number, such as nmcli tool, version x.x.x, it means NetworkManager is installed.

Using NetworkManager to Change IP Address

Step 1: Check if NetworkManager Manages the Network Interface

  1. Check the network interface status:

     nmcli dev status
    
    • Confirm that ens160 is in the connected or disconnected state, not unmanaged.

    • If the state is unmanaged, enable NetworkManager to manage the interface:

        sudo nmcli dev set ens160 managed yes
      
  2. Restart the NetworkManager service to ensure the configuration takes effect:

     sudo systemctl restart NetworkManager
    

Step 2: Configure Static IP Address

  1. Add or modify a static IP configuration:

     sudo nmcli connection add type ethernet con-name StaticIP ifname ens160 ipv4.addresses 192.168.2.19/24 ipv4.gateway 192.168.2.1 ipv4.dns "8.8.8.8,8.8.4.4" ipv4.method manual
    
    • Parameter Explanation:

      • con-name: Connection name, customizable (e.g., StaticIP).

      • ifname: Network interface name (e.g., ens160).

      • ipv4.addresses: Target static IP and subnet mask.

      • ipv4.gateway: Default gateway.

      • ipv4.dns: DNS server addresses.

  2. Activate the newly configured network connection:

     sudo nmcli connection up StaticIP
    
  3. Verify if the IP address has been successfully changed:

     ip addr show ens160
    

Common Issue

Issue : unmanaged Status

  • Cause: The network interface is not managed by NetworkManager.

  • Solution:

    1. Check the NetworkManager configuration file:

       sudo nano /etc/NetworkManager/NetworkManager.conf
      

      Ensure the following content is either absent or correctly configured:

       [keyfile]
       unmanaged-devices=none
      
    2. Restart NetworkManager:

       sudo systemctl restart NetworkManager
      
  • Alternative Solution:

    1. Check Netplan configuration:

       cat /etc/netplan/*.yaml
      

      Ensure the file contains the following content (and modify the interface name if necessary):

       network:
         version: 2
         renderer: NetworkManager
      
    2. Apply the configuration:

       sudo netplan apply