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

guntbert
  • 13,134
  • ifconfig is deprecated? I didn't know. – Mahesh Nov 08 '14 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 04 '16 at 16:16

3 Answers3

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.

balazer
  • 133
guntbert
  • 13,134
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
chaos
  • 27,506
  • 12
  • 74
  • 77
  • Thx for providing the matching commands. – guntbert Nov 08 '14 at 19:19
  • thx @chaos, well can I have more ip's than my no of interfaces on my system?? – lazarus Nov 09 '14 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 09 '14 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 http://askubuntu.com/questions/547105/how-to-fix-a-port-number-statically-for-client-request-on-client-side – lazarus Nov 09 '14 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 03 '15 at 21:33
0

One way is:

sudo ip addr add 192.168.0.2/24 dev eth1
Bert
  • 3,225
  • 1
  • 14
  • 12