理解linux network namespace

network namespace 是linux内核提供的用于实现网络虚拟化的重要功能,它能创建多个隔离的网络空间,一个独立的网络空间内的防火墙、网卡、路由表、邻居表、协议栈都是独立的。不管是虚拟机还是容器,当运行在独立的命名空间时,就像是一台单独的主机一样。

下面会通过一些例子来说明网络命名空间,以加深理解,会用到iproute2工具包的ip命令,请各位先自行安装,并且使用root权限操作

在centos下执行如下命令:

yum install iproute2

验证安装完成:

[root@worker3 ~]# ip help
Usage: ip [ OPTIONS ] OBJECT { COMMAND | help }
       ip [ -force ] -batch filename
where  OBJECT := { link | address | addrlabel | route | rule | neigh | ntable |
                   tunnel | tuntap | maddress | mroute | mrule | monitor | xfrm |
                   netns | l2tp | fou | macsec | tcp_metrics | token | netconf | ila |
                   vrf }
       OPTIONS := { -V[ersion] | -s[tatistics] | -d[etails] | -r[esolve] |
                    -h[uman-readable] | -iec |
                    -f[amily] { inet | inet6 | ipx | dnet | mpls | bridge | link } |
                    -4 | -6 | -I | -D | -B | -0 |
                    -l[oops] { maximum-addr-flush-attempts } | -br[ief] |
                    -o[neline] | -t[imestamp] | -ts[hort] | -b[atch] [filename] |
                    -rc[vbuf] [size] | -n[etns] name | -a[ll] | -c[olor]}

创建网络命名空间

ip命令中用于操作网络命名空间的命令是ip netns,用help来查看一下子命令有哪些

[root@worker3 ~]# ip netns help
Usage: 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

常用的也就是增删查命令,先来创建一个网络空间空间ns1

ip netns add ns1

查看当前所有的网络命名空间

ip netns list
ns1(id:4)

在这有些人可能会很困惑,我主机上明明运行中好几个docker容器,按理说每个容器都运行在独立的网络命名空间,怎么这里没有列出来?不要着急,下面会提到。

先来感觉一下什么叫独立的网卡,独立的路由表,要查看ns1命名空间的网卡,iproute2工具提供了命令ip netns exec ns1,跟在这个命令后面的命令都会在这个网络命名空间中执行

先查看一下主机的网卡和路由表

[root@worker3 ~]# ip link
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP mode DEFAULT group default qlen 1000
    link/ether 00:50:56:bb:ab:df brd ff:ff:ff:ff:ff:ff

[root@worker3 ~]# ip route
default via 10.57.4.1 dev eth0
10.1.2.0/24 dev br0 proto kernel scope link src 10.1.2.1

再看看ns1中的网卡和路由表

[root@worker3 ~]# ip netns exec ns1 ip 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
    
[root@worker3 ~]# ip netns exec ns3 ip route
[root@worker3 ~]#

这样执行命令有点麻烦,也可以简单一点:

[root@worker3 ~]#ip netns exec ns1 bash
//这个命令后执行的命令就都是在ns1中执行了
[root@worker3 ~]#ip 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
[root@worker3 ~]#ip route
[root@worker3 ~]#

用exit可以回到主机的默认空间

[root@worker3 ~]#exit
[root@worker3 ~]#ip link
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP mode DEFAULT group default qlen 1000
    link/ether 00:50:56:bb:ab:df brd ff:ff:ff:ff:ff:ff

ip netns add的原理

当我们在主机上执行ip netns add ns1后 ,实际是在/var/run/netns下创建了一个ns1的文件

[root@worker3 ~]# ls /var/run/netns
ns1

下面的命令可以模拟ip netns add ns2 && ip netns exec ns2 bash

[root@worker3 ~]# touch /var/run/netns/ns2
[root@worker3 ~]# unshare --net bash
[root@worker3 ~]# mount --bin /proc/self/ns/net /var/run/netns/ns2
//上面的过程实际就是执行了ip netns add ns2 && ip netns exec ns2 bash

[root@worker3 ~]# ip 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

[root@worker3 ~]# exit
//退出,回到主机默认命名空间。用ip netns list查看,已经可以看到ns2
[root@worker3 ~]# ip netns list
ns2
ns1
//如果想再次进入ns2,还有一个方法:
[root@worker3 ~]# nsenter --net=/var/run/netns/ns2

从上面的示例可以看出,创建命名的 network namespace 其实就是创建一个文件,然后通过绑定挂载的方式将新创建的 network namespace 文件和进程的/proc/self/ns/net文件绑定。

查看容器的网络命名空间

接下来该回答上面的遗留问题,为什么当我在主机上ip netns list的时候看不到docker的网络命名空间,因为ip netns list的时候只会显示在/var/run/netns下的文件,而docker的文件默认是创建在/var/run/docker/netns下的,所以我们可以通过ls /var/run/docker/netns来显示当前的所有容器的网络命名空间,并且通过nsenter --net=/var/run/docker/xxx来进入容器的网络命名空间

[root@worker3 ~]# ls /var/run/docker/netns
5bbd5f99d403  a2eabf9acccb  b63ec59b3d9e  d6e4ff961713  default
[root@worker3 ~]# nsenter --net=/var/run/docker/netns/b63ec59b3d9e
[root@worker3 ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
4: eth0@if3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
    link/ether fa:a7:8d:05:03:a6 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 10.244.0.11/32 scope global eth0
       valid_lft forever preferred_lft forever

如果想查看具体某个docker容器对应的文件,可以用:

docker inspect $CONTAINER_ID$|grep SandboxKey

注意如果是k8s拉起来的docker,要拿非hostNetwork=true的pause容器来看,如果hostNetwork=true,那么下面的值为/var/run/docker/netns/default,这是主机的默认网络命名空间,如果不是pause容器,那么下面的值为空,因为只有pause容器会创建一个新的网络命名空间,其它container都只是加入这个网络命名空间。(这个SandboxKey大家先记着,后面写cni组件时还会提到这个值)

[root@worker3 ~]# docker inspect ebd6855901ef|grep SandboxKey
            "SandboxKey": "/var/run/docker/netns/b63ec59b3d9e",

还有另一个办法:

[root@worker3 ~]# docker inspect nginx|grep Pid
            "Pid": 31817,
            "PidMode": "",
            "PidsLimit": null,
[root@worker3 ~]# mkdir -p /var/run/netns/
[root@worker3 ~]# ln -s /proc/31817/ns/net /var/run/netns/ns100
[root@worker3 ~]# ip netns ls
ns100
ns2
ns1
[root@worker3 ~]# ip netns exec ns100 bash
[root@worker3 ~]# //这时候已经在容器网络里了,比nsenter还方便

这个小技巧在我们调试pod的网络时非常有用,大多数时候pod里面自带的工具非常少,没有curl没有telnet,这时候用这个技巧先进入空器的网络空间,再执行命令就行了,因为只是切了网络命名空间,其它还在主机上,所以用的工具也全是主机的工具。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Kali Linux Network Scanning Cookbook - Second Edition by Michael Hixon English | 6 Jun. 2017 | ASIN: B06VW5FB1S | 634 Pages | AZW3 | 36.7 MB Key Features Learn the fundamentals behind commonly used scanning techniques Deploy powerful scanning tools that are integrated into the Kali Linux testing platform The practical recipes will help you automate menial tasks and build your own script library Book Description With the ever-increasing amount of data flowing in today’s world, information security has become vital to any appl ication. This is where Kali Linux comes in. Kali Linux focuses mainly on security auditing and penetration testing. This step-by-step cookbook on network scanning trains you in important scanning concepts based on version 2016.2. It will enable you to conquer any network environment through a range of network scanning techniques and will also equip you to script your very own tools. Starting with the fundamentals of installing and managing Kali Linux, this book will help you map your target with a wide range of network scanning tasks, including discovery, port scanning, fingerprinting, and more. You will learn how to utilize the arsenal of tools available in Kali Linux to conquer any network environment. The book offers expanded coverage of the popular Burp Suite and has new and updated scripts for automating scanning and target exploitation. You will also be shown how to identify remote services, how to assess security risks, and how various attacks are performed. You will cover the latest features of Kali Linux 2016.2, which includes the enhanced Sparta tool and many other exciting updates. This immersive guide will also encourage the creation of personally scripted tools and the skills required to create them. What you will learn Develop a network-testing environment that can be used to test scanning tools and techniques Understand the underlying principles of network scanning technologies by building custom scripts and tools Identify distinc

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值