{"id":356806,"date":"2024-05-20T23:37:25","date_gmt":"2024-05-20T23:37:25","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=356806"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=356806","title":{"rendered":"<span>\u0414\u0435\u043b\u0430\u0435\u043c \u00abrouter-on-a-stick\u00bb \u0432 linux namespaces<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-1\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/webt\/xr\/ao\/an\/xraoancc2g0d6hn46ks88r7vu_c.png\" data-src=\"https:\/\/habrastorage.org\/webt\/xr\/ao\/an\/xraoancc2g0d6hn46ks88r7vu_c.png\"\/><br \/>  \u041d\u0435 \u0431\u0443\u0434\u0443 \u0442\u043e\u043c\u0438\u0442\u044c \u0432\u0430\u0441 \u0434\u043e\u043b\u0433\u0438\u043c \u0432\u0441\u0442\u0443\u043f\u043b\u0435\u043d\u0438\u0435\u043c. \u041d\u0438\u0436\u0435 \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u043e\u0435 \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u044d\u0442\u043e\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438. <\/p>\n<p><a name=\"habracut\"><\/a>  <\/p>\n<p>\u0412\u0441\u0435 \u043a\u043e\u043c\u0430\u043d\u0434\u044b \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u043c \u043e\u0442 \u0440\u0443\u0442\u0430.<\/p>\n<p>  <\/p>\n<h2 id=\"sozdanie-neymspeysov-i-virtualnyh-interfeysov\">\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043d\u0435\u0439\u043c\u0441\u043f\u0435\u0439\u0441\u043e\u0432 \u0438 \u0432\u0438\u0440\u0442\u0443\u0430\u043b\u044c\u043d\u044b\u0445 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u043e\u0432<\/h2>\n<p>  <\/p>\n<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043d\u0435\u0439\u043c\u0441\u043f\u0435\u0439\u0441\u044b ns10, ns20, ns30 \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043a\u043b\u0438\u0435\u043d\u0442\u043e\u0432, \u0431\u0440\u0438\u0434\u0436 (\u043a\u043e\u043c\u043c\u0443\u0442\u0430\u0442\u043e\u0440) \u0440\u0430\u0437\u043c\u0435\u0441\u0442\u0438\u043c \u0432 \u043d\u0435\u0439\u043c\u0441\u043f\u0435\u0439\u0441\u0435 nsbridge, \u0440\u0443\u0442\u0435\u0440 \u0432 \u043d\u0435\u0439\u043c\u0441\u043f\u0435\u0439\u0441\u0435 nsrouter. \u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043f\u0430\u0440\u044b \u0432\u0438\u0440\u0442\u0443\u0430\u043b\u044c\u043d\u044b\u0445 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u043e\u0432 veth10 \u2014 veth11, veth20 \u2014 veth21, veth 30 \u2014 veth31, \u043e\u0434\u043d\u0443 \u0441\u0442\u043e\u0440\u043e\u043d\u0443 \u043f\u0430\u0440\u044b \u0437\u0430\u0432\u0435\u0434\u0435\u043c \u0432 \u043d\u0435\u0439\u043c\u0441\u043f\u0435\u0439\u0441\u044b ns10 \u2014 30, \u0432\u0442\u043e\u0440\u0443\u044e \u0432 nsbridge. \u0411\u0440\u0438\u0434\u0436 \u0438 \u0440\u0443\u0442\u0435\u0440 \u0441\u043e\u0435\u0434\u0438\u043d\u0438\u043c \u0435\u0449\u0435 \u043e\u0434\u043d\u043e\u0439 \u043f\u0430\u0440\u043e\u0439 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u043e\u0432 vethbr \u2014 vethrou. <\/p>\n<p>  <\/p>\n<pre><code class=\"plaintext\"># \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u043d\u0435\u0439\u043c\u0441\u043f\u0435\u0439\u0441\u044b ip netns add ns10 ip netns add ns20 ip netns add ns30 ip netns add nsbridge ip netns add nsrouter  # \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u044b ip link add name veth10 type veth peer name veth11 ip link add name veth20 type veth peer name veth21 ip link add name veth30 type veth peer name veth31 ip link add name vethbr type veth peer name vethrou  # \u0437\u0430\u0432\u043e\u0434\u0438\u043c \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u044b \u0432 \u043d\u0435\u0439\u043c\u0441\u043f\u0435\u0439\u0441\u044b ip link set dev veth10 netns ns10   ip link set dev veth20 netns ns20 ip link set dev veth30 netns ns30  ip link set dev veth11 netns nsbridge ip link set dev veth21 netns nsbridge ip link set dev veth31 netns nsbridge ip link set dev vethbr netns nsbridge  ip link set dev vethrou netns nsrouter  # \u0432\u043a\u043b\u044e\u0447\u0430\u0435\u043c \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u044b ip netns exec ns10 ip link set dev lo up ip netns exec ns10 ip link set dev veth10 up ip netns exec ns20 ip link set dev lo up ip netns exec ns20 ip link set dev veth20 up ip netns exec ns30 ip link set dev lo up ip netns exec ns30 ip link set dev veth30 up  ip netns exec nsbridge ip link set dev lo up ip netns exec nsbridge ip link set dev veth11 up ip netns exec nsbridge ip link set dev veth21 up ip netns exec nsbridge ip link set dev veth31 up ip netns exec nsbridge ip link set dev vethbr up  ip netns exec nsrouter ip link set dev lo up ip netns exec nsrouter ip link set dev vethrou up<\/code><\/pre>\n<p>  <\/p>\n<h2 id=\"nastroyka-kommutacii\">\u041d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430 \u043a\u043e\u043c\u043c\u0443\u0442\u0430\u0446\u0438\u0438<\/h2>\n<p>  <\/p>\n<p>\u0418\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u044b, \u0432\u0435\u0434\u0443\u0449\u0438\u0435 \u0432 ns10 \u0438 ns20, \u043f\u043e\u043c\u0435\u0441\u0442\u0438\u043c \u0432 \u043e\u0434\u0438\u043d vlan 10. \u0418\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441, \u0432\u0435\u0434\u0443\u0449\u0438\u0439 \u0432 ns30, \u043f\u043e\u043c\u0435\u0441\u0442\u0438\u043c \u0432 vlan 30. \u0422\u043e\u0433\u0434\u0430 ns10 \u0438 ns20 \u0431\u0443\u0434\u0443\u0442 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u044c\u0441\u044f \u0432 \u043e\u0434\u043d\u043e\u043c \u0448\u0438\u0440\u043e\u043a\u043e\u0432\u0435\u0449\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u043c \u0434\u043e\u043c\u0435\u043d\u0435 \u0438 \u0441\u043c\u043e\u0433\u0443\u0442 \u043e\u0431\u0449\u0430\u0442\u044c\u0441\u044f \u0434\u0440\u0443\u0433 \u0441 \u0434\u0440\u0443\u0433\u043e\u043c \u0438 \u0431\u0435\u0437 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0440\u0443\u0442\u0435\u0440\u0430. <\/p>\n<p>  <\/p>\n<p>ns30 \u0431\u0443\u0434\u0435\u0442 \u0432 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e\u043c \u0448\u0438\u0440\u043e\u043a\u043e\u0432\u0435\u0449\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u043c \u0434\u043e\u043c\u0435\u043d\u0435 \u0438 \u0441\u043c\u043e\u0436\u0435\u0442 \u043e\u0431\u0449\u0430\u0442\u044c\u0441\u044f \u0441 ns10 \u0438 ns20 \u0442\u043e\u043b\u044c\u043a\u043e \u0447\u0435\u0440\u0435\u0437 \u043c\u0430\u0440\u0448\u0440\u0443\u0442\u0438\u0437\u0430\u0446\u0438\u044e \u043d\u0430 \u0440\u0443\u0442\u0435\u0440\u0435 (\u0433\u0434\u0435 \u043c\u043e\u0436\u043d\u043e \u0432\u0432\u043e\u0434\u0438\u0442\u044c \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u0435 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f \u043d\u0430 \u0442\u0430\u043a\u043e\u0439 \u0434\u043e\u0441\u0442\u0443\u043f). <\/p>\n<p>  <\/p>\n<p>\u0417\u0430\u0432\u0435\u0434\u0435\u043d\u043d\u044b\u0435 \u0432 nsbridge \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u044b veth11, veth21, veth31 \u043f\u043e\u0434\u0441\u043e\u0435\u0434\u0438\u043d\u0438\u043c \u043a \u0431\u0440\u0438\u0434\u0436\u0443 \u0432 \u0440\u0435\u0436\u0438\u043c\u0435 access. \u041d\u0430\u0437\u043d\u0430\u0447\u0438\u043c vlan id 10 \u043d\u0430 veth11 \u0438 veth21, \u0438 vlan id 30 \u043d\u0430 veth31. \u0418\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 vethbr \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0438\u043c \u0432 \u0440\u0435\u0436\u0438\u043c\u0435 trunk \u0438 \u0440\u0430\u0437\u0440\u0435\u0448\u0438\u043c \u0435\u043c\u0443 \u043f\u0440\u043e\u043f\u0443\u0441\u043a\u0430\u0442\u044c vlan 10 \u0438 30.<\/p>\n<p>  <\/p>\n<pre><code class=\"plaintext\"># \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u0431\u0440\u0438\u0434\u0436 \u0438 \u0432\u043a\u043b\u044e\u0447\u0430\u0435\u043c \u0435\u0433\u043e ip netns exec nsbridge ip link add name br type bridge ip netns exec nsbridge ip link set dev br up  # \u043f\u0435\u0440\u0435\u0432\u043e\u0434\u0438\u043c \u0431\u0440\u0438\u0434\u0436 \u0432 \u0440\u0435\u0436\u0438\u043c VLAN-aware ip netns exec nsbridge ip link set dev br type bridge vlan_filtering 1  # \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0430\u0435\u043c \u043a \u043d\u0435\u043c\u0443 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u044b ip netns exec nsbridge ip link set dev veth11 master br ip netns exec nsbridge ip link set dev veth21 master br ip netns exec nsbridge ip link set dev veth31 master br ip netns exec nsbridge ip link set dev vethbr master br  # \u0443\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c \u0440\u0435\u0436\u0438\u043c access \u043d\u0430 veth11, veth21, veth31 ip netns exec nsbridge bridge vlan add vid 10 dev veth11 pvid untagged ip netns exec nsbridge bridge vlan add vid 10 dev veth21 pvid untagged ip netns exec nsbridge bridge vlan add vid 30 dev veth31 pvid untagged  # \u0443\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c \u0440\u0435\u0436\u0438\u043c trunk \u043d\u0430 vethbr ip netns exec nsbridge bridge vlan add dev vethbr vid 10 ip netns exec nsbridge bridge vlan add dev vethbr vid 30  # \u043f\u0440\u0438\u0441\u0432\u0430\u0438\u0432\u0430\u0435\u043c ip \u0430\u0434\u0440\u0435\u0441\u0430 ip netns exec ns10 ip addr add 192.168.10.2\/24 dev veth10 ip netns exec ns20 ip addr add 192.168.10.3\/24 dev veth20 ip netns exec ns30 ip addr add 192.168.30.2\/24 dev veth30  # \u0438 \u0434\u0435\u0444\u043e\u043b\u0442\u043d\u044b\u0435 \u043c\u0430\u0440\u0448\u0440\u0443\u0442\u044b \u0447\u0435\u0440\u0435\u0437 \u043c\u0430\u0440\u0448\u0440\u0443\u0442\u0438\u0437\u0430\u0442\u043e\u0440 ip netns exec ns10 ip r add default via 192.168.10.1 ip netns exec ns20 ip r add default via 192.168.10.1 ip netns exec ns30 ip r add default via 192.168.30.1<\/code><\/pre>\n<p>  <\/p>\n<h2 id=\"ruter\">\u0420\u0443\u0442\u0435\u0440<\/h2>\n<p>  <\/p>\n<p>\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0440\u0443\u0442\u0435\u0440\u0430 \u0437\u0430\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0441\u044f \u0432 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0438 vlan \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u043e\u0432 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430, \u0441\u043e\u0435\u0434\u0438\u043d\u044f\u044e\u0449\u0435\u0433\u043e \u0440\u0443\u0442\u0435\u0440 \u0438 \u0431\u0440\u0438\u0434\u0436, \u043f\u0440\u0438\u0441\u0432\u0430\u0438\u0432\u0430\u043d\u0438\u044f \u0438\u043c ip \u0430\u0434\u0440\u0435\u0441\u043e\u0432 \u0438 \u0432\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f \u0444\u043e\u0440\u0432\u0430\u0440\u0434\u0438\u043d\u0433\u0430 \u043f\u0430\u043a\u0435\u0442\u043e\u0432 \u043c\u0435\u0436\u0434\u0443 vlan \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430\u043c\u0438. <\/p>\n<p>  <\/p>\n<pre><code class=\"plaintext\"># \u0441\u043e\u0437\u0434\u0430\u0435\u043c vlan \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u044b \u0438 \u0432\u043a\u043b\u044e\u0447\u0430\u0435\u043c \u0438\u0445 ip netns exec nsrouter ip link add link vethrou name vethrou.10 type vlan id 10 ip netns exec nsrouter ip link add link vethrou name vethrou.30 type vlan id 30 ip netns exec nsrouter ip link set dev vethrou.10 up ip netns exec nsrouter ip link set dev vethrou.30 up  # \u043f\u0440\u0438\u0441\u0432\u0430\u0438\u0432\u0430\u0435\u043c \u0438\u043c \u0430\u0434\u0440\u0435\u0441\u0430 ip netns exec nsrouter ip addr add 192.168.10.1\/24 dev vethrou.10 ip netns exec nsrouter ip addr add 192.168.30.1\/24 dev vethrou.30  # \u0432\u043a\u043b\u044e\u0447\u0430\u0435\u043c \u0444\u043e\u0440\u0432\u0430\u0440\u0434\u0438\u043d\u0433 \u043f\u0430\u043a\u0435\u0442\u043e\u0432 \u043c\u0435\u0436\u0434\u0443 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430\u043c\u0438 ip netns exec nsrouter sysctl -w net.ipv4.ip_forward=1<\/code><\/pre>\n<p>  <\/p>\n<h2 id=\"proverka\">\u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430<\/h2>\n<p>  <\/p>\n<p>\u041f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c \u0441\u0432\u044f\u0437\u043d\u043e\u0441\u0442\u044c \u043c\u0435\u0436\u0434\u0443 \u043d\u0435\u0439\u043c\u0441\u043f\u0435\u0439\u0441\u0430\u043c\u0438: <\/p>\n<p>  <\/p>\n<p>\u043f\u0438\u043d\u0433 \u043c\u0435\u0436\u0434\u0443 ns10 \u0438 ns20<\/p>\n<p>  <\/p>\n<pre><code class=\"plaintext\">ip netns exec ns10 ping 192.168.10.3 --- PING 192.168.10.3 (192.168.10.3) 56(84) bytes of data. 64 bytes from 192.168.10.3: icmp_seq=1 ttl=64 time=0.123 ms 64 bytes from 192.168.10.3: icmp_seq=2 ttl=64 time=0.110 ms<\/code><\/pre>\n<p>  <\/p>\n<p>\u043f\u0438\u043d\u0433 \u043c\u0435\u0436\u0434\u0443 ns10 \u0438 ns30<\/p>\n<p>  <\/p>\n<pre><code class=\"plaintext\">ip netns exec ns10 ping 192.168.30.2 --- PING 192.168.30.2 (192.168.30.2) 56(84) bytes of data. 64 bytes from 192.168.30.2: icmp_seq=1 ttl=63 time=0.199 ms 64 bytes from 192.168.30.2: icmp_seq=2 ttl=63 time=0.151 ms<\/code><\/pre>\n<p>  <\/p>\n<p>\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0442\u0440\u0430\u0444\u0438\u043a \u043d\u0430 \u043a\u043e\u043c\u043c\u0443\u0442\u0430\u0442\u043e\u0440\u0435<\/p>\n<p>  <\/p>\n<pre><code class=\"plaintext\">ip netns exec nsbridge tcpdump -i vethbr -n -e -v --- # \u043f\u0430\u043a\u0435\u0442 \u0441 ns10 \u043d\u0430 \u0440\u0443\u0442\u0435\u0440. \u0432\u0438\u0434\u0435\u043d vlan10 09:26:42.066162 5e:b0:da:5a:95:76 > 3a:02:15:e3:44:d1, ethertype 802.1Q (0x8100), length 102: vlan 10, p 0, ethertype IPv4, (tos 0x0, ttl 64, id 4537, offset 0, flags [DF], proto ICMP (1), length 84)     192.168.10.2 > 192.168.30.2: ICMP echo request, id 43125, seq 1, length 64 # \u043f\u0430\u043a\u0435\u0442 \u0441 \u0440\u0443\u0442\u0435\u0440\u0430 \u043d\u0430 ns30. \u0432\u0438\u0434\u0435\u043d vlan30 09:26:42.066206 3a:02:15:e3:44:d1 > f6:df:30:1a:96:7e, ethertype 802.1Q (0x8100), length 102: vlan 30, p 0, ethertype IPv4, (tos 0x0, ttl 63, id 4537, offset 0, flags [DF], proto ICMP (1), length 84)     192.168.10.2 > 192.168.30.2: ICMP echo request, id 43125, seq 1, length 64 # \u043f\u0430\u043a\u0435\u0442 \u0441 ns30 \u043d\u0430 \u0440\u0443\u0442\u0435\u0440 (vlan 30) 09:26:42.066233 f6:df:30:1a:96:7e > 3a:02:15:e3:44:d1, ethertype 802.1Q (0x8100), length 102: vlan 30, p 0, ethertype IPv4, (tos 0x0, ttl 64, id 20169, offset 0, flags [none], proto ICMP (1), length 84)     192.168.30.2 > 192.168.10.2: ICMP echo reply, id 43125, seq 1, length 64 # \u043f\u0430\u043a\u0435\u0442 \u0441 \u0440\u0443\u0442\u0435\u0440\u0430 \u043d\u0430 ns10 (vlan 10) 09:26:42.066238 3a:02:15:e3:44:d1 > 5e:b0:da:5a:95:76, ethertype 802.1Q (0x8100), length 102: vlan 10, p 0, ethertype IPv4, (tos 0x0, ttl 63, id 20169, offset 0, flags [none], proto ICMP (1), length 84)     192.168.30.2 > 192.168.10.2: ICMP echo reply, id 43125, seq 1, length 64<\/code><\/pre>\n<p>  <\/p>\n<p>\u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u0432 ns30 \u0432\u0435\u0431-\u0441\u0435\u0440\u0432\u0435\u0440 \u0438 \u043e\u0431\u0440\u0430\u0449\u0430\u0435\u043c\u0441\u044f \u043a \u043d\u0435\u043c\u0443 \u0438\u0437 ns10, \u0432\u0441\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442<\/p>\n<p>  <\/p>\n<pre><code class=\"plaintext\">ip netns exec ns30 python3 -m http.server &amp; ip netns exec ns10 curl -v 192.168.30.2:8000<\/code><\/pre>\n<p>  <\/p>\n<p>\u043e\u0442\u043a\u043b\u044e\u0447\u0430\u0435\u043c net.ipv4.ip_forward \u043d\u0430 \u0440\u0443\u0442\u0435\u0440\u0435<\/p>\n<p>  <\/p>\n<pre><code class=\"plaintext\">ip netns exec nsrouter sysctl -w net.ipv4.ip_forward=0<\/code><\/pre>\n<p>  <\/p>\n<p>\u0442\u043e\u0433\u0434\u0430 \u043f\u0438\u043d\u0433 \u043c\u0435\u0436\u0434\u0443 ns10 \u0438 ns20 \u043f\u0440\u043e\u0445\u043e\u0434\u0438\u0442, \u0442.\u043a. \u043e\u043d\u0438 \u0432 \u043e\u0434\u043d\u043e\u043c vlan (\u043f\u0430\u043a\u0435\u0442\u044b \u0438\u0434\u0443\u0442 \u0447\u0435\u0440\u0435\u0437 \u043a\u043e\u043c\u043c\u0443\u0442\u0430\u0442\u043e\u0440 \u0431\u0435\u0437 \u0443\u0447\u0430\u0441\u0442\u0438\u044f \u0440\u0443\u0442\u0435\u0440\u0430)<\/p>\n<p>  <\/p>\n<pre><code class=\"plaintext\">ip netns exec ns10 ping 192.168.10.3 --- PING 192.168.10.3 (192.168.10.3) 56(84) bytes of data. 64 bytes from 192.168.10.3: icmp_seq=1 ttl=64 time=0.136 ms 64 bytes from 192.168.10.3: icmp_seq=2 ttl=64 time=0.112 ms<\/code><\/pre>\n<p>  <\/p>\n<p>\u0430 \u043f\u0438\u043d\u0433 \u043c\u0435\u0436\u0434\u0443 ns10 \u0438 ns30 \u043d\u0435 \u043f\u0440\u043e\u0445\u043e\u0434\u0438\u0442, \u0442.\u043a. \u0440\u0443\u0442\u0435\u0440 \u0432\u044b\u043a\u043b\u044e\u0447\u0435\u043d<\/p>\n<p>  <\/p>\n<pre><code class=\"plaintext\">ip netns exec ns10 ping 192.168.30.2 -c 2 --- PING 192.168.30.2 (192.168.30.2) 56(84) bytes of data. ^C --- 192.168.30.2 ping statistics --- 2 packets transmitted, 0 received, 100% packet loss, time 1027ms <\/code><\/pre>\n<p>  <\/p>\n<h2 id=\"dobavlyaem-vneshniy-internet\">\u0414\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0432\u043d\u0435\u0448\u043d\u0438\u0439 \u0438\u043d\u0442\u0435\u0440\u043d\u0435\u0442<\/h2>\n<p>  <\/p>\n<p>\u0414\u043e\u0431\u0430\u0432\u0438\u043c \u043f\u0430\u0440\u0443 \u0432\u0438\u0440\u0442\u0443\u0430\u043b\u044c\u043d\u044b\u0445 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u043e\u0432 veth1 \u2014 veth2. veth1 \u043e\u0441\u0442\u0430\u0432\u0438\u043c \u0432 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u043c \u043d\u0435\u0439\u043c\u0441\u043f\u0435\u0439\u0441\u0435, veth2 \u043f\u0435\u0440\u0435\u043d\u0435\u0441\u0435\u043c \u0432 nsrouter <\/p>\n<p>  <\/p>\n<pre><code class=\"plaintext\">ip link add name veth1 type veth peer name veth2 ip link set dev veth2 netns nsrouter ip link set dev veth1 up ip addr add 10.10.10.1\/24 dev veth1 ip netns exec nsrouter ip link set dev veth2 up ip netns exec nsrouter ip addr add 10.10.10.2\/24 dev veth2<\/code><\/pre>\n<p>  <\/p>\n<p>\u0412 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u043c \u043d\u0435\u0439\u043c\u0441\u043f\u0435\u0439\u0441\u0435 \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u0442\u0440\u0430\u043d\u0441\u043b\u044f\u0446\u0438\u044e ip \u0430\u0434\u0440\u0435\u0441\u0430 \u0438\u0441\u0442\u043e\u0447\u043d\u0438\u043a\u0430 \u043d\u0430 \u0430\u0434\u0440\u0435\u0441 \u0432\u044b\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430 enp1s0 \u0434\u043b\u044f \u043f\u0430\u043a\u0435\u0442\u043e\u0432 \u0438\u0437 veth1 \u0438 \u0432\u043a\u043b\u044e\u0447\u0438\u043c \u0444\u043e\u0440\u0432\u0430\u0440\u0434\u0438\u043d\u0433 \u043f\u0430\u043a\u0435\u0442\u043e\u0432 \u043c\u0435\u0436\u0434\u0443 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430\u043c\u0438<\/p>\n<p>  <\/p>\n<pre><code class=\"plaintext\">iptables -t nat -A POSTROUTING -s 10.10.10.0\/24 -o enp1s0 -j MASQUERADE sysctl -w net.ipv4.ip_forward=1<\/code><\/pre>\n<p>  <\/p>\n<p>\u0412 \u043d\u0435\u0439\u043c\u0441\u043f\u0435\u0439\u0441\u0435 \u0440\u0443\u0442\u0435\u0440\u0430 \u0442\u0430\u043a\u0436\u0435 \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u043c\u0430\u0440\u0448\u0440\u0443\u0442 \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e \u0438 \u0442\u0440\u0430\u043d\u0441\u043b\u044f\u0446\u0438\u044e ip \u0430\u0434\u0440\u0435\u0441\u043e\u0432<\/p>\n<p>  <\/p>\n<pre><code class=\"plaintext\">ip netns exec nsrouter ip r add default via 10.10.10.1 ip netns exec nsrouter iptables -t nat -A POSTROUTING  -s 192.168.0.0\/16 -o veth2 -j MASQUERADE<\/code><\/pre>\n<p>  <\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0432\u043d\u0435\u0448\u043d\u0438\u0439 \u0438\u043d\u0442\u0435\u0440\u043d\u0435\u0442 \u0434\u043e\u0441\u0442\u0443\u043f\u0435\u043d \u0432 ns10 \u2014 ns30<\/p>\n<p>  <\/p>\n<pre><code class=\"plaintext\">ip netns exec ns30 ping 8.8.8.8 -c 2 --- PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data. 64 bytes from 8.8.8.8: icmp_seq=1 ttl=102 time=138 ms 64 bytes from 8.8.8.8: icmp_seq=2 ttl=102 time=160 ms <\/code><\/pre>\n<p>  <\/p>\n<p>\u0421\u043f\u0430\u0441\u0438\u0431\u043e \u0437\u0430 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/765748\/\"> https:\/\/habr.com\/ru\/articles\/765748\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-1\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/webt\/xr\/ao\/an\/xraoancc2g0d6hn46ks88r7vu_c.png\" data-src=\"https:\/\/habrastorage.org\/webt\/xr\/ao\/an\/xraoancc2g0d6hn46ks88r7vu_c.png\"\/><br \/>  \u041d\u0435 \u0431\u0443\u0434\u0443 \u0442\u043e\u043c\u0438\u0442\u044c \u0432\u0430\u0441 \u0434\u043e\u043b\u0433\u0438\u043c \u0432\u0441\u0442\u0443\u043f\u043b\u0435\u043d\u0438\u0435\u043c. \u041d\u0438\u0436\u0435 \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u043e\u0435 \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u044d\u0442\u043e\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438. <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-356806","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/356806","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=356806"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/356806\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=356806"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=356806"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=356806"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}