Quickly add one (to many) IPs to an Ubuntu/Debian host

I had a bout of temporary confusion because I hadn’t manually added an IP to a Linux network adapter in a long time. In addition to that, some conventions have changed for the simpler. No longer do you have to append aliases to IPs like :0, :1, and so on.

In the below examples we’ll pretend our main IP is 172.29.0.99, and we wish to add 172.29.0.100. The subnets and whatever other mask calculation blah blah probably doesn’t match up sue me.

Temporarily add a single IP:

ip addr add 172.29.0.100/24 dev eth0

snooooooze who adds an IP to be removed on reboot?

 

Permanently add one or more IPs:

sudo vi /etc/network/interfaces

 

Observe default:

auto lo
iface lo inet loopback

# The primary network interface
auto ens160
iface ens160 inet static
   address 172.29.0.99
   netmask 255.255.255.0
   network 172.29.0.97
   broadcast 172.29.0.112
   gateway 172.29.0.98
   #dns-* options are implemented by the resolveconf package, if installed
   dns-nameservers 208.67.222.222 208.67.220.220

 

Modify to add your new IPs’ stanza

auto lo
iface lo inet loopback

# The primary network interface
auto ens160
iface ens160 inet static
   address 172.29.0.99
   netmask 255.255.255.240
   network 172.29.0.97
   broadcast 172.29.0.112
   gateway 172.29.0.98
   #dns-* options are implemented by the resolveconf package, if installed
   dns-nameservers 208.67.222.222 208.67.220.220


iface ens160 inet static
   address 172.29.0.100
   netmask 255.255.255.240

Once you’re done, reset networking either with a reboot, or with:

sudo systemctl restart networking.service

Of course everything above needs to be customized with your unique network information, such as IP addresses, netmask, network, broadcast, gateway. You can use those DNS servers though, they’re OpenDNS.

Have fun!


So, what do you think ?