一个接口上有多个静态 IP Debian 9 Stretch

一个接口上有多个静态 IP Debian 9 Stretch

无法将多个 IP 添加到网络接口。

尝试通过 Webmin 向接口 enp2s0 添加 IP 时,它显示:

Failed to save interface : Missing or invalid interface name

这是 Webmin 中的一个已知错误,所以我们现在就跳过它。

直接修改 /etc/network/interfaces

auto lo
iface lo inet loopback
iface lo inet6 loopback

auto enp2s0
allow-hotplug enp2s0
iface enp2s0 inet static
  address XX.4.87.10
  netmask 255.255.255.224
  gateway XX.4.87.1
  # route XX.4.87.0/27 via XX.4.87.1
  up route add -net XX.4.87.0 netmask 255.255.255.224 gw XX.4.87.1 dev enp2s0

iface enp2s0 inet static
  address XX.4.87.47
  netmask 255.255.255.224
  gateway XX.4.87.33
  up route add -net XX.4.87.0 netmask 255.255.255.224 gw XX.4.87.33 dev enp2s0

iface enp2s0 inet static
  address XX.4.87.37
  netmask 255.255.255.224
  gateway XX.4.87.33
  up route add -net XX.4.87.0 netmask 255.255.255.224 gw XX.4.87.33 dev enp2s0

iface enp2s0 inet6 static
  address XXXX:4f8:140:701e::2
  netmask 64
  gateway fe80::1

列表中的第一个(主 IP)和第二个始终可连接,但第三个始终不可连接。所以我能够切换 IP 顺序,并且可以在重启后访问我的服务器,我做错了什么?

(我也尝试过 Debian Docs 的传统方法,但这导致一切都崩溃了)

root@hdtu1 ~ # systemctl status networking.service
● networking.service - Raise network interfaces
   Loaded: loaded (/lib/systemd/system/networking.service; enabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Wed 2018-08-01 22:40:12 CEST; 7s ago
     Docs: man:interfaces(5)
  Process: 2184 ExecStart=/sbin/ifup -a --read-environment (code=exited, status=1/FAILURE)
  Process: 2179 ExecStartPre=/bin/sh -c [ "$CONFIGURE_INTERFACES" != "no" ] && [ -n "$(ifquery --read-environment --list --exclude
 Main PID: 2184 (code=exited, status=1/FAILURE)

Aug 01 22:40:12 hdtu1 systemd[1]: Starting Raise network interfaces...
Aug 01 22:40:12 hdtu1 ifup[2184]: RTNETLINK answers: File exists
Aug 01 22:40:12 hdtu1 ifup[2184]: ifup: failed to bring up enp2s0
Aug 01 22:40:12 hdtu1 systemd[1]: networking.service: Main process exited, code=exited, status=1/FAILURE
Aug 01 22:40:12 hdtu1 systemd[1]: Failed to start Raise network interfaces.
Aug 01 22:40:12 hdtu1 systemd[1]: networking.service: Unit entered failed state.
Aug 01 22:40:12 hdtu1 systemd[1]: networking.service: Failed with result 'exit-code'.

答案1

一个接口不能有多个默认网关,而且我认为向同一个接口添加多个 IP 的方式是不正确的。

使用以下符号添加辅助 IP:

iface enp2s0 inet static
  address XX.4.87.10
  netmask 255.255.255.224
  gateway XX.4.87.1
  # route XX.4.87.0/27 via XX.4.87.1
  up route add -net XX.4.87.0 netmask 255.255.255.224 gw XX.4.87.1 dev enp2s0

iface enp2s0:1 inet static
  address XX.4.87.47
  netmask 255.255.255.224
  # I removed the "gateway" instruction in this block, as it is a secondary IP
  up route add -net XX.4.87.0 netmask 255.255.255.224 gw XX.4.87.33 dev enp2s0

答案2

我的 Debian 9 使用来自@Marc R 的提示就是这样工作的。

# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

source /etc/network/interfaces.d/*

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
allow-hotplug   enp0s3
#iface enp0s3   inet dhcp

iface enp0s3    inet static
address     192.168.0.203
netmask     255.255.255.0
gateway     192.168.0.1

iface enp0s3    inet static
address     192.168.0.204
netmask     255.255.255.0

通常我会使用 eth0:1 或 devicename:1 来实现多 ip,但现在令我惊讶的是,Debian 9 看起来可以自行处理分割。

相关内容