如何为现有界面设置额外的IP?

我有一个接口eth0 ,我希望给它一个额外的虚拟IP。 我通过以下方式实现这一目标:

 ifconfig eth0:0 ip.address.goes.here netmask subnet.address.goes.here 

这工作正常,但是,当我重新启动时,这会丢失。

我已经尝试编辑/etc/network/interfaces来添加以下内容:

 auto eth0:0 iface eth0:0 inet static address ip.address.goes.here netmask subnet.address.goes.here 

但是,重新启动时,eth0的静态ip被正确加载,但是,根本没有加载eth0:0虚拟IP。

那么,如何永久添加eth0:0虚拟IP?

而不是那个eth0:0业务,你应该这样做:

  • 像往常一样在/etc/network/interfaces配置(一个)静态IP地址:

     # The primary network interface auto eth0 iface eth0 inet static address 192.168.0.201 network 192.168.0.0 netmask 255.255.255.0 broadcast 192.168.0.255 gateway 192.168.0.1 
  • 通过在上面添加之后添加另一个IP 到此接口:

     up /sbin/ip addr add 192.168.0.203/24 dev eth0 down /sbin/ip addr del 192.168.0.203/24 dev eth0 
  • 完整文件应如下所示

现在,如果您通过运行ip addr show检查配置了哪些IP地址,则两者都将显示:

  2:eth0:mtu 1500 qdisc pfifo_fast state UP qlen 1000
     link / ether 08:00:27:1d:fa:0b brd ff:ff:ff:ff:ff:ff
     inet 192.168.0.201/24 brd 192.168.0.255 scope global eth0
     inet 192.168.0.203/24范围全球次要eth0

我要感谢Lekensteyn指出我正确的方向。 互联网上的每个站点都只讨论eth0:0作为辅助IP地址。 似乎是正确的方法。

如果你想以“传统”的方式做事, /etc/network/interfaces的相关部分应如下所示:

 auto eth0:0 iface eth0:0 inet static address ip.address.goes.here netmask subnet.address.goes.here 

而不是这个,你犯了一个错误:

 auto eth0:0 iface eth0:0 inet static address ip.address.goes.here netmask subnet.address.goes.here