我正在寻找一种干净的、“现代”的方式来配置、启动和停止dummy0
网络接口(从dummy
内核模块)。
我/etc/network/interfaces
曾经在较旧的系统上工作,但现在却默默地失败了ifup dummy0
:
iface dummy0 inet static
address 10.10.0.1
netmask 255.255.255.0
# post-up ip link set dummy0 multicast on
取消注释该post-up
行会产生此错误(显示它运行但从未创建接口):dummy0: post-up cmd 'ip link set dummy0 multicast on'failed: returned 1 (Cannot find device "dummy0")
这个shell脚本完美运行但这不是一个干净的配置文件:
#!/bin/sh
sudo ip link add dummy0 type dummy
sudo ip link set dummy0 multicast on
sudo ip addr add 10.10.0.1/24 dev dummy0
sudo ip link set dummy0 up
我的意图就是同时使用它手动并与系统服务:
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/sbin/ifup dummy0
ExecStop=/sbin/ifdown dummy0
StandardOutput=syslog+console
环境:
- 库班图 18.04.2 LTS
- 网络管理器 1.10.6
- iproute2 4.15.0
- ifupdown2 1.0
- systemd 237 +PAM +AUDIT +SELINUX +IMA +APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ +LZ4 +SECCOMP +BLKID +ELFUTILS +KMOD -IDN2 +IDN -PCRE2 默认层次结构=混合
问题:
- 如何将 shell 脚本转换为工作
/etc/network/interfaces
配置? - 还有其他更清洁或推荐的方法可以做到这一点吗?
答案1
该界面不是之前“创建”的; ifupdown 依赖于它在“虚拟”内核模块加载后立即神奇地出现。这是旧的兼容性行为,并且(AFAIIRC)它还会干扰相同接口名称的显式创建,因此通过模块参数将其禁用。现在必须以与创建 dummy1 或 dummyfoobar 相同的方式创建 dummy0。
您应该能够在“pre-up”命令中创建接口:
iface dummy0 inet static
address 10.10.0.1/24
pre-up ip link add dummy0 type dummy
如果您还在该系统上使用 NetworkManager,则最新的 NM 版本支持虚拟接口。
nmcli con add type dummy ifname dummy0 ipv4.addresses 10.10.0.1/24 [...]
如果接口应在启动时创建并永久保留,则可以使用 systemd-networkd 来完成(一个 .netdev 配置用于创建设备,一个 .network 配置用于设置 IP 地址)。但是,“networkctl”仍然没有手动“向上”或“向下”子命令。
答案2
最近我需要在 Oracle Linux 8(标准 4.18 内核)上引入两个虚拟接口。自动清理配置文件对我来说并不重要,我在第一篇文章中使用了 Oleg 的想法和他们的脚本以及文章中的 NetworkManager 脚本RHEL 上的虚拟接口并编写了两个简单的脚本来启动两个虚拟接口
- 加载虚拟模块
# echo "dummy" > /etc/modules-load.d/dummy.conf
- 确定两个虚拟接口
# echo "options dummy numdummies=2" > /etc/modprobe.d/dummy.conf
在 目录中创建了两个脚本 - 例如98-dummy0
和 example :99-dummy1
/etc/NetworkManager/dispatcher.d/
98-虚拟0
#!/bin/sh
# See the "DISPATCHER SCRIPTS" section of `man NetworkManager`.
# Remember to make this file executable!
# Create the dummy interface.
/sbin/ip link add dummy0 type dummy
/sbin/ip link set dummy0 multicast on
/sbin/ip addr add 10.10.0.1/24 dev dummy0
/sbin/ip link set dummy0 up
将99-dummy1
IP 地址更改为10.10.0.2/24
并重命名为dummy1
.
重启并检查
[mvg@oracle ~]$ ifconfig
dummy0: flags=4291<UP,BROADCAST,RUNNING,NOARP,MULTICAST> mtu 1500
inet 10.10.0.1 netmask 255.255.255.0 broadcast 0.0.0.0
inet6 fe80::e4b7:27ff:fe33:73e9 prefixlen 64 scopeid 0x20<link>
ether e6:b7:27:33:73:e9 txqueuelen 1000 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 84 bytes 10858 (10.6 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
dummy1: flags=4291<UP,BROADCAST,RUNNING,NOARP,MULTICAST> mtu 1500
inet 10.10.0.2 netmask 255.255.255.0 broadcast 0.0.0.0
inet6 fe80::7c8a:e7ff:feee:b648 prefixlen 64 scopeid 0x20<link>
ether 7e:8a:e7:ee:b6:48 txqueuelen 1000 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 78 bytes 10306 (10.0 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
enp3s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.1.131 netmask 255.255.255.0 broadcast 192.168.1.255
inet6 fdb0:34c:6e16:0:a01e:6127:6aa7:1bba prefixlen 64 scopeid 0x0<global>
inet6 fe80::2cd7:76d7:c757:6b30 prefixlen 64 scopeid 0x20<link>
ether 94:de:80:07:50:d2 txqueuelen 1000 (Ethernet)
RX packets 15759 bytes 14474446 (13.8 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 11577 bytes 1411688 (1.3 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 112 bytes 9480 (9.2 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 112 bytes 9480 (9.2 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
virbr0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255
ether 52:54:00:6b:09:b3 txqueuelen 1000 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
虚拟接口已 ping 通。