How can I (from CLI) assign multiple IP addresses to one interface?

  1. If you need an additional IP address just for the moment you can add it to any interface on your machine with

     sudo ip address add <ip-address>/<prefix-length> dev <interface>
    

    for instance

     sudo ip address add 172.16.100.17/24 dev eth0
    

    would add 172.16.100.17 using a 24 bit network prefix to the list of addresses configured for your eth0.

    You can check the result with

    ip address show eth0
    

    and you can delete this address again with

    sudo ip address del 172.16.100.17/24 dev eth0
    

    Of course these changes are lost when you reboot your machine.

  2. To make the additional addresses permanent you can edit the file /etc/network/interfaces by adding as many stanzas of the form

    iface eth0 static
        address 172.16.100.17/24
    

    so that it looks like

    iface eth0 inet dhcp
    
    iface eth0 inet static
        address 172.16.100.17/24
    
    iface eth0 inet static
        address 172.16.24.11/24
    

    You can even keep the dhcp for the primary address.

    To activate these settings without a reboot use ifdown/ifup like

    sudo ifdown eth0 && sudo ifup eth0
    

    It is essential to put those two commands into one line if you are remoting into the server because the first one will drop your connection! Given in this way the ssh-session will survive.


With the new toolkit, it is as easy as with the old to add new ip addresses:

ip addr add 192.168.1.1/24 dev eth0

When looking with ip addr show again, you see the second ip address assigned to the interface:

2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether xx:xx:xx:xx:xx:xx brd ff:ff:ff:ff:ff:ff
    inet 192.168.0.100/24 brd 192.168.0.255 scope global eth0
    inet 192.168.1.1/24 scope global eth0
    inet6 fe80::223:54ff:fe45:f307/64 scope link
       valid_lft forever preferred_lft forever

Remove that ip address with:

ip addr del 192.168.1.1/24 dev eth0

The iproute2 suite:

The iproute2 suite is the communication suite for interprocess communication beween the kernel and the user space via the netlink protocol. It should replace the whole standard network tools. Here is what they replace:

  • ifconfig --> ip addr and ip link
  • route --> ip route
  • arp --> ip neigh
  • iptunnel --> ip tunnel
  • ipmaddr --> ip maddr
  • netstat --> ss