How to setup multiple IP on Debian

Today, that will be my first "tech" oriented post. I would like to share a way to easily setup Public IPs on Linux. To be honnest it’s pretty easy, I will also share a way to bring up or down your interfaces easily with a bash script. This will definitely be a cheat sheet for me and for you guys. I often need to setup multiple IPs on my servers this is why it could be helpful to post this here.

Info: Again, just to let you know guys my blog is a place where I will love to write about things that interest me. I'm not an expert and sometimes I may talk about thing which are not EXACTLY right or even It might be right at the time of writting and then expired after few days, weeks, months or years. Please be kind and of course I invite you to leave a comment or any suggestion in order to let me know if I'm wrong or supposed to do things in another way. Do not forget we are always learning in our life and I think we need to share knowledges to each other.

Dealing with interfaces

First of all if you need to check your current interfaces on Linux (or debian based one) this is the following command line:
iiaku$ 
ifconfig

Then you’ll get something that look like this depending on your OS/Hardware:

eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500

inet YOUR_IPV4 netmask NETMASK broadcast BROADCAST

inet6 YOUR_IPV6 prefixlen 64 scopeid 0x20<link>

ether MAC_ADDRESS txqueuelen 1000 (Ethernet)

RX packets 589916544 bytes 125667563361 (117.0 GiB)

RX errors 0 dropped 0 overruns 4219 frame 0

TX packets 522642062 bytes 154555782206 (143.9 GiB)

TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

device memory 0xdf120000-df13ffff

Conf files are located there on Debian based distributions:
/etc/network/interface.conf

The following part depends on which server provider you are using. Most of the time a failover IP will be attached to a server/instance or wathever.
Once this is done, you will have to configure it as interface then your server will be reachable from this IP.

You need to edit the following file from here:
iiaku$ cd /etc/network

iiaku$ /etc/network# ls

if-down.d if-post-down.d if-pre-up.d if-up.d interfaces interfaces.d run
nano /etc/networks/interfaces

Then you add your failover ip like this:

iface eth0:1 inet static

address FAILOVER IPv4

netmask 255.255.255.255

iface eth0:2 inet static

address FAILOVER IPv4

netmask 255.255.255.255

You can then bring up or down an interface like this:

  • Getting up eth0:1 interface:

    ifup interface_name ~> eg: iiaku$ ifup eth0:1
  • Getting down eth0:1 interface:

    ifdown interface_name ~> eg: iiaku$ ifdown eth0:1

Check your interfaces:

iiaku$ ifconfig

Then you can see your reachable interfaces.

eth0:1 Link encap:Ethernet HWaddr INTERFACE_MAC_ADDRESS

inet addr:IPV4_IP Bcast:BROADCAST Mask:255.255.255.255

UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1

Memory:80000000-8001ffff

eth0:2 Link encap:Ethernet HWaddr INTERFACE_MAC_ADDRESS

inet addr:IPV4_IP Bcast:BROADCAST Mask:255.255.255.255

UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1

Memory:80000000-8001ffff

Automate the bringing up/down step

When there is multiple interfaces. It may be tedious to bring up each interfaces one by one.
This is why I wrote a simple script available on my github see below.

You can customise the script with ease if you want to stick to your configuration.

How to use it:

iiaku$: git clone https://github.com/iiAku/ifupdown

iiaku$: chmod a+x interfaces.sh

eg: iiaku$:./interfaces ifup eth0 3

Will output:
Setting up eth0 eth0:1

Setting up eth0 eth0:2

Setting up eth0 eth0:3

That's pretty simple. You are supposed to see your interfaces up now into:

That's sit for today, keep this one on your sticky notes it might help you when you are getting started for a fresh new server.