Debian 11 上的持久静态路由

Debian 11 上的持久静态路由

我正在尝试建立一个测试实验室,如下所示:

PD:不是Metasploit,而是Metasploitable。

我正在运行 VirtualBox,路由器是 2 个运行 Debian 11 的虚拟机。每个路由器有 2 个接口:

~# ls /sys/class/net/
enp0s3    enp0s8    lo

为了路由器1

~# cat /etc/network/interfaces
[...]
# The primary network interface
auto enp0s3
iface enp0s3 inet static
        address 192.168.10.254
        netmask 255.255.255.0
auto enp0s8
iface enp0s8 inet static
        address 192.168.100.1
        netmask 255.255.255.0

为了路由器2

~# cat /etc/network/interfaces
[...]
# The primary network interface
auto enp0s3
iface enp0s3 inet static
        address 192.168.100.2
        netmask 255.255.255.0
auto enp0s8
iface enp0s8 inet static
        address 192.168.20.254
        netmask 255.255.255.0

我尝试添加路线路由器1

~# ip route add 192.168.20.0/24 via 192.168.100.2 dev enp0s8

并为路由器2

~# ip route add 192.168.10.0/24 via 192.168.100.1 dev enp0s3

ip route show为了路由器1

169.254.0.0/16 dev enp0s3 scope link metric 1000
192.168.10.0/24 dev enp0s3 proto kernel scope link src 192.168.10.254
192.168.20.0/24 via 192.168.100.2 dev enp0s8
192.168.100.0/24 dev enp0s8 proto kernel scope link src 192.168.100.1

ip route show为了路由器2

169.254.0.0/16 dev enp0s3 scope link metric 1000
192.168.10.0/24 via 192.168.100.1 dev enp0s3
192.168.20.0/24 dev enp0s3 proto kernel scope link src 192.168.20.254
192.168.100.0/24 dev enp0s8 proto kernel scope link src 192.168.100.2

后来,我发现这样做的变化不会持久,所以我尝试了所有这些

https://www.mybluelinux.com/debian-permanent-static-routes/

当我重新启动网络服务时,通过systemctl restart networking/etc/init.d/networking restart输出是

Jobs for networking.service failed because the control process exited with error code.
See "systemctl status networking.service" and "journal -xe" for details.

systemctl status networking.service输出:

Loaded: loaded (/lib/systemd/system/networking.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code)
Process: 478 ExecStart=/sbin/ifup -a --read-enviroment (code=exited, status=1/FAILURE)


router1 systemd[1]: Starting Raise network interfaces...
router1 ifup[499]: RTNETLINK answers: File existis
router1 ifup[478]: ifup: failed to bring up enp0s8
router1 systemd[1]: networking.service: Main process exited, code=exited, status=1/FAILURE
router1 systemd[1]: networking.service: Failed with result 'exit-code'.
router1 systemd[1]: Failed to start Raise network interfaces.

journalctl -u networking.service输出:

--Boot blabla hash blabla
router1 systemd[1]: Starting Raise network interfaces...
router1 ifup[499]: RTNETLINK answers: No such process
router1 ifup[478]: ifup: failed to bring up enp0s8
router1 systemd[1]: networking.service: Main process exited, code=exited, status=1/FAILURE
router1 systemd[1]: networking.service: Failed with result 'exit-code'.
router1 systemd[1]: Failed to start Raise network interfaces.
--Boot blabla other hash blabla
router1 systemd[1]: Starting Raise network interfaces...
router1 ifup[499]: RTNETLINK answers: File existis
router1 ifup[478]: ifup: failed to bring up enp0s8
router1 systemd[1]: networking.service: Main process exited, code=exited, status=1/FAILURE
router1 systemd[1]: networking.service: Failed with result 'exit-code'.
router1 systemd[1]: Failed to start Raise network interfaces.

cat /etc/network/interfaces对于路由器 1:

# 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
auto enp0s3
iface enp0s3 inet static
        address 192.168.10.254
        netmask 255.255.255.0
auto enp0s8
iface enp0s8 inet static
        address 192.168.100.1
        netmask 255.255.255.0
#       gateway 192.168.10.254
        up ip route del 192.168.20.0/24 via 192.168.100.2 dev enp0s8
        up ip route add 192.168.20.0/24 via 192.168.100.2 dev enp0s8

然后,如果我尝试重新启动系统,启动时会出现错误“启动网络接口失败”

所以这就是我迷路的地方。有任何想法吗?

答案1

你有 2向上命令。将其更改为

post-up ip route add...
post-down ip route del...

并重新启动网络服务或重新启动

答案2

建议:

  1. 使用以下命令检查 systemd 网络配置:

    networkctl -a status
    
  2. 检查失败的服务:

    systemctl --type=service --state=failed
    
  3. 考虑使用 systemd 而不是/etc/network/interfaces.如果您选择这种方法,请转至 /etc/network/interfaces使用/etc/network/interfaces.savesystemd 并配置您的网络。

为了避免所有无法解释的行为,请放弃使用接口并将所有内容转换为 systemd,创建xxx.network文件:

  • /etc/systemd/network/10-enp0s3.network 和
  • /etc/systemd/network/20-enp0s8.network

然后启用systemd-networkd:

systemctl enable systemd-networkd

并重新启动。

相关内容