Linux中网络命名空间实战各种坑

简介: 实战笔记

network namespace

我有一个程序,监听某个端口,在一台Linux机器上,运行多个这样的程序,端口监听一样,冲突怎么办?

可以更改监听端口,这样即使端口不一样,但是程序还是一样的。

如果你硬要监听一样的端口,那么端口肯定是冲突的,除非你另外创建一个网络命名空间,把程序运行在这个命名空间中

可以实战操作一把

root@ubuntu:/home/ubuntu# ip netns --helpCommand "--help" is unknown, try "ip netns help".
root@ubuntu:/home/ubuntu# ip netns helpUsage: 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


创建网络命名空间 demo

root@ubuntu:/home/ubuntu# ip netns add demoroot@ubuntu:/home/ubuntu# ip netns lsdemo

添加虚拟网络设备

下面命令添加了两个端对端的网络veth1veth2

ip link add veth1 type veth peer name veth2

将虚拟网络一端添加到命名空间中, 这里把veth1添加到网络命名空间

ip link set veth1 netns demo

配置网络设备,这里把veth2留在本机并配置IP

ip addr add 172.17.0.1/24 dev veth2

启动设备

ip link set dev veth2 up

查看网络

root@ubuntu:/home/ubuntu# ifconfig -v veth2veth2: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500        inet 172.17.0.1  netmask 255.255.255.0  broadcast 0.0.0.0
        ether 8e:7e:64:ff:30:8a  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

给网络命名空间虚拟设备配置IP,并启用网络设备

root@ubuntu:/home/ubuntu# ip netns exec demo ip addr add 172.17.0.2/24 dev veth1root@ubuntu:/home/ubuntu# ip netns exec demo ip link set dev veth1 uproot@ubuntu:/home/ubuntu# ip netns exec demo ifconfigveth1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500        inet 172.17.0.2  netmask 255.255.255.0  broadcast 0.0.0.0
        inet6 fe80::38d1:35ff:fe2a:757b  prefixlen 64  scopeid 0x20<link>
        ether 3a:d1:35:2a:75:7b  txqueuelen 1000  (Ethernet)
        RX packets 8  bytes 656 (656.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0        TX packets 8  bytes 656 (656.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

在默认网络与demo命名空间网络,互相ping,都是可以ping通的

但是,最后在demo命名空间中,ping 172.17.0.2 却是没有响应

root@ubuntu:/home/ubuntu# ping 172.17.0.1PING 172.17.0.1 (172.17.0.1) 56(84) bytes of data.
64 bytes from 172.17.0.1: icmp_seq=1ttl=64time=0.107 ms
64 bytes from 172.17.0.1: icmp_seq=2ttl=64time=0.159 ms
64 bytes from 172.17.0.1: icmp_seq=3ttl=64time=0.158 ms
^C
---172.17.0.1 ping statistics ---3 packets transmitted, 3 received, 0% packet loss, time 2025ms
rtt min/avg/max/mdev =0.107/0.141/0.159/0.026 ms
root@ubuntu:/home/ubuntu# ping 172.17.0.2PING 172.17.0.2 (172.17.0.2) 56(84) bytes of data.
64 bytes from 172.17.0.2: icmp_seq=1ttl=64time=0.162 ms
64 bytes from 172.17.0.2: icmp_seq=2ttl=64time=0.096 ms
64 bytes from 172.17.0.2: icmp_seq=3ttl=64time=0.092 ms
^C
---172.17.0.2 ping statistics ---3 packets transmitted, 3 received, 0% packet loss, time 2148ms
rtt min/avg/max/mdev =0.092/0.116/0.162/0.034 ms
root@ubuntu:/home/ubuntu# ip netns exec demo ping 172.17.0.1PING 172.17.0.1 (172.17.0.1) 56(84) bytes of data.
64 bytes from 172.17.0.1: icmp_seq=1ttl=64time=0.217 ms
64 bytes from 172.17.0.1: icmp_seq=2ttl=64time=0.090 ms
64 bytes from 172.17.0.1: icmp_seq=3ttl=64time=0.096 ms
^C
---172.17.0.1 ping statistics ---3 packets transmitted, 3 received, 0% packet loss, time 2115ms
rtt min/avg/max/mdev =0.090/0.134/0.217/0.059 ms
root@ubuntu:/home/ubuntu# ip netns exec demo ping 172.17.0.2PING 172.17.0.2 (172.17.0.2) 56(84) bytes of data.


进入demo命名空间排查,发现回环设备也无法ping通,通过启用lo设备,问题就解决了

root@ubuntu:/home/ubuntu# ping 127.0.0.1connect: Network is unreachable
root@ubuntu:/home/ubuntu# ping 0.0.0.0connect: Network is unreachable
root@ubuntu:/home/ubuntu# ip link set dev lo uproot@ubuntu:/home/ubuntu# ping 0.0.0.0PING 0.0.0.0 (127.0.0.1) 56(84) bytes of data.
64 bytes from 127.0.0.1: icmp_seq=1ttl=64time=0.070 ms
64 bytes from 127.0.0.1: icmp_seq=2ttl=64time=0.086 ms
^C
---0.0.0.0 ping statistics ---2 packets transmitted, 2 received, 0% packet loss, time 1013ms
rtt min/avg/max/mdev =0.070/0.078/0.086/0.008 ms
root@ubuntu:/home/ubuntu# ping 172.17.0.2PING 172.17.0.2 (172.17.0.2) 56(84) bytes of data.
64 bytes from 172.17.0.2: icmp_seq=1ttl=64time=0.077 ms
64 bytes from 172.17.0.2: icmp_seq=2ttl=64time=0.087 ms
^C
---172.17.0.2 ping statistics ---2 packets transmitted, 2 received, 0% packet loss, time 1022ms
rtt min/avg/max/mdev =0.077/0.082/0.087/0.005 ms

发现demo网络命名空间,无法ping通外网

root@ubuntu:/home/ubuntu# ip netns exec demo ping 114.114.114.114connect: Network is unreachable

查看路由,发现并没有匹配的路由

root@ubuntu:/home/ubuntu# ip netns exec demo routeKernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
172.17.0.0      0.0.0.0         255.255.255.0   U     000 veth1

添加一条路由试试,enp0s2为默认网络空间的网络设备名

ip netns exec demo route add default gw 172.17.0.1
echo1 > /proc/sys/net/ipv4/ip_forward
iptables -t nat -A POSTROUTING -s172.17.0.0/24 -o enp0s2 -j MASQUERADE



验证

root@ubuntu:/home/ubuntu# ip netns exec demo ping 114.114.114.114PING 114.114.114.114 (114.114.114.114) 56(84) bytes of data.
64 bytes from 114.114.114.114: icmp_seq=1ttl=58time=13.4 ms
64 bytes from 114.114.114.114: icmp_seq=2ttl=58time=7.34 ms
64 bytes from 114.114.114.114: icmp_seq=3ttl=58time=10.7 ms

发现demo网络命名空间无法ping通百度

root@ubuntu:/home/ubuntu# ip netns exec demo ping www.baidu.comping: www.baidu.com: Temporary failure in name resolution

进入命名空间创建dns解析

root@ubuntu:/home/ubuntu# ip netns exec demo bashroot@ubuntu:/home/ubuntu# mkdir -p /etc/netns/demoroot@ubuntu:/home/ubuntu# vim /etc/netns/demo/resolv.confroot@ubuntu:/home/ubuntu# cat /etc/netns/demo/resolv.confnameserver 114.114.114.114

在默认命名空间,执行如下命令

root@ubuntu:/home/ubuntu# strace  -f ip netns exec demo sleep 1 2>&1|egrep '/etc/|clone|mount|unshare'|egrep -vw '/etc/ld.so|access'unshare(CLONE_NEWNS)                    =0mount("", "/", 0x55ebf359d725, MS_REC|MS_SLAVE, NULL) =0umount2("/sys", MNT_DETACH)             =0mount("demo", "/sys", "sysfs", 0, NULL) =0openat(AT_FDCWD, "/etc/netns/demo", O_RDONLY|O_NONBLOCK|O_CLOEXEC|O_DIRECTORY) =5mount("/etc/netns/demo/resolv.conf", "/etc/resolv.conf", 0x55ebf359d725, MS_BIND, NULL) =0

发现已经可以ping通百度了

root@ubuntu:/home/ubuntu# ip netns exec demo bashroot@ubuntu:/home/ubuntu# ping www.baidu.comPING www.a.shifen.com (36.152.44.95) 56(84) bytes of data.
64 bytes from 36.152.44.95 (36.152.44.95): icmp_seq=1ttl=49time=12.8 ms
64 bytes from 36.152.44.95 (36.152.44.95): icmp_seq=2ttl=49time=11.0 ms
64 bytes from 36.152.44.95 (36.152.44.95): icmp_seq=3ttl=49time=13.2 ms



目录
相关文章
|
2天前
|
消息中间件 Java Linux
2024年最全BATJ真题突击:Java基础+JVM+分布式高并发+网络编程+Linux(1),2024年最新意外的惊喜
2024年最全BATJ真题突击:Java基础+JVM+分布式高并发+网络编程+Linux(1),2024年最新意外的惊喜
|
6天前
|
运维 Oracle 容灾
Oracle dataguard 容灾技术实战(笔记),教你一种更清晰的Linux运维架构
Oracle dataguard 容灾技术实战(笔记),教你一种更清晰的Linux运维架构
|
21小时前
|
JSON 安全 网络协议
【Linux 网络】网络基础(二)(应用层协议:HTTP、HTTPS)-- 详解
【Linux 网络】网络基础(二)(应用层协议:HTTP、HTTPS)-- 详解
|
21小时前
|
存储 网络协议 Unix
【Linux 网络】网络编程套接字 -- 详解
【Linux 网络】网络编程套接字 -- 详解
|
21小时前
|
存储 网络协议 Linux
【Linux 网络】网络基础(一)(局域网、广域网、网络协议、TCP/IP结构模型、网络传输、封装和分用)-- 详解(下)
【Linux 网络】网络基础(一)(局域网、广域网、网络协议、TCP/IP结构模型、网络传输、封装和分用)-- 详解(下)
|
21小时前
|
存储 网络协议 安全
【Linux 网络】网络基础(一)(局域网、广域网、网络协议、TCP/IP结构模型、网络传输、封装和分用)-- 详解(上)
【Linux 网络】网络基础(一)(局域网、广域网、网络协议、TCP/IP结构模型、网络传输、封装和分用)-- 详解(上)
|
1天前
|
网络协议 Linux Shell
linux实战
linux实战
27 7
linux实战
|
4天前
|
网络协议 Python
Python 网络编程实战:构建高效的网络应用
【5月更文挑战第18天】Python在数字化时代成为构建网络应用的热门语言,因其简洁的语法和强大功能。本文介绍了网络编程基础知识,包括TCP和UDP套接字,强调异步编程、数据压缩和连接池的关键作用。提供了一个简单的TCP服务器和客户端代码示例,并提及优化与改进方向,鼓励读者通过实践提升网络应用性能。
23 6
|
5天前
|
存储 安全 算法
网络安全与信息安全:防护之道与实战策略
【5月更文挑战第17天】 在数字化时代,网络安全和信息安全已成为维护社会稳定、保障个人隐私和企业资产的重要屏障。本文深入探讨了网络安全漏洞的成因、加密技术的最新进展以及提升安全意识的有效方法。通过对网络攻防技术的剖析,揭示了防御策略的重要性,并提供了实用的防护措施和应对方案,旨在为读者打造一道坚固的信息安全防线。