以下是设置两个网络命名空间和其间的路由器的一系列命令。我想将两者连接起来进行尝试。
正如您在下面使用 ipv4 所看到的,这可以工作,我如何为网络设备分配 ipv6 地址并进行测试?
[email protected]:~# ip netns add enasource
[email protected]:~# ip netns add enadestination
[email protected]:~# ip netns add Router100
[email protected]:~# ip link add veth1-ns type veth peer name veth1-rt
[email protected]:~# ip link add veth2-ns type veth peer name veth2-rt
[email protected]:~# ip link set veth1-ns netns enasource
[email protected]:~# ip link set veth2-ns netns enadestination
[email protected]:~# ip link set veth1-rt netns Router100
[email protected]:~# ip link set veth2-rt netns Router100
[email protected]:~# ip netns exec enasource bash
root@n62-006-049:~# ifconfig -a
lo: flags=8<LOOPBACK> mtu 65536
loop txqueuelen 1000 (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
veth1-ns: flags=4098<BROADCAST,MULTICAST> mtu 1500
ether fe:c7:0f:42:e9:5b 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
root@n62-006-049:~# ifconfig lo up
root@n62-006-049:~# ifconfig veth1-ns 1.1.1.2/24 up
root@n62-006-049:~# exit
exit
[email protected]:~# ip netns exec enadestination bash
root@n62-006-049:~# ifconfig lo up
root@n62-006-049:~# iconfig veth2-ns 2.2.2.2/24
bash: iconfig: command not found
root@n62-006-049:~# ifconfig veth2-ns 2.2.2.2/24 up
root@n62-006-049:~# exit
exit
[email protected]:~# ip netns exec Router100 bash
root@n62-006-049:~# ifconfig -a
lo: flags=8<LOOPBACK> mtu 65536
loop txqueuelen 1000 (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
veth1-rt: flags=4098<BROADCAST,MULTICAST> mtu 1500
ether 02:cd:e4:82:0e:5f 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
veth2-rt: flags=4098<BROADCAST,MULTICAST> mtu 1500
ether ba:a8:6f:a8:c8:7a 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
root@n62-006-049:~# ifconfig lo up
root@n62-006-049:~# ifconfig veth1-rt 1.1.1.1/24 up
root@n62-006-049:~# ifconfig veth2-rt 2.2.2.1/24 up
root@n62-006-049:~# ping 1.1.1.2
PING 1.1.1.2 (1.1.1.2) 56(84) bytes of data.
64 bytes from 1.1.1.2: icmp_seq=1 ttl=64 time=0.033 ms
64 bytes from 1.1.1.2: icmp_seq=2 ttl=64 time=0.004 ms
64 bytes from 1.1.1.2: icmp_seq=3 ttl=64 time=0.005 ms
64 bytes from 1.1.1.2: icmp_seq=4 ttl=64 time=0.003 ms
^C
--- 1.1.1.2 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3061ms
rtt min/avg/max/mdev = 0.003/0.011/0.033/0.012 ms
root@n62-006-049:~# ping 2.2.2.2
PING 2.2.2.2 (2.2.2.2) 56(84) bytes of data.
64 bytes from 2.2.2.2: icmp_seq=1 ttl=64 time=0.029 ms
64 bytes from 2.2.2.2: icmp_seq=2 ttl=64 time=0.007 ms
64 bytes from 2.2.2.2: icmp_seq=3 ttl=64 time=0.004 ms
^C
--- 2.2.2.2 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2066ms
rtt min/avg/max/mdev = 0.004/0.013/0.029/0.011 ms
root@n62-006-049:~# exit
exit
[email protected]:~# ip netns exec enasource bash
root@n62-006-049:~# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
1.1.1.0 0.0.0.0 255.255.255.0 U 0 0 0 veth1-ns
root@n62-006-049:~# ping 2.2.2.1
connect: Network is unreachable
root@n62-006-049:~# route add -net 0.0.0.0/0 gw 1.1.1.1
root@n62-006-049:~# ping 2.2.2.1
PING 2.2.2.1 (2.2.2.1) 56(84) bytes of data.
64 bytes from 2.2.2.1: icmp_seq=1 ttl=64 time=0.015 ms
64 bytes from 2.2.2.1: icmp_seq=2 ttl=64 time=0.006 ms
64 bytes from 2.2.2.1: icmp_seq=3 ttl=64 time=0.004 ms
64 bytes from 2.2.2.1: icmp_seq=4 ttl=64 time=0.003 ms
64 bytes from 2.2.2.1: icmp_seq=5 ttl=64 time=0.006 ms
^C
--- 2.2.2.1 ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4099ms
rtt min/avg/max/mdev = 0.003/0.006/0.015/0.005 ms
root@n62-006-049:~#
答案1
与通常分配 IPv4 地址的方式相同 – 使用ip address add
:
ip link set veth2-ns up
ip addr add 2.2.2.2/24 dev veth2-ns
ip addr add 2001:db8::2/64 dev veth2-ns
ip route add 0.0.0.0/0 via 2.2.2.1
ip route add ::/0 via 2001:db8::1