This article will guide you through adding, removing, or modifying IPv4 and IPv6 addresses on a Linux machine.
As the Desktop versions of most Linux operating systems come with a Graphical User Interface (GUI), they have easy-to-use tools to add and edit connections. It’s recommended to stick with these and not try to manually modify the networking service with other tools, unless you have a purposeful reason.
Determine interface and service
[1] Find out the logical name of your ethernet interface:sudo lshw -class network | less
For example, “logical name: eth4”
[2] Determine which network service is running:systemctl list-units --type=service | grep -E 'NetworkManager|systemd-networkd|cloud-init|ifup'
# older systems use: chkconfig --list | grep -E 'NetworkManager|systemd-networkd|cloud-init|ifup'
You are looking for the service that is loaded, active, and running (all three).
[3] Check if netplan is active:netplan status
NetworkManager and systemd-networkd can be shown as active, but may be actually managed by Netplan.
- NetworkManager with Netplan
- NetworkManager without Netplan
- systemd-networkd with Netplan
- systemd-networkd without Netplan
- Cloud-Init managed networking
- Verifying & troubleshooting
NetworkManager with Netplan
[1] List the netplan connection profiles:ls /etc/netplan
[2] Edit the existing config:nano /etc/netplan/50-cloud-init.yaml
# or create a new one: nano /etc/netplan/99_config.yaml
DHCP example:
network:
version: 2
renderer: NetworkManager
Static IP example:
network:
version: 2
renderer: NetworkManager
ethernets:
eth4:
addresses:
- 10.10.10.2/24
- 2001:db8::2/64
routes:
- to: default
via: 10.10.10.1
- to: ::/0
via: 2001:db8::1
nameservers:
search:
- "mycompany.local"
addresses:
- 10.10.10.253
- 8.8.8.8
- 2001:4860:4860::8888
- 2001:4860:4860::8844
[3] Apply the configuration:
netplan try
This will apply the configurations and prompt the user to accept the new changes as working. If no response is received, changes will be reverted.
NetworkManager without Netplan
- NetworkManager replaced Network Administration Tool as of RHEL 6. The config files are stored in /etc/sysconfig/network-scripts/ in RHEL versions 6 thru 8.
- NetworkManager parses /usr/lib/NetworkManager/conf.d/ first, then /run/NetworkManager/conf.d/
- NetworkManager on RHEL 9 and later use keyfiles within /etc/NetworkManager/system-connections/
- Although you can manually edit these files, it is much simpler to use the nmcli tool instead.
[1] List the NetworkManager connection profiles:nmcli connection show
[2] If no connection profile exists, create one:nmcli connection add con-name <connection_name> ifname <network-interface-name> type ethernet
[3] Display the current settings of the connection profile:
nmcli connection show <network-interface-name>
Configure networking via DHCP:nmcli connection modify ipv4.method auto
Or, configure networking via static IP instead (with auto IPv6):nmcli connection modify <connection_name> ipv4.method manual ipv4.addresses 192.0.2.1/24 ipv4.gateway 192.0.2.254 ipv4.dns 192.0.2.200 ipv4.dns-search example.com ipv6.method auto
Static IPv6 configuration:nmcli connection modify <connection_name> ipv6.method manual ipv6.addresses 2001:db8:1::fffe/64 ipv6.gateway 2001:db8:1::fffe ipv6.dns 2001:db8:1::ffbb ipv6.dns-search example.com
[4] Activate the profilenmcli connection up
systemd-networkd with Netplan
[1] List the netplan connection profiles:ls /etc/netplan
[2] Edit the existing config:
nano /etc/netplan/50-cloud-init.yaml
# or create a new one: nano /etc/netplan/99_config.yaml
DHCP example:
network:
version: 2
renderer: networkd
ethernets:
eth4:
dhcp4: true
Static IP example:
network:
version: 2
renderer: networkd
ethernets:
eth4
addresses:
- 10.10.10.2/24
- 2001:db8::2/64
routes:
- to: default
via: 10.10.10.1
- to: ::/0
via: 2001:db8::1
nameservers:
search: [mydomain, otherdomain]
addresses:
- 10.10.10.1
- 1.1.1.1
- 2001:db8::53
- 2001:4860:4860::8888
Note: Older versions of Netplan don’t understand the “to: default” syntax for routes, and should use the older gateway4 and gateway6 keys instead. For example: addresses:
- 10.10.10.2/24
gateway4: 192.168.42.1
gateway6: 2001:db8::1
Apply the configuration:netplan try
This will apply the configurations and prompt the user to accept the new changes as working. If no response is received, changes will be reverted.
systemd-networkd without netplan
Determine if ifupdown is enabled:
ifdown -V
If ifupdown is installed
[1] Edit the network interfaces config file:
sudo nano /etc/network/interfaces
DHCP example:
auto eth0
iface eth0 inet dhcp
Static IP example:
# The loopback network interface
auto lo
iface lo inet loopback
# The primary network interface
auto eth0
# primary IPv4
iface eth0 inet static
address 192.168.2.33
gateway 192.168.2.1
netmask 255.255.255.0
# secondary IPv4
auto eth0:1
iface eth0:1 inet static
address 192.168.2.33
netmask 255.255.255.0
# IPv6
iface eth0 inet6 auto
accept_ra 2
or static IPv6:iface eth0 inet6 static
address [ip-address]/128
accept_ra 2
[2] Reload with ifupdown:sudo ifdown eth4 && sudo ip addr flush eth4 && sudo ifup eth4
If ifupdown is not installed
[1] List the existing network interface file:
ls /etc/systemd/network
[2] If no profile exists, create one:
nano /etc/systemd/network/10-ens4.network
DHCP example:
[Match]
Name=ens6
[Network]
DHCP=yes
Static IP example:
[Match]
Name=ens6
[Network]
Address=74.208.61.32/24
Gateway=74.208.61.1
DNS=212.227.123.16,212.227.123.1
Additional IPv4 address
Address=74.208.98.26/24
# IPv6 configuration
Address=2001:db8::c0a8:1/64
Gateway=fe80::1
# Additional IPv6 address
Address=2001:db8::c0a8:2/64
[3] Restart networking:
sudo systemctl restart systemd-networkd
# older systems use: sudo /etc/init.d/networking restart
# or: sudo /etc/init.d/network restart
Cloud-Init managed networking
On some systems offered by Cloud server providers, the networking will be preconfigured by the host with Cloud-Init. In many cases, this means that adding or removing an IP address in the provider’s control panel should be enough. It’s best to check with your provider for appropriate steps for adding or removing IP addresses. However, if you or your provider require manual configuration of IP addresses, first you will need to disable Cloud-Init network management, and then use an alternative service.
Note: It’s wise to consider which networking utility you will use, and install if it’s not already installed. If you disable networking first, you may not be able to download the required packages after. Debian uses ifupdown by default. Ubuntu uses Netplan (as of 18.04; ifupdown in version 16.04). RHEL uses NetworkManager
[1] List Cloud-Init network profiles:
ls /etc/cloud/cloud.cfg.d
The configuration with the higher number (up to 99) overwrites anything below it.
[2] List Cloud-Init network profiles:
ls /etc/cloud/cloud.cfg.d
[3] If you already have a 99- file, edit it. Otherwise, create one.
nano /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg
Add the following line
network: {config: disabled}
[4] Reboot your machine. Configure networking using ifupdown, Netplan, systemd-networkd, or NetworkManager using the applicable docs.
Verifying & troubleshooting
Display the loaded IP settings:
ip a
Display the IPv4 default gateway:
ip route show default
Display the IPv6 default gateway:
ip -6 route show default
Check service status:
service NetworkManager status || systemctl status systemd-networkd || systemctl status networking || systemctl status network.service || systemctl status cloud-init
Ping the outside world:
ping google.com