49

On my server I want to assign several IP addresses to one NIC, but without using the deprecated ifconfig or the obsolete "alias" notation (like eth0:0) in /etc/network/interfaces because in IP Aliasing (on www.kernel.org) you can read

IP-aliases are an obsolete way to manage multiple IP-addresses/masks per interface

2
  • ifconfig is deprecated? I didn't know.
    – Mahesh
    Nov 8, 2014 at 18:53
  • @Mahesh Yeah, ifconfig is an artifact from the SysV era. 'iproute2' is the more modern tool. ifconfig will be around for a while on various distros, but yeah, it's deprecated; "deprecated" is just a colloquialism we used to denote "look for something newer to use".
    – David Betz
    Jan 4, 2016 at 16:16

3 Answers 3

57
  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.

3
  • You can maybe make an vpn whit more then one ip address by installing from from software manager.
    – Michael
    Nov 8, 2014 at 19:23
  • No reboot is required on Ubuntu 16.04.3 LTS after adding or deleting an IP
    – Daniel F
    Nov 9, 2017 at 17:55
  • 2
    looks like ifdown is deprecated in newer Ubuntu versions.
    – Sajuuk
    Jun 10, 2018 at 4:29
22

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
5
  • Thx for providing the matching commands.
    – guntbert
    Nov 8, 2014 at 19:19
  • thx @chaos, well can I have more ip's than my no of interfaces on my system??
    – lazarus
    Nov 9, 2014 at 12:29
  • @jazzz yes, as you can see in the output of my interface above, there are 2 ip addresses assigned to it (192.168.0.100 and 192.168.1.1). You can add another one if you wish.
    – chaos
    Nov 9, 2014 at 13:33
  • @chaos, but how can I use them, for an instance I want to create a group for multicasting,,can I form a group from them,, can you please refer to my question askubuntu.com/questions/547105/…
    – lazarus
    Nov 9, 2014 at 14:29
  • 2
    @chaos I am accepting my own answer instead of your excellent one because yours lacks the "permanency" which I expect on a server config.
    – guntbert
    Feb 3, 2015 at 21:33
0

One way is:

sudo ip addr add 192.168.0.2/24 dev eth1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .