Configuring a single network interface with multiple IP addresses in Linux allows you to host several services on one physical connection. This technique, often called IP aliasing or secondary addressing, is essential for web servers hosting multiple domains and VPN gateways managing diverse client subnets. By assigning distinct IPs to one NIC, you simplify routing logic while maintaining strict separation of network traffic flows.
Understanding Network Interfaces and Aliases
In Linux, a network interface like
eth0 or
ens33 typically holds one primary IP address. However, the kernel supports adding additional logical interfaces, often referred to as aliases (e.g.,
eth0:1), which share the same physical hardware but operate independently at the IP layer. This approach avoids the complexity of managing multiple physical cards or VLANs when simple IP separation suffices.
You can achieve this configuration using several methods depending on your distribution and init system. The two most common modern approaches are using
netplan on Ubuntu-based systems and
systemd-networkd on RHEL/Fedora/CentOS systems. Legacy systems might still use the
/etc/network/interfaces file or
ifcfg scripts in Red Hat environments. Understanding your current network stack is crucial before applying changes.
For users coming from Windows backgrounds, this concept is similar to binding multiple IP addresses to a single network adapter in the TCP/IP properties. In Linux, these addresses are managed by the networking daemon rather than a static GUI setting. If you are already familiar with how Ubuntu server network interfaces work, you will find that modern declarative configuration files make this process even more straightforward. You can read more about the underlying mechanics in our guide on
How do Ubuntu-server network interfaces work?.
Configuring Multiple IPs with Netplan
Netplan is the default network configuration utility for Ubuntu 18.04 and later. It uses YAML files located in
/etc/netplan/ to define network settings. To add a secondary IP to an existing interface, you must edit the appropriate YAML file, such as
01-netcfg.yaml.
First, identify your interface name using
ip a. Let us assume your primary interface is
eth0 with an existing DHCP or static configuration. You need to add a
addresses list under the
addresses key for that specific interface. Each additional IP must be specified in CIDR notation (e.g.,
192.168.1.50/24).
Here is an example configuration for a static setup:
network:
version: 2
ethernets:
eth0:
dhcp4: no
addresses:
- 192.168.1.10/24
- 192.168.1.50/24
- 192.168.1.51/24
routes:
- to: default
via: 192.168.1.1
nameservers:
addresses:
- 8.8.8.8
- 8.8.4.4
After saving the file, apply the changes using
sudo netplan apply. If there are syntax errors, Netplan will report them immediately. Verify the new addresses are active by running
ip addr show eth0. You should see all three IP addresses listed under the same interface.
Configuring Multiple IPs with systemd-networkd
For distributions using
systemd-networkd, configuration is handled through unit files in
/etc/systemd/network/. Create a new file named after your interface, such as
eth0.network. This method is highly portable across various Linux distributions that support systemd.
The configuration requires defining the
[Network] section correctly. A common error is placing the
Address directive outside this section or using incorrect indentation. Ensure that all IP addresses are listed under the
Addresses key within the
[Network] block.
Here is the correct structure for a static configuration with multiple IPs:
[Match]
Name=eth0
[Network]
Address=192.168.1.10/24
Address=192.168.1.50/24
Address=192.168.1.51/24
Gateway=192.168.1.1
DNS=8.8.8.8
DNS=8.8.4.4
Once the file is saved, restart the network service using
sudo systemctl restart systemd-networkd. Check the status with
systemctl status systemd-networkd to ensure there are no startup failures. Use
journalctl -u systemd-networkd if you suspect issues during initialization.
Verification and Troubleshooting
Regardless of the method used, verification is the final critical step. Use
ip addr show to confirm that all assigned IPs appear on the target interface. You can also test connectivity by pinging each IP from another machine on the same subnet. If packets are not reaching a specific IP, check your firewall rules using
ufw or
iptables to ensure they allow traffic on the new addresses.
If you are using this setup for a VPN gateway, ensuring proper routing for each IP is vital. Misconfigured routes can lead to asymmetric routing issues where return traffic takes a different path. For advanced scenarios involving encrypted tunnels, you may need to consider PMTU discovery to avoid fragmentation issues. Learn more about optimizing UDP connections in
Implement PMTU Discovery on UDP Connection for VPN Tunnel.
Additionally, if you plan to route traffic between different peers or clients, understanding how to manage peer traffic is essential. Improper routing tables can cause connectivity drops for specific IPs. Review best practices for
Routing Internet-Bound Traffic Between Two WireGuard Peers to complement your multi-IP setup.
Frequently Asked Questions
Can I assign IPv6 addresses alongside IPv4 on the same interface?
Yes, you can specify both IPv4 and IPv6 addresses in the same configuration file. Simply add the IPv6 addresses to the
addresses list in Netplan or the
Addresses key in systemd-networkd. The kernel handles both protocol stacks independently on the same physical interface.
Do I need to modify firewall rules for the new IP addresses?
Typically, yes. Firewalls like UFW or iptables often bind to specific IPs or subnets. If your rules only allow traffic to the primary IP, the new secondary IPs will drop incoming connections. Update your firewall configurations to explicitly permit traffic on the new addresses or the entire subnet.
What happens to the secondary IPs if the interface goes down?
All IP addresses assigned to a single interface are lost if that interface is brought down or disconnects. They are not persistent across reboots unless defined in the configuration files. Upon reboot, the network manager reads the config files and re-applies all defined addresses automatically.
Is there a limit to how many IPs I can add to one interface?
The limit is generally determined by the kernel and available memory, but practically, you can add dozens or even hundreds of IPs. However, performance may degrade if you have thousands of entries due to increased ARP table size and routing lookups. For massive scale, consider using virtual interfaces or VLANs instead.
How do I remove a secondary IP without affecting the primary one?
You can remove a secondary IP temporarily using the
ip addr del command followed by the IP and interface name. To remove it permanently, edit the configuration file (Netplan or systemd-networkd) and delete the specific line corresponding to that IP, then apply the changes again.