ip addr add 192.168.33.13/24 dev br0 ip linkset up br0
查看IP配置:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
[root@localhost vagrant]# ip a ... 3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 08:00:27:f5:af:5a brd ff:ff:ff:ff:ff:ff inet 192.168.33.12/24 brd 192.168.33.255 scope global eth1 valid_lft forever preferred_lft forever inet6 fe80::a00:27ff:fef5:af5a/64 scope link valid_lft forever preferred_lft forever 4: ovs-system: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN link/ether 0a:95:b4:09:9d:a2 brd ff:ff:ff:ff:ff:ff 5: br0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN link/ether a2:fe:04:72:0e:4f brd ff:ff:ff:ff:ff:ff inet 192.168.33.13/24 scope global br0 valid_lft forever preferred_lft forever inet6 fe80::a0fe:4ff:fe72:e4f/64 scope link valid_lft forever preferred_lft forever
此时,我们在宿主机上访问192.168.33.13,可以成功:
1 2 3 4 5 6 7
flygoast:~ $ ping 192.168.33.13 -c 1 PING 192.168.33.13 (192.168.33.13): 56 data bytes 64 bytes from 192.168.33.13: icmp_seq=0 ttl=64 time=0.682 ms
--- 192.168.33.13 ping statistics --- 1 packets transmitted, 1 packets received, 0.0% packet loss round-trip min/avg/max/stddev = 0.682/0.682/0.682/0.000 ms
查看MAC地址表:
1 2 3 4 5
flygoast:~ $ arp -an ? (192.168.0.1) at d0:fa:1d:62:a3:84 on en1 ifscope [ethernet] ? (192.168.33.12) at 8:0:27:f5:af:5a on vboxnet0 ifscope [ethernet] ? (192.168.33.13) at 8:0:27:f5:af:5a on vboxnet0 ifscope [ethernet] ? (224.0.0.251) at 1:0:5e:0:0:fb on en1 ifscope permanent [ethernet]
[root@localhost vagrant]# ip netns add ns1 [root@localhost vagrant]# ip netns list ns1
将p1移入namespace ns1中并查看:
1 2 3 4 5 6
[root@localhost vagrant]# ip link set p1 netns ns1 [root@localhost vagrant]# ip netns exec ns1 ip a 1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 6: p1: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN link/ether 42:f6:f2:75:df:60 brd ff:ff:ff:ff:ff:ff
给接口p1配置IP: 192.168.33.14/24, 并开启接口:
1 2 3 4 5 6 7 8 9 10 11
[root@localhost vagrant]# ip netns exec ns1 ip addr add 192.168.33.14/24 dev p1 [root@localhost vagrant]# ip netns exec ns1 ip link set up p1 [root@localhost vagrant]# ip netns exec ns1 ip a 1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 6: p1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN link/ether 42:f6:f2:75:df:60 brd ff:ff:ff:ff:ff:ff inet 192.168.33.14/24 scope global p1 valid_lft forever preferred_lft forever inet6 fe80::40f6:f2ff:fe75:df60/64 scope link valid_lft forever preferred_lft forever
[root@localhost vagrant]# ip route default via 10.0.2.2 dev eth0 proto static metric 100 10.0.2.0/24 dev eth0 proto kernel scope link src 10.0.2.15 metric 100 169.254.0.0/16 dev eth1 scope link metric 1003 192.168.33.0/24 dev eth1 proto kernel scope link src 192.168.33.12 192.168.33.0/24 dev br0 proto kernel scope link src 192.168.33.13
[root@localhost vagrant]# ip route del 192.168.33.0/24 dev eth1 [root@localhost vagrant]# ip route add 192.168.33.1/32 dev eth1 [root@localhost vagrant]# ip route default via 10.0.2.2 dev eth0 proto static metric 100 10.0.2.0/24 dev eth0 proto kernel scope link src 10.0.2.15 metric 100 169.254.0.0/16 dev eth1 scope link metric 1003 192.168.33.0/24 dev br0 proto kernel scope link src 192.168.33.13 192.168.33.1 dev eth1 scope link
此时再从宿主机访问192.168.33.14, 访问成功:
1 2 3 4 5 6 7 8
flygoast:~ $ ping -c 2 192.168.33.14 PING 192.168.33.14 (192.168.33.14): 56 data bytes 64 bytes from 192.168.33.14: icmp_seq=0 ttl=63 time=0.612 ms 64 bytes from 192.168.33.14: icmp_seq=1 ttl=63 time=0.737 ms
--- 192.168.33.14 ping statistics --- 2 packets transmitted, 2 packets received, 0.0% packet loss round-trip min/avg/max/stddev = 0.612/0.674/0.737/0.062 ms
再来查看MAC地址:
1 2 3 4 5 6
flygoast:~ $ arp -an ? (192.168.0.1) at d0:fa:1d:62:a3:84 on en1 ifscope [ethernet] ? (192.168.33.12) at 8:0:27:f5:af:5a on vboxnet0 ifscope [ethernet] ? (192.168.33.13) at 8:0:27:f5:af:5a on vboxnet0 ifscope [ethernet] ? (192.168.33.14) at 8:0:27:f5:af:5a on vboxnet0 ifscope [ethernet] ? (224.0.0.251) at 1:0:5e:0:0:fb on en1 ifscope permanent [ethernet]
[root@localhost vagrant]# ip neigh 10.0.2.2 dev eth0 lladdr 52:54:00:12:35:02 DELAY 192.168.33.1 dev eth1 lladdr 0a:00:27:00:00:00 STALE 10.0.2.3 dev eth0 lladdr 52:54:00:12:35:03 STALE 192.168.33.14 dev br0 lladdr 42:f6:f2:75:df:60 STALE
flygoast:~ $ arp -an ? (192.168.0.1) at d0:fa:1d:62:a3:84 on en1 ifscope [ethernet] ? (192.168.33.12) at 8:0:27:f5:af:5a on vboxnet0 ifscope [ethernet] ? (192.168.33.13) at 8:0:27:f5:af:5a on vboxnet0 ifscope [ethernet] ? (192.168.33.14) at 8:0:27:f5:af:5a on vboxnet0 ifscope [ethernet] ? (192.168.33.16) at 8:0:27:f5:af:5a on vboxnet0 ifscope [ethernet] ? (224.0.0.251) at 1:0:5e:0:0:fb on en1 ifscope permanent [ethernet]
flygoast:~ $ arp -an ? (192.168.0.1) at d0:fa:1d:62:a3:84 on en1 ifscope [ethernet] ? (192.168.33.14) at 8:0:27:f5:af:5a on vboxnet0 ifscope [ethernet] ? (192.168.33.16) at (incomplete) on vboxnet0 ifscope [ethernet] ? (224.0.0.251) at 1:0:5e:0:0:fb on en1 ifscope permanent [ethernet]