The Linux Page

Assigning multiple IP addresses to the same Network Card

Multiple Adresses on the same Network Interface Card

Netplan Method

I think that the Netplan method is pretty explicit. The addresses option accepts an array of addresses with a mask (prefix length, really, which is mandatory; the "/24" in my example). So you do not have to duplicate the whole entry.

Here is an example:

network:
    version: 2
    renderer: networkd
    ethernets:
        eno1:
            addresses:
                - 192.168.1.2/24
                - 10.1.1.233/24
            dhcp4: false
            dhcp6: false
            optional: false
            gateway4: 192.168.1.1
            nameservers:
                addresses:
                    - 75.75.75.75
                    - 75.75.76.76

Notice that the array is also used for the list of nameservers IP addresses.

New Method

On newer systems (probably starting with 16.04 and as long as the old interface files are used along with ifconfig), you can enter multiple address simply by repeating the same iface any number of times:

auto eth0
iface eth0 inet static
    address 192.168.1.1
    ...snip (see below)...

iface eth0 inet static
    address 10.1.0.1
    netmask 255.255.0.0

The eth0:0 trick shown below may actually work on newer system. From what I understand, it depends on the hardware as well as the OS version.

I had to use this option on my Jetson AGX Xavier. All NVidia Jetsons probably support a similar definition.

Setup your interface for multiple IP addresses

Today I got a new IP address for a server and I had to assign it to the same NIC as my current IP address.

So?! How do you do that on a Debian or Ubuntu server?

It is actually very simple. On Ubuntu you go to /etc/network and edit the interfaces file. It should already include a definition for eth0 (virtual machines will have something else than eth0).

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
auto eth0
iface eth0 inet static
    address 192.168.1.1
    netmask 255.255.255.0
    network 192.168.1.0
    broadcast 192.168.1.255
    gateway 192.168.1.1

auto eth0:0
iface eth0:0 inet static
    name Alias for Foo
    address 192.168.1.2
    netmask 255.255.255.0
    network 192.168.1.0
    broadcast 192.168.1.255
    gateway 192.168.1.1

I marked in red what is different between both definitions.

Once you write that file to disk, you can start that interface with the ifup command:

ifup eth0:0

Now it is up and running (unless you got tons of errors?) Obviously, the eth0 interface is expected to already be up. You cannot start an alias if the main interface is not running.

To verify that the new interface is up and running, use the ifconfig command as in:

ifconfig

This command lists all the interfaces, including aliases. Note that aliases do not list the number of packets transfered since they are considered 100% similar to the main interface. This also applies to the firewall. In other words, you probably won't have to make any changes to your firewall...1

You can add more changing the number after the colon, for instance eth0:1, eth0:2, etc.

To be noted: You are limited to 254 aliases (0 to 254 + main interface, that's 255 IPs per NIC!)

But outbound traffic comes from the wrong IP address now!

Another problem you will run into is the randomness of the IP address used to send traffic from your server to the world. In general it won't matter, unless you have processes doing things like backups that need firewall like protection and you want to make sure that only one address is used.

In that case, you want to add a new route. On the command line, it looks like this:

route add -host 1.2.3.4 dev eth0

The IP address 1.2.3.4 is an external IP (another computer on your local network or on the Internet.) All traffic sent to that server will be forced through eth0.

If you want all your traffic to go through eth0, then don't specify the destination IP as in:

route dev eth0

This uses 0.0.0.0 as the destination IP address which is the any IP wildcard.

To keep the route permanent (i.e. set again on reboot) add it to the interface file as follow:

up route add -net 192.168.1.0 netmask 255.255.255.0 gw 192.168.1.1 eth0

You can enter whatever route command you need to make your system work. Just tweak the parameters as required for your network.

Somehow now Postfix is using the wrong IP address

The route trick is not likely to work right with Postfix. What you want is to edit the settings and add the IP address to be used with SMTP. That's usually very important for other mail system to verify your PTR.

The settings go in main.cf and look like this:

smtp_bind_address=1.2.3.4

You may include several IPs, such as localhost, separated by commas. Although at this point I did not find that there was a need for anything more.

More information

For more details about all the options available in the interface file, check out the man page of the same name:

man interfaces
  • 1. Yes. If you want a highly secure setup, you want to have two NICs.