大网数据广告活动
服务器运维问答

Can't ping internal network namespace

· 52CE 我爱测

I have created two network namespaces, i.e., red and blue on centos machine as follows: [root@ip-xxx-xxx-xxx-xxx ~]# ip netns add red [root@ip-xxx-xxx-xxx-xxx ~]# ip netns add blue [root@ip-xxx-xxx-xxx-xxx ~]# ip netns blue red I…

问题原文

原帖链接 · Gompu · CC BY-SA 4.0

I have created two network namespaces, i.e., red and blue on centos machine as follows:

[root@ip-xxx-xxx-xxx-xxx ~]# ip netns add red
[root@ip-xxx-xxx-xxx-xxx ~]# ip netns add blue

[root@ip-xxx-xxx-xxx-xxx ~]# ip netns
blue
red

I have created the virtual cable 'veth-red' and 'veth-blue' and then connected them using the following command:

[root@ip-xxx-xxx-xxx-xxx ~]# ip link add veth-red type veth peer name veth-blue

Then I attached the appropriate interface to each namespace as follows:

[root@ip-xxx-xxx-xxx-xxx ~]# ip link set veth-red netns red
[root@ip-xxx-xxx-xxx-xxx ~]# ip link set veth-blue netns blue

I then assigned IP addresses to each of these namespaces as follows:

[root@ip-xxx-xxx-xxx-xxx ~]# ip -n red addr add 192.168.15.1 dev veth-red

[root@ip-xxx-xxx-xxx-xxx ~]# ip -n blue addr add 192.168.15.2 dev veth-blue

I then bring up the interface using the IP link set up command for each device within the respective namespaces.

[root@ip-xxx-xxx-xxx-xxx ~]# ip -n red link set veth-red up
[root@ip-xxx-xxx-xxx-xxx ~]# ip -n blue link set veth-blue up

Then, when I check the interfaces inside each namespace, I get the 'veth-red' in 'red' namespace and 'veth-blue' in blue namespace as follows:

[root@ip-xxx-xxx-xxx-xxx ~]# ip -n red link
1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN mode DEFAULT group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
17: veth-red@if16: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP mode DEFAULT group default qlen 1000
    link/ether 7e:9d:42:79:2d:2f brd ff:ff:ff:ff:ff:ff link-netnsid 1
[root@ip-xxx-xxx-xxx-xxx ~]# ip -n blue link
1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN mode DEFAULT group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
16: veth-blue@if17: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP mode DEFAULT group default qlen 1000
    link/ether 62:aa:79:55:46:56 brd ff:ff:ff:ff:ff:ff link-netnsid 0

My doubt arises when I try to send a ping from red to blue (IP address: 192.168.15.2) as follows:

[root@ip-xxx-xxx-xxx-xxx ~]# ip netns exec red ping 192.168.15.2
connect: Network is unreachable

Can someone let me know why am I getting 'Network is unreachable' when I did everything by the book? Please help

已采纳解答

原帖链接 · chutz · CC BY-SA 4.0

You can troubleshoot with the route command:

# ip -n red route get 192.168.15.2
RTNETLINK answers: Network is unreachable

That's a good hint. You don't have a proper routing in the namespaces. In this case, it is because you missed the netmask when adding the IP address:

# ip -o -n red a
7: veth-red    inet 192.168.15.1/32 scope global veth-red\       valid_lft forever preferred_lft forever 

Try adding the IP address with a /24 so the routing table knows that it can reach other IPs in the network through this interface.

  1. clear all IP addresses first
# ip -n red  addr flush dev veth-red
# ip -n blue addr flush dev veth-blue

  1. Add the correct IP with netmask (using /24 as least confusing, but even /30 will work for your example).
# ip -n red  addr add 192.168.15.1/24 dev veth-red
# ip -n blue addr add 192.168.15.2/24 dev veth-blue

And you're done:

# ip netns exec red ping -c2 192.168.15.2
PING 192.168.15.2 (192.168.15.2) 56(84) bytes of data.
64 bytes from 192.168.15.2: icmp_seq=1 ttl=64 time=0.034 ms
64 bytes from 192.168.15.2: icmp_seq=2 ttl=64 time=0.022 ms

--- 192.168.15.2 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1035ms
rtt min/avg/max/mdev = 0.022/0.028/0.034/0.006 ms```

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2021-08-03)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。