我正在 Ubuntu 上测试一个网络程序。有没有办法在 Ubuntu 中创建虚拟网络接口。类似的东西lo
很好,但不幸的是,上面有很多lo
带有127.0.0.1
源和目标 IP 地址的数据包。
答案1
使用案例:在服务器中创建持久 VIP 地址,以用作“环回”地址
切换到 /etc/systemd/network/ 目录
user@server:~$ cd /etc/systemd/network/
创建两个文件“vip.netdev”和“vip.network”
user@server:/etc/systemd/network$ touch vip.netdev vip.network
显示新创建的文件
user@server:/etc/systemd/network$ ls
vip.netdev vip.network
使用您喜欢的编辑器编辑这两个文件以反映下面的细节
user@server:/etc/systemd/network$ more vip.netdev
[NetDev]
Name=vip
Kind=dummy
... 和 ...
user@server:/etc/systemd/network$ more vip.network
[Match]
Name=vip
[Network]
Address=172.16.1.23 (or whatever address you so choose)
Mask=255.255.255.255
Broadcast=172.16.1.255 (match the Address x.x.x.255)
启用新创建的 VIP 接口,无需重启服务器
user@server:/etc/systemd/network$ systemctl restart systemd-networkd
显示新创建的VIP界面
user@server:/etc/systemd/network$ ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.16.1.16 netmask 255.255.255.0 broadcast 172.16.1.255
inet6 fe80::20c:29ff:fe9b:703f prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:9b:70:3f txqueuelen 1000 (Ethernet)
RX packets 244 bytes 32605 (32.6 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 653 bytes 51807 (51.8 KB)
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 411 bytes 66247 (66.2 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 411 bytes 66247 (66.2 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
vip: flags=195<UP,BROADCAST,RUNNING,NOARP> mtu 1500
inet 172.16.1.23 netmask 255.255.0.0 broadcast 172.16.255.255
inet6 fe80::d0a7:56ff:fe83:95ff prefixlen 64 scopeid 0x20<link>
ether d2:a7:56:83:95:ff txqueuelen 1000 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 7 bytes 490 (490.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
... 或者 ...
user@server:/etc/systemd/network$ ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 00:0c:29:9b:70:3f brd ff:ff:ff:ff:ff:ff
inet 172.16.1.16/24 brd 172.16.1.255 scope global ens33
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe9b:703f/64 scope link
valid_lft forever preferred_lft forever
3: vip: <BROADCAST,NOARP,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN group default qlen 1000
link/ether d2:a7:56:83:95:ff brd ff:ff:ff:ff:ff:ff
inet 172.16.1.23/16 brd 172.16.255.255 scope global vip
valid_lft forever preferred_lft forever
inet6 fe80::d0a7:56ff:fe83:95ff/64 scope link
valid_lft forever preferred_lft forever
答案2
您可以使用以下命令添加另一个环回接口:
ip link add name mylo type dummy
ifconfig mylo up
用作mylo
环回接口。