0
点赞
收藏
分享

微信扫一扫

Linux Bridge与veth

创建一对 veth(Virtual Ethernet)接口,并将它们连接到不同的网络命名空间,然后通过 Linux Bridge 相连

# 创建第一个veth对
ip netns add ns1                  # 创建命名空间 ns1
ip netns exec ns1 ip link set lo up # 在 ns1 中启用 loopback 接口

ip link add veth1 type veth peer name veth2 # 创建 veth1 和 veth2 接口
ip link set veth1 netns ns1       # 将 veth1 接口移动到命名空间 ns1

# 创建第二个veth对
ip netns add ns2                  # 创建命名空间 ns2
ip netns exec ns2 ip link set lo up # 在 ns2 中启用 loopback 接口

ip link set veth2 netns ns2       # 将 veth2 接口移动到命名空间 ns2

# 创建 Linux Bridge
ip link add name br0 type bridge  # 创建一个名为 br0 的 Linux Bridge
ip link set dev br0 up            # 启用 Linux Bridge

# 连接 veth 接口到 Linux Bridge
ip link set veth1 master br0      # 将 veth1 接口连接到 br0
ip link set veth2 master br0      # 将 veth2 接口连接到 br0

ip netns exec ns1 ip addr add 192.168.1.1/24 dev veth1   # 配置 veth1 的IP地址为 192.168.1.1/24
ip netns exec ns1 ip link set veth1 up                   # 启用 veth1 接口

ip netns exec ns2 ip addr add 192.168.1.2/24 dev veth2   # 配置 veth2 的IP地址为 192.168.1.2/24
ip netns exec ns2 ip link set veth2 up                   # 启用 veth2 接口

check

root@Copy-of-VM-U2204:~# ip netns exec ns2 ip r
192.168.1.0/24 dev veth2 proto kernel scope link src 192.168.1.2
root@Copy-of-VM-U2204:~# ip netns exec ns1 ip r
192.168.1.0/24 dev veth1 proto kernel scope link src 192.168.1.1
root@Copy-of-VM-U2204:~# ip netns exec ns2 ping  192.168.1.1
PING 192.168.1.1 (192.168.1.1) 56(84) bytes of data.
64 bytes from 192.168.1.1: icmp_seq=1 ttl=64 time=0.011 ms
64 bytes from 192.168.1.1: icmp_seq=2 ttl=64 time=0.009 ms
64 bytes from 192.168.1.1: icmp_seq=3 ttl=64 time=0.010 ms
^C
--- 192.168.1.1 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2029ms
rtt min/avg/max/mdev = 0.009/0.010/0.011/0.000 ms
root@Copy-of-VM-U2204:~# ip netns exec ns2 ping  192.168.1.2
PING 192.168.1.2 (192.168.1.2) 56(84) bytes of data.
64 bytes from 192.168.1.2: icmp_seq=1 ttl=64 time=0.007 ms
64 bytes from 192.168.1.2: icmp_seq=2 ttl=64 time=0.010 ms

借别人一个图

Linux Bridge与veth_命名空间

举报

相关推荐

0 条评论