$ ip link add type veth
$ ip ad
1: lo:<LOOPBACK> mtu 65536 qdisc noop state DOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:0081: veth0:<BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN qlen 1000
link/ether 12:39:09:81:3a:dd brd ff:ff:ff:ff:ff:ff
82: veth1:<BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN qlen 1000
link/ether 32:4f:fd:cc:79:1b brd ff:ff:ff:ff:ff:ff
$ ip ad
1: lo:<LOOPBACK> mtu 65536 qdisc noop state DOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
$ ip netns exec net0 ip ad
1: lo:<LOOPBACK> mtu 65536 qdisc noop state DOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:0081: veth0:<BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN qlen 1000
link/ether 12:39:09:81:3a:dd brd ff:ff:ff:ff:ff:ff
$ ip netns exec net1 ip ad
1: lo:<LOOPBACK> mtu 65536 qdisc noop state DOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:0082: veth1:<BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN qlen 1000
link/ether 32:4f:fd:cc:79:1b brd ff:ff:ff:ff:ff:ff
$ ip netns exec net0 ip link set veth0 up
$ ip netns exec net0 ip address add 10.0.1.1/24 dev veth0
$ ip netns exec net1 ip link set veth1 up
$ ip netns exec net1 ip address add 10.0.1.2/24 dev veth1
分别配置好两个设备,然后用ping测试一下联通性:
$ ip netns exec net0 ping -c 310.0.1.2
PING 10.0.1.2(10.0.1.2)56(84) bytes of data.64 bytes from 10.0.1.2:icmp_req=1ttl=64time=0.101 ms
64 bytes from 10.0.1.2:icmp_req=2ttl=64time=0.057 ms
64 bytes from 10.0.1.2:icmp_req=3ttl=64time=0.048 ms
--- 10.0.1.2 ping statistics ---
3 packets transmitted,3 received,0% packet loss, time 1999ms
rtt min/avg/max/mdev =0.048/0.068/0.101/0.025 ms
标题
创建虚拟网络环境并且连接网线
ip netns add net0
ip netns add net1
ip netns add bridge
ip link add type veth
ip link set dev veth0 name net0-bridge netns net0
ip link set dev veth1 name bridge-net0 netns bridge
ip link add type veth
ip link set dev veth0 name net1-bridge netns net1
ip link set dev veth1 name bridge-net1 netns bridge
在bridge中创建并且设置br设备
ip netns exec bridge brctl addbr br
ip netns exec bridge ip link set dev br up
ip netns exec bridge ip link set dev bridge-net0 up
ip netns exec bridge ip link set dev bridge-net1 up
ip netns exec bridge brctl addif br bridge-net0
ip netns exec bridge brctl addif br bridge-net1
然后配置两个虚拟环境的网卡
ip netns exec net0 ip link set dev net0-bridge up
ip netns exec net0 ip address add 10.0.1.1/24 dev net0-bridge
ip netns exec net1 ip link set dev net1-bridge up
ip netns exec net1 ip address add 10.0.1.2/24 dev net1-bridge
测试
$ ip netns exec net0 ping -c 310.0.1.2
PING 10.0.1.2(10.0.1.2)56(84) bytes of data.64 bytes from 10.0.1.2:icmp_req=1ttl=64time=0.121 ms
64 bytes from 10.0.1.2:icmp_req=2ttl=64time=0.072 ms
64 bytes from 10.0.1.2:icmp_req=3ttl=64time=0.069 ms
--- 10.0.1.2 ping statistics ---
3 packets transmitted,3 received,0% packet loss, time 1999ms
rtt min/avg/max/mdev =0.069/0.087/0.121/0.025 ms
[root@localhost ~]# ip netns help
Usage: ip netns list
ip netns add NAME
ip netns set NAME NETNSID
ip [-all] netns delete [NAME]
ip netns identify [PID]
ip netns pids NAME
ip [-all] netns exec [NAME] cmd ...
ip netns monitor
ip netns list-id
[root@monitor ~]# ip netns exec r1 ifconfig lo 127.0.0.1 up
[root@monitor ~]# ip netns exec r1 ifconfig -a
lo: flags=73 mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10
loop txqueuelen 0 (Local Loopback)
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
此时的r2并没有地址,因为他们是被隔离的
在网络名称空间上添加一对网卡,一个在r1,一个在r2.
[root@localhost ~]# ip link add veth1.1 type veth peer name veth1.2
[root@localhost ~]# ip link show
1: lo: mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: eno16777736: mtu 1500 qdisc pfifo_fast master br-ex state UP mode DEFAULT qlen 1000
link/ether 00:0c:29:4b:bb:d0 brd ff:ff:ff:ff:ff:ff
3: br-ex: mtu 1500 qdisc noqueue state UP mode DEFAULT
link/ether 00:0c:29:4b:bb:d0 brd ff:ff:ff:ff:ff:ff
4: br-in: mtu 1500 qdisc noqueue state UNKNOWN mode DEFAULT
link/ether 56:8d:9f:d2:96:21 brd ff:ff:ff:ff:ff:ff
5: [email protected]: mtu 1500 qdisc noop state DOWN mode DEFAULT qlen 1000
link/ether 7e:ea:fe:98:30:cd brd ff:ff:ff:ff:ff:ff
6: [email protected]: mtu 1500 qdisc noop state DOWN mode DEFAULT qlen 1000
link/ether a2:48:54:92:c2:ed brd ff:ff:ff:ff:ff:ff
将一对网卡分别添加给2个名称空间。
[root@localhost ~]# ip link set veth1.1 netns r1
[root@localhost ~]# ip link set veth1.2 netns r2
[root@localhost ~]# ip netns add r1
[root@localhost ~]# ip netns add r2
[root@localhost ~]# ip netns add r3
[root@localhost ~]# ip netns list
r3
r2
r1
然后在创建2对虚拟网络,(此处是3对后面要用,我就直接创建了)
[root@localhost ~]# ip link add veth1.1 type veth peer name veth1.2
[root@localhost ~]# ip link add veth2.1 type veth peer name veth2.2
[root@localhost ~]# ip link add veth3.1 type veth peer name veth3.2
[root@localhost ~]# ip link set dev veth1.2 up
[root@localhost ~]# ip link set dev veth2.2 up
[root@localhost ~]# ip link set dev veth3.2 up
[root@localhost ~]# ip link show
1: lo: mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: enp49s0f0: mtu 1500 qdisc mq state UP mode DEFAULT group default qlen 1000
link/ether d4:5d:64:07:a8:ea brd ff:ff:ff:ff:ff:ff
3: enp49s0f1: mtu 1500 qdisc mq state UP mode DEFAULT group default qlen 1000
link/ether d4:5d:64:07:a8:eb brd ff:ff:ff:ff:ff:ff
33: [email protected]: mtu 1500 qdisc noqueue state LOWERLAYERDOWN mode DEFAULT group default qlen 1000
link/ether 0a:85:cc:42:57:4f brd ff:ff:ff:ff:ff:ff
34: [email protected]: mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000
link/ether 0a:54:87:03:1c:8f brd ff:ff:ff:ff:ff:ff
35: [email protected]: mtu 1500 qdisc noqueue state LOWERLAYERDOWN mode DEFAULT group default qlen 1000
link/ether 0a:e4:37:41:e3:c6 brd ff:ff:ff:ff:ff:ff
36: [email protected]: mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000
link/ether fe:74:70:bc:fe:cd brd ff:ff:ff:ff:ff:ff
37: [email protected]: mtu 1500 qdisc noqueue state LOWERLAYERDOWN mode DEFAULT group default qlen 1000
link/ether ba:5a:ea:11:1e:e2 brd ff:ff:ff:ff:ff:ff
38: [email protected]: mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000
link/ether 22:47:55:1e:37:49 brd ff:ff:ff:ff:ff:ff
[root@localhost ~]# ip a s
1: lo: 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: enp49s0f0: mtu 1500 qdisc mq state UP group default qlen 1000
link/ether d4:5d:64:07:a8:ea brd ff:ff:ff:ff:ff:ff
inet 10.10.1.2/24 brd 10.10.1.255 scope global enp49s0f0
valid_lft forever preferred_lft forever
inet6 fe80::d65d:64ff:fe07:a8ea/64 scope link
valid_lft forever preferred_lft forever
3: enp49s0f1: mtu 1500 qdisc mq state UP group default qlen 1000
link/ether d4:5d:64:07:a8:eb brd ff:ff:ff:ff:ff:ff
inet 10.10.1.3/24 brd 10.10.1.255 scope global enp49s0f1
valid_lft forever preferred_lft forever
inet6 fe80::d65d:64ff:fe07:a8eb/64 scope link
valid_lft forever preferred_lft forever
33: [email protected]: mtu 1500 qdisc noqueue state LOWERLAYERDOWN group default qlen 1000
link/ether 0a:85:cc:42:57:4f brd ff:ff:ff:ff:ff:ff
34: [email protected]: mtu 1500 qdisc noop state DOWN group default qlen 1000
link/ether 0a:54:87:03:1c:8f brd ff:ff:ff:ff:ff:ff
35: [email protected]: mtu 1500 qdisc noqueue state LOWERLAYERDOWN group default qlen 1000
link/ether 0a:e4:37:41:e3:c6 brd ff:ff:ff:ff:ff:ff
36: [email protected]: mtu 1500 qdisc noop state DOWN group default qlen 1000
link/ether fe:74:70:bc:fe:cd brd ff:ff:ff:ff:ff:ff
37: [email protected]: mtu 1500 qdisc noqueue state LOWERLAYERDOWN group default qlen 1000
link/ether ba:5a:ea:11:1e:e2 brd ff:ff:ff:ff:ff:ff
38: [email protected]: mtu 1500 qdisc noop state DOWN group default qlen 1000
link/ether 22:47:55:1e:37:49 brd ff:ff:ff:ff:ff:ff
为r1,r2,r3的回环接口添加一个ip地址。
[root@localhost ~]# ip netns exec r1 ifconfig lo 127.0.0.1 up
[root@localhost ~]# ip netns exec r2 ifconfig lo 127.0.0.1 up
[root@localhost ~]# ip netns exec r3 ifconfig lo 127.0.0.1 up
[root@localhost ~]# ip netns exec r1 ip a
1: lo: 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
[root@localhost ~]# ip netns exec r2 ip a
1: lo: 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
[root@localhost ~]# ip netns exec r3 ip a
1: lo: 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
创建桥接
[root@localhost ~]# brctl addbr br-ex
[root@localhost ~]# ip link set br-ex up
[root@localhost ~]# ifconfig
br-ex: flags=4163 mtu 1500
ether 1e:d6:fd:9b:2a:fc txqueuelen 0 (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
给桥设备添加IP地址。
# ip addr del 10.10.1.3/24 dev enp49s0f1
# ip addr add 10.10.1.3/24 dev br-ex
# brctl addif br-ex enp49s0f1
[root@localhost ~]# ip a s
1: lo: 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: enp49s0f0: mtu 1500 qdisc mq state UP group default qlen 1000
link/ether d4:5d:64:07:a8:ea brd ff:ff:ff:ff:ff:ff
inet 10.10.1.2/24 brd 10.10.1.255 scope global enp49s0f0
valid_lft forever preferred_lft forever
inet6 fe80::d65d:64ff:fe07:a8ea/64 scope link
valid_lft forever preferred_lft forever
3: enp49s0f1: mtu 1500 qdisc mq master br-ex state UP group default qlen 1000
link/ether d4:5d:64:07:a8:eb brd ff:ff:ff:ff:ff:ff
inet6 fe80::d65d:64ff:fe07:a8eb/64 scope link
valid_lft forever preferred_lft forever
33: [email protected]: mtu 1500 qdisc noqueue state LOWERLAYERDOWN group default qlen 1000
link/ether 0a:85:cc:42:57:4f brd ff:ff:ff:ff:ff:ff
34: [email protected]: mtu 1500 qdisc noop state DOWN group default qlen 1000
link/ether 0a:54:87:03:1c:8f brd ff:ff:ff:ff:ff:ff
35: [email protected]: mtu 1500 qdisc noqueue state LOWERLAYERDOWN group default qlen 1000
link/ether 0a:e4:37:41:e3:c6 brd ff:ff:ff:ff:ff:ff
36: [email protected]: mtu 1500 qdisc noop state DOWN group default qlen 1000
link/ether fe:74:70:bc:fe:cd brd ff:ff:ff:ff:ff:ff
37: [email protected]: mtu 1500 qdisc noqueue state LOWERLAYERDOWN group default qlen 1000
link/ether ba:5a:ea:11:1e:e2 brd ff:ff:ff:ff:ff:ff
38: [email protected]: mtu 1500 qdisc noop state DOWN group default qlen 1000
link/ether 22:47:55:1e:37:49 brd ff:ff:ff:ff:ff:ff
39: br-ex: mtu 1500 qdisc noqueue state UP group default qlen 1000
link/ether d4:5d:64:07:a8:eb brd ff:ff:ff:ff:ff:ff
inet 10.10.1.3/24 scope global br-ex
valid_lft forever preferred_lft forever
inet6 fe80::b82c:97ff:fe7e:8562/64 scope link
valid_lft forever preferred_lft forever
[root@localhost ~]# brctl show
bridge name bridge id STP enabled interfaces
br-ex 8000.d45d6407a8eb no enp49s0f1
再添加一个桥
[root@localhost ~]# brctl addbr br-in
[root@localhost ~]# ip link set br-in up
将3对网卡分别添加给3个名称空间和网桥br-in中
#r1 <- veth1.1 ;veth1.2>br-in
#ip link set veth1.1 netns r1
#ip link set veth1.2 up
#brctl addif br-in veth1.2
#r2 <- veth2.1 ;veth2.2>br-in
#ip link set veth2.1 netns r2
#ip link set veth2.2 up
#brctl addif br-in veth2.2
#r3 <- veth3.1 ;veth3.2>br-in
#ip link set veth3.1 netns r3
#ip link set veth3.2 up
#brctl addif br-in veth3.2
[root@localhost ~]# ip link show
1: lo: mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: enp49s0f0: mtu 1500 qdisc mq state UP mode DEFAULT group default qlen 1000
link/ether d4:5d:64:07:a8:ea brd ff:ff:ff:ff:ff:ff
3: enp49s0f1: mtu 1500 qdisc mq master br-ex state UP mode DEFAULT group default qlen 1000
link/ether d4:5d:64:07:a8:eb brd ff:ff:ff:ff:ff:ff
33: veth1.2@if34: mtu 1500 qdisc noqueue master br-in state LOWERLAYERDOWN mode DEFAULT group default qlen 1000
link/ether 0a:85:cc:42:57:4f brd ff:ff:ff:ff:ff:ff link-netnsid 1
35: veth2.2@if36: mtu 1500 qdisc noqueue master br-in state LOWERLAYERDOWN mode DEFAULT group default qlen 1000
link/ether 0a:e4:37:41:e3:c6 brd ff:ff:ff:ff:ff:ff link-netnsid 2
37: [email protected]: mtu 1500 qdisc noqueue state LOWERLAYERDOWN mode DEFAULT group default qlen 1000
link/ether ba:5a:ea:11:1e:e2 brd ff:ff:ff:ff:ff:ff
39: br-ex: mtu 1500 qdisc noqueue state UP mode DEFAULT group default qlen 1000
link/ether d4:5d:64:07:a8:eb brd ff:ff:ff:ff:ff:ff
40: br-in: mtu 1500 qdisc noqueue state DOWN mode DEFAULT group default qlen 1000
link/ether 0a:85:cc:42:57:4f brd ff:ff:ff:ff:ff:ff
[root@localhost ~]# brctl show
bridge name bridge id STP enabled interfaces
br-ex 8000.d45d6407a8eb no enp49s0f1
br-in 8000.0a85cc42574f no veth1.2
veth2.2
veth3.2
给每个r2 r3 网络空间ip
先重命名
[root@localhost ~]# ip netns exec r2 ip link set dev veth2.1 name eth0
[root@localhost ~]# ip netns exec r3 ip link set dev veth3.1 name eth0
[root@localhost ~]# ip netns exec r1 ip link set dev veth1.1 name eth0
[root@localhost ~]# ip netns exec r2 ip a
1: lo: 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
36: eth0@if35: mtu 1500 qdisc noop state DOWN group default qlen 1000
link/ether fe:74:70:bc:fe:cd brd ff:ff:ff:ff:ff:ff link-netnsid 0
[root@localhost ~]#
分配ip
[root@localhost ~]# ip netns exec r1 ifconfig eth0 192.168.1.1/24 up
[root@localhost ~]# ip netns exec r2 ifconfig eth0 192.168.1.2/24 up
[root@localhost ~]# ip netns exec r3 ifconfig eth0 192.168.1.3/24 up
[root@localhost ~]# ip netns exec r2 ip a
1: lo: 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
36: eth0@if35: mtu 1500 qdisc noqueue state UP group default qlen 1000
link/ether fe:74:70:bc:fe:cd brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet 192.168.1.2/24 brd 192.168.1.255 scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::fc74:70ff:febc:fecd/64 scope link
valid_lft forever preferred_lft forever
[root@localhost ~]# ip netns exec r3 ip a
1: lo: 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
38: eth0@if37: mtu 1500 qdisc noqueue state UP group default qlen 1000
link/ether 22:47:55:1e:37:49 brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet 192.168.1.3/24 brd 192.168.1.255 scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::2047:55ff:fe1e:3749/64 scope link
valid_lft forever preferred_lft forever
[root@localhost ~]# ip netns exec r1 ip a
1: lo: 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
34: eth0@if33: mtu 1500 qdisc noqueue state UP group default qlen 1000
link/ether 0a:54:87:03:1c:8f brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet 192.168.1.1/24 brd 192.168.1.255 scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::854:87ff:fe03:1c8f/64 scope link
valid_lft forever preferred_lft forever
[root@localhost ~]# ip netns exec r2 ip link set dev eth0 up
[root@localhost ~]# ip netns exec r1 ip link set dev eth0 up
[root@localhost ~]# ip netns exec r3 ip link set dev eth0 up
[root@localhost ~]# ip netns exec r2 ip a
1: lo: 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
36: eth0@if35: mtu 1500 qdisc noqueue state UP group default qlen 1000
link/ether fe:74:70:bc:fe:cd brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet6 fe80::fc74:70ff:febc:fecd/64 scope link
valid_lft forever preferred_lft forever
[root@localhost ~]#
[root@localhost ~]# ip link add veth1.1 type veth peer name veth1.2
[root@localhost ~]# ip link set veth1.1 up
[root@localhost ~]# ip link set veth1.2 up
将网卡添加到桥上去。
[root@localhost ~]# brctl addif br-in veth1.2
[root@localhost ~]# brctl show
bridge name bridge id STP enabled interfaces
br-ex 8000.d45d6407a8eb no enp49s0f1
br-in 8000.0a85cc42574f no veth1.2
veth2.2
veth3.2
给veth1.1改个名字,并且启动
[root@localhost ~]# ip link set veth1.1 netns r1 #将网卡rinr添加至r1
[root@localhost ~]# ip netns exec r1 ip link set veth1.1 name eth0
[root@localhost ~]# ip netns exec r1 ip link set eth0 up
[root@localhost ~]# ip netns exec r2 route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
[root@localhost ~]# ip netns exec r2 route add default gw 192.168.1.1
[root@localhost ~]# ip netns exec r2 route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.1.1 0.0.0.0 UG 0 0 0 eth0
192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
[root@localhost ~]# ip netns exec r3 route add default gw 192.168.1.1
[root@localhost ~]# ip netns exec r3 route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.1.1 0.0.0.0 UG 0 0 0 eth0
192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
目前来说整幅图的左半边完全好了。 开始右半边。
添加一对网卡,再把其中一个桥接
[root@localhost ~]# ip link add veth4.1 type veth peer name veth4.2
[root@localhost ~]# ip link set veth4.1 up
[root@localhost ~]# ip link set veth4.2 up
[root@localhost ~]# brctl show
bridge name bridge id STP enabled interfaces
br-ex 8000.d45d6407a8eb no enp49s0f1
br-in 8000.0a85cc42574f no veth1.2
veth2.2
veth3.2
[root@localhost ~]# brctl addif br-ex veth4.2
[root@localhost ~]# brctl show
bridge name bridge id STP enabled interfaces
br-ex 8000.8aa7e4e58374 no enp49s0f1
veth4.2
br-in 8000.0a85cc42574f no veth1.2
veth2.2
veth3.2
将另一个网卡添加到路由器的另一边,且给个另一个网络的地址
[root@localhost ~]# ip link set veth4.1 netns r1
[root@localhost ~]# ip netns exec r1 ip link set veth4.1 name eth1
[root@localhost ~]# ip netns exec r1 ifconfig eth1 10.10.1.20/24 up
[root@localhost ~]# ip netns exec r1 ip a
1: lo: 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
34: eth0@if33: mtu 1500 qdisc noqueue state UP group default qlen 1000
link/ether 0a:54:87:03:1c:8f brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet 192.168.1.1/24 brd 192.168.1.255 scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::854:87ff:fe03:1c8f/64 scope link
valid_lft forever preferred_lft forever
46: eth1@if45: mtu 1500 qdisc noqueue state UP group default qlen 1000
link/ether 36:69:14:6b:a8:9c brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet 10.10.1.20/24 brd 10.10.1.255 scope global eth1
valid_lft forever preferred_lft forever
inet6 fe80::3469:14ff:fe6b:a89c/64 scope link
valid_lft forever preferred_lft forever
利用防火墙的源地址转换,实现将内网中的地址转换 就是r1中的2个网卡(eth0和eth1)地址转换
[root@localhost ~]# ip netns exec r1 iptables -t nat -A POSTROUTING -s 192.168.1.0/24 ! -d 192.168.1.0/24 -j SNAT --to-source 10.10.1.20
测试。vm1可以ping同vm2.vm1可以访问真机所在局域网的主机。
[root@localhost ~]# ip netns exec r3 ping 10.10.1.20
PING 10.10.1.20 (10.10.1.20) 56(84) bytes of data.
64 bytes from 10.10.1.20: icmp_seq=1 ttl=64 time=0.055 ms
64 bytes from 10.10.1.20: icmp_seq=2 ttl=64 time=0.030 ms
^C
--- 10.10.1.20 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1038ms
rtt min/avg/max/mdev = 0.030/0.042/0.055/0.014 ms
[root@localhost ~]#
[root@localhost ~]# ip netns exec r3 ping 10.10.1.20
PING 10.10.1.20 (10.10.1.20) 56(84) bytes of data.
64 bytes from 10.10.1.20: icmp_seq=1 ttl=64 time=0.072 ms
^C
--- 10.10.1.20 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.072/0.072/0.072/0.000 ms
[root@localhost ~]# ip netns exec r3 ping 10.10.1.2
PING 10.10.1.2 (10.10.1.2) 56(84) bytes of data.
64 bytes from 10.10.1.2: icmp_seq=1 ttl=63 time=0.248 ms
^C
--- 10.10.1.2 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.248/0.248/0.248/0.000 ms
[root@localhost ~]# ip netns exec r3 ping 10.10.1.3
PING 10.10.1.3 (10.10.1.3) 56(84) bytes of data.
64 bytes from 10.10.1.3: icmp_seq=1 ttl=63 time=0.194 ms
^C
--- 10.10.1.3 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.194/0.194/0.194/0.000 ms
[root@localhost ~]# ip netns exec r3 ping 10.10.3.160
PING 10.10.3.160 (10.10.3.160) 56(84) bytes of data.
From 192.168.1.1 icmp_seq=1 Destination Net Unreachable
^C
--- 10.10.3.160 ping statistics ---
1 packets transmitted, 0 received, +1 errors, 100% packet loss, time 0ms
[root@localhost ~]# ip netns exec r3 ping 10.10.1.5
PING 10.10.1.5 (10.10.1.5) 56(84) bytes of data.
64 bytes from 10.10.1.5: icmp_seq=1 ttl=63 time=0.433 ms
^C
--- 10.10.1.5 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.433/0.433/0.433/0.000 ms
[root@localhost ~]# ip netns exec r3 ping 10.10.1.6
PING 10.10.1.6 (10.10.1.6) 56(84) bytes of data.
64 bytes from 10.10.1.6: icmp_seq=1 ttl=63 time=0.426 ms
^C
--- 10.10.1.6 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.426/0.426/0.426/0.000 ms
[root@localhost ~]# ip netns exec r3 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.054 ms
^C
--- 192.168.1.2 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.054/0.054/0.054/0.000 ms
[root@localhost ~]# ip netns exec r3 ping 192.168.1.3
PING 192.168.1.3 (192.168.1.3) 56(84) bytes of data.
64 bytes from 192.168.1.3: icmp_seq=1 ttl=64 time=0.031 ms
^C
--- 192.168.1.3 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.031/0.031/0.031/0.000 ms
[root@localhost ~]# ip netns exec r3 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.054 ms
^C
--- 192.168.1.1 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.054/0.054/0.054/0.000 ms
[root@localhost ~]# ^C
[root@localhost ~]#
[root@localhost ~]# ip netns exec r3 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.070 ms
^C
--- 192.168.1.1 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.070/0.070/0.070/0.000 ms
[root@localhost ~]# ip netns exec r3 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.045 ms
^C
--- 192.168.1.2 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.045/0.045/0.045/0.000 ms
[root@localhost ~]# ip netns exec r3 ping 192.168.1.3
PING 192.168.1.3 (192.168.1.3) 56(84) bytes of data.
64 bytes from 192.168.1.3: icmp_seq=1 ttl=64 time=0.027 ms
^C
--- 192.168.1.3 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.027/0.027/0.027/0.000 ms
[root@localhost ~]# ip netns exec r3 ping 192.168.1.4
PING 192.168.1.4 (192.168.1.4) 56(84) bytes of data.
^C
--- 192.168.1.4 ping statistics ---
1 packets transmitted, 0 received, 100% packet loss, time 0ms
[root@localhost ~]# ip netns exec r3 ping 10.10.1.1
PING 10.10.1.1 (10.10.1.1) 56(84) bytes of data.
64 bytes from 10.10.1.1: icmp_seq=1 ttl=254 time=4.49 ms
^C
--- 10.10.1.1 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 4.494/4.494/4.494/0.000 ms
[root@localhost ~]# ip netns exec r3 ping 10.10.1.2
PING 10.10.1.2 (10.10.1.2) 56(84) bytes of data.
64 bytes from 10.10.1.2: icmp_seq=1 ttl=63 time=0.169 ms
^C
--- 10.10.1.2 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.169/0.169/0.169/0.000 ms
[root@localhost ~]# ip netns exec r3 ping 10.10.1.3
PING 10.10.1.3 (10.10.1.3) 56(84) bytes of data.
64 bytes from 10.10.1.3: icmp_seq=1 ttl=63 time=0.133 ms
^C
--- 10.10.1.3 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.133/0.133/0.133/0.000 ms
[root@localhost ~]# ip netns exec r3 ping 10.10.1.4
PING 10.10.1.4 (10.10.1.4) 56(84) bytes of data.
64 bytes from 10.10.1.4: icmp_seq=1 ttl=63 time=0.260 ms
^C
--- 10.10.1.4 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.260/0.260/0.260/0.000 ms
[root@localhost ~]# ip netns exec r3 ping 10.10.1.5
PING 10.10.1.5 (10.10.1.5) 56(84) bytes of data.
64 bytes from 10.10.1.5: icmp_seq=1 ttl=63 time=0.272 ms
^C
--- 10.10.1.5 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.272/0.272/0.272/0.000 ms
[root@localhost ~]# ip netns exec r3 ping 10.10.1.6
PING 10.10.1.6 (10.10.1.6) 56(84) bytes of data.
64 bytes from 10.10.1.6: icmp_seq=1 ttl=63 time=0.257 ms
^C
--- 10.10.1.6 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.257/0.257/0.257/0.000 ms
[root@localhost ~]# ip netns exec r3 ping 10.10.1.20
PING 10.10.1.20 (10.10.1.20) 56(84) bytes of data.
64 bytes from 10.10.1.20: icmp_seq=1 ttl=64 time=0.054 ms
^C
--- 10.10.1.20 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.054/0.054/0.054/0.000 ms
[root@localhost ~]# ping -I br-ex 10.10.1.20
PING 10.10.1.20 (10.10.1.20) from 10.10.1.3 br-ex: 56(84) bytes of data.
64 bytes from 10.10.1.20: icmp_seq=1 ttl=64 time=0.076 ms
64 bytes from 10.10.1.20: icmp_seq=2 ttl=64 time=0.027 ms
64 bytes from 10.10.1.20: icmp_seq=3 ttl=64 time=0.023 ms
64 bytes from 10.10.1.20: icmp_seq=4 ttl=64 time=0.021 ms
/**
* 要执行的算法,返回结果v
*/
public interface Computable<A, V> {
public V comput(final A arg);
}
/**
* 用于缓存数据
*/
public class Memoizer<A, V> implements Computable<A,