Linux: How to bind IP that doesn't exist (yet).

May 25, 2010  

This tip is really usefull if you use virtual ips.

Imagine you are using keepalived + haproxy on your loadbalancers in active/active mode.

You have 2 virtual IPs one is on the first load balancer and the other is on the second.

Now in your haproxy.cfg you have to bind these IPs, the probem is that one of the virtual IPs is not on the current loadbalancer, so haproxy will refuse to start with the following error: Starting proxy appli1-rewrite: cannot bind socket.

The common solution is to listen on 0.0.0.0, but it not always practical on wanted, especially if you want to have another server using the same port but on another ip, or if you only want to listen on internal interfaces.

The solution is a sysctl that will allow any software to bind an IP even if it’s not up yet:

sysctl -w net.ipv4.ip_nonlocal_bind=1

You should put this in /etc/sysctl.conf to make this change permanent.