Add and Remove IPv4 Addresses
Adding an IPv4 Address
Adding an IPv4 Address
To add an IPv4 address,
- Login to the instance.
- 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 - 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
- You should be able to PING or login to the instance via SSH using this new IPv4.
- Make this change persistent as per the distribution, refer to the distribution changes:
- Open and edit the file "/etc/sysconfig/network-scripts/ifcfg-eth0":
# vi /etc/sysconfig/network-scripts/ifcfg-eth0
- Add the below contents:
BOOTPROTO=static
GATEWAY=97.64.98.1
IPADDR0=97.64.98.26
PREFIX0=24
IPADDR1=97.64.98.36
PREFIX1=24Comments:
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
- 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 yesComments:
The GATEWAY address is available in the instance networking section - Add the additional IPv4 address.
# nmcli con modify "System eth0" +ipv4.addresses 97.64.98.36/24
- Restart the instance.
- 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.
Note - Here, you should add the default/main IPv4 address as IPADDR_0 along with the new IPv4 address as IPADDR_1.
#
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
Follow the steps below for the given distributions:
- 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
- Open the file " /etc/netplan/multiple-ips-init.yaml" and add the address entries as below:
# vi /etc/netplan/multiple-ips-init.yaml
network:
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.
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 - Apply the net plan to have multiple IP addresses.
# netplan apply
Follow the steps below for the given distributions:
- 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 - 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,
- Login to the instance.
- 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 - 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
- You should not be able to PING or login to the instance via SSH using this IPv4.
- Make this change persistent as per the distribution, refer to the distribution changes:
- Open and edit the file "/etc/sysconfig/network-scripts/ifcfg-eth0" file.
# vi /etc/sysconfig/network-scripts/ifcfg-eth0
- 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
- Remove the additional IPv4 address configured on the instance.
# nmcli con modify "System eth0" -ipv4.addresses 97.64.98.36/24
- Reboot the instance to apply the configuration.
# shutdown -r now
- 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
Follow the steps below for the given distributions:
- 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:
Or if you have only one additional IPV4 address, remove the file
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
"/etc/netplan/multiple-ips-init.yaml".# rm /etc/netplan/multiple-ips-init.yaml
- Apply the net plan to remove the additional IP address.
# netplan apply
Follow the steps below for the given distributions:
- 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
Or, if you have less than two additional IPv4 addresses, remove the file
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
"/etc/network/interfaces.d/multiple-ips-init.cfg".
# rm /etc/network/interfaces.d/multiple-ips-init.cfg
- Reboot the system to remove the additional IP address permanently.
# shutdown -r now