54

I have an extra IP address available to my server, and so I need to assign it in the interfaces file. At the moment, I've got this:

auto lo
iface lo inet loopback
  auto eth0

iface eth0 inet static
  address aaa.aaa.aaa.aaa
  netmask 255.255.254.0
  gateway bbb.bbb.bbb.bbb
  dns-nameservers ccc.ccc.ccc.ccc ddd.ddd.ddd.ddd eee.eee.eee.eee
  dns-search vps-number.com

What do I add/assign my new IP address (fff.fff.fff.fff)? And how do I then restart it to accept the new configuration?

Alexis Wilke
  • 2,632
  • 2
  • 21
  • 33
babbaggeii
  • 877
  • 3
  • 9
  • 14

3 Answers3

64
auto lo
iface lo inet loopback

auto eth0
iface eth0 inet static
  address aaa.aaa.aaa.aaa
  netmask 255.255.254.0
  gateway bbb.bbb.bbb.bbb
  dns-nameservers ccc.ccc.ccc.ccc ddd.ddd.ddd.ddd eee.eee.eee.eee
  dns-search vps-number.com

auto eth0:0
iface eth0:0 inet static
  address fff.fff.fff.fff
  netmask 255.255.254.0

Then you can run sudo ifup eth0:0 to bring it up and sudo ifdown eth0:0 to bring it down.

Eric Carvalho
  • 53,609
  • 102
  • 137
  • 162
  • 9
    This is outdated variant with aliases. Use below one with "ip addr" instead. – Alexander Kim Jun 04 '15 at 16:14
  • 6
    It may be old (and maybe outdated), but there's nothing wrong with using this method, since it was not marked 'deprecated' and there's no plan for it being removed in the near future. IMHO this method is cleaner and safer than the others (see the [Wilmer's answer](http://askubuntu.com/a/667243/65926) and its comments). – Eric Carvalho Dec 06 '15 at 11:00
  • P.S If using `auto eth1`(it didnt on AWS) does not working use as in the example `auto eth0:0`. – IvRRimUm Jan 21 '18 at 10:47
  • The legacy solution [didn't work](https://askubuntu.com/questions/313877/how-do-i-add-an-additional-ip-address-to-etc-network-interfaces/667243#comment1936787_667243) for me on Debian Stretch, I ended up with just the second IP. See the [`iproute2` method](https://askubuntu.com/a/667243/186999), it worked. – x-yuri Jul 31 '19 at 11:42
37

You can just have repeated iface stanzas for the same interface. Example from https://wiki.debian.org/NetworkConfiguration#Multiple_IP_addresses_on_one_ :

auto eth0
allow-hotplug eth0
iface eth0 inet static
    address 192.168.1.42/24
    gateway 192.168.1.1

iface eth0 inet static
    address 192.168.1.43/24

iface eth0 inet static
    address 192.168.1.44/24

# adding IP addresses from different subnets is also possible
iface eth0 inet static
    address 10.10.10.14/24

So just the solution above but drop the :x suffix, which as Heihachi points out is outdated.

(The ip addr suggestion is the worst. It's ugly and incomplete, as you'll have to also add a down variant or ifdown won't work very cleanly.)

Wilmer
  • 488
  • 4
  • 5
  • 2
    From the documentation at your link, talking about the most modern method for doing things (called iproute2): _Note however that this method is **dangerous**! Certain driver/hardware combinations may sometimes fail to bring the link up if no labels are assigned to the alias interfaces._ Considering that it's official documentation saying that the new way isn't such reliable, I'd prefer to stick with the working deprecated one. – reallynice Nov 27 '15 at 11:07
  • 1
    Upvote because of official and precise documentation reference, but not for "a better solution", since it's marked as dangerous in the official documentation. – reallynice Nov 27 '15 at 12:46
  • 4
    @reallynice Reasonable point, though IMHO if this causes problems on a system that sounds to me like a buggy kernel/driver. I've been using multiple IP addresses on a single interfaces without the ugly :0/:1/etc aliases for years.(Also I'm pretty sure the solution from the Wiki is functionally equivalent to Robbie's solution above.) – Wilmer Jul 09 '16 at 18:37
  • @Wilmer I have a vmxnet3 device (VMware ESXi 6.5) on Ubuntu 16.04, and I could only get the new way to bring up the first device. Dropping the :x suffix was clearly not better for me. The documentation cleared up an error I was getting with the :x suffix though. – jbo5112 Jul 09 '17 at 03:06
  • 2
    The last known issue with `iproute2` method was [in 2015](https://wiki.debian.org/NetworkConfiguration?action=diff&rev1=88&rev2=89). And the legacy method (have you noticed the name?) didn't work for me on Debian Stretch. It seemed like the later options (from `eth0:0`) overrided the earlier ones (`from eth0`). That is, after `systemctl restart networking` I ended up with just the second IP (the first IP was detached). – x-yuri Jul 31 '19 at 11:33
  • 1
    ...Then, in [Debian Stretch](https://salsa.debian.org/debian/ifupdown/commit/5e64a04a4f6ca49bfb95c1a843152168ba1c2f60#8f15e769733d16404498d48f0612811cb5fbb822_167_169) ([Ubuntu 16.04 Xenial](http://manpages.ubuntu.com/manpages/xenial/man5/interfaces.5.html)) they added: 'Multiple "iface" stanzas can be given for the same interface, in which case all of the configured addresses and options for that interface will be applied when bringing up that interface. [...] It can also be used to configure multiple addresses of the same type on a single interface.' – x-yuri Jul 31 '19 at 11:33
  • This worked properly under Ubuntu 18.04 on a Jetson AGX Xavier. The old method (using `eth0:0`) did not let me even ping the second IP address locally. – Alexis Wilke Feb 03 '21 at 01:39
27

For an extra IP address, I usually add:

up ip addr add fff.fff.fff.fff/prefixlen dev $IFACE

to the bottom of the iface eth0 inet static stanza for future reboots and then run the command sudo ip addr add fff.fff.fff.fff/prefixlen dev eth0 again manually to activate it directly.

If your netmask is 255.255.254.0 then prefixlen should be 23 for you.

I'd love to know if there's a better way, though.

0xC0000022L
  • 5,656
  • 6
  • 52
  • 91
Robie Basak
  • 15,524
  • 4
  • 61
  • 86
  • 1
    thank you so much, the accepted approach doesn't work for bonding (**ifenslave**), but this does! – mekkanizer Dec 13 '18 at 11:18
  • 1
    `up` is problematic as network-online.target does not wait for it. systemd services like nginx will fail to bind on IP added by `up` on startup time – sdaffa23fdsf Mar 17 '19 at 03:37
  • For Debian Stretch/Ubuntu 16.04 Xenial or later there's [the official solution](https://askubuntu.com/a/667243/186999). – x-yuri Jul 31 '19 at 11:36
  • One would always use `$IFACE` instead of hardcoding the interface name in the `up`/`down` stanzas _if_ one opts to use this method. – 0xC0000022L Dec 12 '19 at 14:38