Adding an IPv4 Address

Adding an IPv4 Address

To add an IPv4 address,

  1. Login to the instance.
  2. Use the following command to get the details of the existing public IPv4 address(es) of the instance:
    # ip -4 -d addr show
    E.g: The IP address for your instance ( CentOS-7 distribution) is 97.64.98.26 with the prefix 24. You will receive an output similar to this:
    2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    inet 97.64.98.26/24 brd 206.246.84.255 scope global dynamic eth0
    valid_lft 85681sec preferred_lft 85681sec
  3. Add the new IP address via the following command "ip addr add <ip address/prefix> dev <interface name>".
    # ip addr add 97.64.98.36/24 dev eth0
  4. You should be able to PING or login to the instance via SSH using this new IPv4.
  5. Make this change persistent as per the distribution, refer to the distribution changes:

CentOS-7

  1. Open and edit the file "/etc/sysconfig/network-scripts/ifcfg-eth0":
    # vi /etc/sysconfig/network-scripts/ifcfg-eth0
  2. Add the below contents:
                BOOTPROTO=static 
    GATEWAY=97.64.98.1
    IPADDR0=97.64.98.26
    PREFIX0=24
    IPADDR1=97.64.98.36
    PREFIX1=24

    Comments:
    The GATEWAY address is available in the instance networking section
    IPADDR0 is the main IPv4 of the instance
    IPADDR1 is the newly added IPv4 to the instance
    Do not make any changes to IPv6 address related entries

CentOS Stream 8, CentOS Stream 9, Fedora, Rocky Linux and Alma Linux

  1. Make the Primary IP assigned to your instance as static.
    Note: Below commands need to be executed only once, subsequent configurations to assign IPv4 addresses can be done directly.
                #  nmcli con mod "System eth0"  ipv4.addresses 97.64.98.26/24
    # nmcli con mod "System eth0" ipv4.gateway 97.64.98.1
    # nmcli con mod "System eth0" ipv4.method manual
    # nmcli con mod "System eth0" connection.autoconnect yes

    Comments:
    The GATEWAY address is available in the instance networking section

  2. Add the additional IPv4 address.
            # nmcli con modify "System eth0" +ipv4.addresses 97.64.98.36/24
        
  3. Restart the instance.

OpenSUSE

  1. Open and edit the file "/etc/sysconfig/network/ifcfg-eth0" by adding the default and new IPv4 address entries.
                # vi /etc/sysconfig/network/ifcfg-eth0
            
                    # Created by cloud-init on instance boot automatically, do not edit.
    #
    BOOTPROTO=dhcp
    DHCLIENT6_MODE=info
    LLADDR=fa:16:3e:65:8b:5a
    MTU=1500
    STARTMODE=auto
    IPADDR_0=97.64.98.26/24
    IPADDR_1=97.64.98.36/24
    Note - Here, you should add the default/main IPv4 address as IPADDR_0 along with the new IPv4 address as IPADDR_1.

Ubuntu

Follow the steps below for the given distributions:

Ubuntu22.04 LTS, 20.04 LTS, 18.04 LTS, 16.04 LTS
  1. Make a copy of the "/etc/netplan/50-cloud-init.yaml" file as "multiple-ips-init.yaml".
                # cp /etc/netplan/50-cloud-init.yaml /etc/netplan/multiple-ips-init.yaml
            
  2. Open the file " /etc/netplan/multiple-ips-init.yaml" and add the address entries as below:
    # vi /etc/netplan/multiple-ips-init.yaml
                network:
       version: 2
       ethernets:
         ens3:
            dhcp4: false
            dhcp6: true
            match:
                macaddress: fa:16:3e:d9:58:50
             mtu: 1500
            set-name: ens3
            addresses: [97.64.98.26/24,97.64.98.36/24]
            gateway4: 97.64.98.1
    Note - Here, you should add the default/main IPv4 address along with the new IPv4 address, set dhcp4 to false and provide the gateway address.
  3. Apply the net plan to have multiple IP addresses.
    # netplan apply

Debian & Ubuntu 16.04

Follow the steps below for the given distributions:

Debian11,10,9
Ubuntu16.04 LTS
  1. Make a copy of the file "/etc/network/interfaces.d/50-cloud-init.cfg" as "multiple-ips-init.cfg".
            # cp /etc/network/interfaces.d/50-cloud-init.cfg \  
    /etc/network/interfaces.d/multiple-ips-init.cfg
  2. Open the file "/etc/network/interfaces.d/multiple-ips-init.cfg" and add the address entries as below:
    # vi /etc/network/interfaces.d/multiple-ips-init.cfg
                auto lo
    iface lo inet loopback
       dns-nameservers 8.8.8.8 8.8.4.4 2001:4860:4860::8888 2001:4860:4860::8844
    auto ens3
    iface ens3 inet static
      mtu 1500
      address 97.64.98.26/24
      gateway 97.64.98.1
    auto ens3:1
    allow-hotplug ens3:1
    iface ens3:1 inet static
      address 97.64.98.36/24

Removing an IPv4 Address


To remove an IPv4 address,

  1. Login to the instance.
  2. Use the following command to get the details of the existing public IPv4 address(es) of the instance:
    # ip -4 -d addr show
    E.g: The IP address for your instance ( CentOS 7 distribution) is 97.64.98.26 with a prefix 24. You have an additional IP address associated with the instance that is 97.64.98.36 with a prefix 24. You will receive an output similar to this:
    2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    inet 97.64.98.26/24 brd 206.246.84.255 scope global dynamic eth0
    valid_lft 85681sec preferred_lft 85681sec
    inet 97.64.98.36/24 brd 206.246.84.255 scope global dynamic eth0
    valid_lft 85681sec preferred_lft 85681sec
  3. Remove the IP address via the following command "ip addr del <ip address/prefix> dev <interface name>".
    # ip addr del 97.64.98.36/24 dev eth0
  4. You should not be able to PING or login to the instance via SSH using this IPv4.
  5. Make this change persistent as per the distribution, refer to the distribution changes:

CentOS-7

  1. Open and edit the file "/etc/sysconfig/network-scripts/ifcfg-eth0" file.
                # vi /etc/sysconfig/network-scripts/ifcfg-eth0
            
  2. Remove the entries from the file for the IPv4:
                BOOTPROTO=static
    GATEWAY=97.64.98.1
    IPADDR0=97.64.98.26
    PREFIX0=24
    IPADDR1=97.64.98.36
    PREFIX1=24

CentOS Stream 8, CentOS Stream 9, Fedora, Rocky Linux and Alma Linux

  1. Remove the additional IPv4 address configured on the instance.
                # nmcli con modify "System eth0" -ipv4.addresses 97.64.98.36/24
            
  2. Reboot the instance to apply the configuration.
                # shutdown -r now
            

OpenSUSE

  1. Open and edit the file "/etc/sysconfig/network/ifcfg-eth0" to remove the entries for IPv4 address.
                # vi /etc/sysconfig/network/ifcfg-eth0
            
                # Created by cloud-init on instance boot automatically, do not edit.
    #
    BOOTPROTO=dhcp
    DHCLIENT6_MODE=info
    LLADDR=fa:16:3e:65:8b:5a
    MTU=1500
    STARTMODE=auto
    IPADDR_0=97.64.98.26/24
    IPADDR_1=97.64.98.36/24

Ubuntu

Follow the steps below for the given distributions:

Ubuntu22.04 LTS, 20.04 LTS, 18.04 LTS, 16.04 LTS
  1. If you have more than two IPv4 addresses assigned to your instance, you can un-assign a specific address by removing it from the
    "/etc/netplan/multiple-ips-init.yaml" file as below:
    E.g:
                network:
      version: 2
      ethernets:
        ens3:
          dhcp4: false
          dhcp6: true
          match:
            macaddress: fa:16:3e:d9:58:50
          mtu: 1500
          set-name: ens3
          addresses: [97.64.98.26/24,97.64.98.36/24,97.64.98.46/24]
          gateway4: 97.64.98.1
    Or if you have only one additional IPV4 address, remove the file
    "/etc/netplan/multiple-ips-init.yaml".
                # rm /etc/netplan/multiple-ips-init.yaml 
            
  2. Apply the net plan to remove the additional IP address.
     # netplan apply

Debian & Ubuntu 16.04

Follow the steps below for the given distributions:

Debian11,10,9
Ubuntu16.04 LTS
  1. If you have more than two IPv4 addresses assigned to your instance, you can un-assign a specific address by removing it from the "/etc/network/interfaces.d/multiple-ips-init.cfg" file.
    E.g:
                auto lo
    iface lo inet loopback
      dns-nameservers 8.8.8.8 8.8.4.4 2001:4860:4860::8888 2001:4860:4860::8844
    auto ens3
    iface ens3 inet static
       mtu 1500
       Address 97.64.98.26/24
      gateway 97.64.98.1
    auto ens3:1
    allow-hotplug ens3:1
    iface ens3:1 inet static
      address 97.64.98.36/24
    auto ens3:2
    allow-hotplug ens3:2
    iface ens3:2 inet static
      address 97.64.98.46/24
    Or, if you have less than two additional IPv4 addresses, remove the file
    "/etc/network/interfaces.d/multiple-ips-init.cfg".
                # rm /etc/network/interfaces.d/multiple-ips-init.cfg
            
  2. Reboot the system to remove the additional IP address permanently.
    # shutdown -r now