【sysctl】How to bind IP that doesn't exist yet?

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 on0.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.confto make this change permanent.

#  echo "net.ipv4.ip_nonlocal_bind=1" >> /etc/sysctl.conf


How to bind IP that doesn't exist yet?


方式1:

# sysctl -w net.ipv4.ip_nonlocal_bind=1

# echo "net.ipv4.ip_nonlocal_bind=1" >> /etc/sysctl.conf


方式2:

# echo "net.ipv4.ip_nonlocal_bind=1" >> /etc/sysctl.conf

# sysctl  -p


方式3:

# echo 1 > /proc/sys/net/ipv4/ip_nonlocal_bind

#  echo "net.ipv4.ip_nonlocal_bind=1" >> /etc/sysctl.conf


参考 

Linux bind IP that doesn’t exist with net.ipv4.ip_nonlocal_bind

https://www.codercto.com/a/102985.html


/proc/sys/net/ipv4/

https://www.cnblogs.com/klcf0220/p/6003222.html


Linux bind任意ip,Linux 绑定非本机 IP - net.ipv4.ip_nonlocal_bind

https://blog.csdn.net/weixin_33330762/article/details/116737936

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容