ip命令

linux 的强大的网络配置命令‘ip’。

netns可以让一台机器上模拟多个网络设备,是网络虚拟化的重要组成,将不同类型的网络应用隔离。

一个net namespace有自己独立的路由表,iptables策略,设备管理。说来说去,它就是用来隔离的。比如将eth0加入了netns 1,那么netns 2中的应用程序就找不到eth0了。netns 1中的iptables策略,不会去影响netns 2中的iptables策略。

netns的用法

[html]  view plain copy
  1. [root@monitor ~]# ip netns help list
  2. Usage: ip netns list
  3. ip netns add NAME
  4. ip netns set NAME NETNSID
  5. ip [-all] netns delete [NAME]
  6. ip netns identify [PID]
  7. ip netns pids NAME
  8. ip [-all] netns exec [NAME] cmd ...
  9. ip netns monitor
  10. ip netns list-id

先打开内核的网络转发功能。

[html]  view plain copy
  1. [root@localhost ~]# vim /etc/sysctl.conf
  2. [root@localhost ~]# sysctl -p
  3. net.ipv4.ip_forward = 1

添加两个namespace

[html]  view plain copy
  1. [root@monitor ~]# ip netns add r1
  2. [root@monitor ~]# ip netns add r2
  3. [root@monitor ~]# ip netns list
  4. r2
  5. r1

查看r1的网络。

[html]  view plain copy
  1. [root@monitor ~]# ip netns exec r1 ifconfig -a
  2. lo: flags=8<LOOPBACK>  mtu 65536
  3. loop  txqueuelen 0  (Local Loopback)
  4. RX packets 0  bytes 0 (0.0 B)
  5. RX errors 0  dropped 0  overruns 0  frame 0
  6. TX packets 0  bytes 0 (0.0 B)
  7. TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

为r1的回环接口添加一个ip地址。

[html]  view plain copy
  1. [root@monitor ~]# ip netns exec r1 ifconfig lo 127.0.0.1 up
  2. [root@monitor ~]# ip netns exec r1 ifconfig -a
  3. lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
  4. inet 127.0.0.1  netmask 255.0.0.0
  5. inet6 ::1  prefixlen 128  scopeid 0x10<host>
  6. loop  txqueuelen 0  (Local Loopback)
  7. RX packets 0  bytes 0 (0.0 B)
  8. RX errors 0  dropped 0  overruns 0  frame 0
  9. TX packets 0  bytes 0 (0.0 B)
  10. TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

此时的r2并没有地址,因为他们是被隔离的

在网络名称空间上添加一对网卡,一个在r1,一个在r2.

[html]  view plain copy
  1. [root@localhost ~]# ip link add veth1.1 type veth peer name veth1.2
  2. [root@localhost ~]# ip link show
  3. 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT
  4. link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
  5. 2: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master br-ex state UP mode DEFAULT qlen 1000
  6. link/ether 00:0c:29:4b:bb:d0 brd ff:ff:ff:ff:ff:ff
  7. 3: br-ex: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP mode DEFAULT
  8. link/ether 00:0c:29:4b:bb:d0 brd ff:ff:ff:ff:ff:ff
  9. 4: br-in: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN mode DEFAULT
  10. link/ether 56:8d:9f:d2:96:21 brd ff:ff:ff:ff:ff:ff
  11. 5: veth1.2@veth1.1: <BROADCAST,MULTICAST,M-DOWN> mtu 1500 qdisc noop state DOWN mode DEFAULT qlen 1000
  12. link/ether 7e:ea:fe:98:30:cd brd ff:ff:ff:ff:ff:ff
  13. 6: veth1.1@veth1.2: <BROADCAST,MULTICAST,M-DOWN> mtu 1500 qdisc noop state DOWN mode DEFAULT qlen 1000
  14. link/ether a2:48:54:92:c2:ed brd ff:ff:ff:ff:ff:ff
  15. [root@localhost ~]#

将一对网卡分别添加给2个名称空间。

[html]  view plain copy
  1. [root@localhost ~]# ip link set veth1.1 netns r1
  2. [root@localhost ~]# ip link set veth1.2 netns r2

查看r1的网络信息

[html]  view plain copy
  1. [root@localhost ~]# ip netns exec r1 ifconfig -a
  2. lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
  3. inet 127.0.0.1  netmask 255.0.0.0
  4. inet6 ::1  prefixlen 128  scopeid 0x10<host>
  5. loop  txqueuelen 0  (Local Loopback)
  6. RX packets 0  bytes 0 (0.0 B)
  7. RX errors 0  dropped 0  overruns 0  frame 0
  8. TX packets 0  bytes 0 (0.0 B)
  9. TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
  10. veth1.1: flags=4098<BROADCAST,MULTICAST>  mtu 1500
  11. ether a2:48:54:92:c2:ed  txqueuelen 1000  (Ethernet)
  12. RX packets 0  bytes 0 (0.0 B)
  13. RX errors 0  dropped 0  overruns 0  frame 0
  14. TX packets 0  bytes 0 (0.0 B)
  15. TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

给r1的veth1.1改个名字,为eth0

[html]  view plain copy
  1. [root@localhost ~]# ip netns exec r1 ip link set veth1.1 name eth0

为两个网卡添加ip地址。

[html]  view plain copy
  1. [root@localhost ~]# ip netns exec r1 ifconfig eth0 10.0.1.1/24 up
  2. [root@localhost ~]# ip netns exec r2 ifconfig eth0 10.0.1.2/24 up

ping功能

[html]  view plain copy
  1. [root@localhost ~]# ip netns exec r1 ping 10.0.1.2
  2. PING 10.0.1.2 (10.0.1.2) 56(84) bytes of data.
  3. 64 bytes from 10.0.1.2: icmp_seq=1 ttl=64 time=0.042 ms
  4. 64 bytes from 10.0.1.2: icmp_seq=2 ttl=64 time=0.036 ms
  5. 64 bytes from 10.0.1.2: icmp_seq=3 ttl=64 time=0.043 ms
  6. ^C
  7. --- 10.0.1.2 ping statistics ---
  8. 3 packets transmitted, 3 received, 0% packet loss, time 1999ms
  9. rtt min/avg/max/mdev = 0.036/0.040/0.043/0.006 ms

到目前为止,看吧,此时就好像创建了两个虚拟机一样。两个网络是互相独立的。但是在一个网段内的时候,又可以互相联通。

现在利用netns来创建1个虚拟网络空间。大致内容如下图。

创建桥接

[html]  view plain copy
  1. [root@localhost ~]# brctl addbr br-ex
  2. [root@localhost ~]# ip link set br-ex up
  3. [root@localhost ~]# ifconfig
  4. br-ex: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
  5. ether 1e:d6:fd:9b:2a:fc  txqueuelen 0  (Ethernet)
  6. RX packets 0  bytes 0 (0.0 B)
  7. RX errors 0  dropped 0  overruns 0  frame 0
  8. TX packets 0  bytes 0 (0.0 B)
  9. TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

给桥设备添加IP地址。

[html]  view plain copy
  1. # ip addr del 192.168.217.71/24 dev eno16777736
  2. # ip addr add 192.168.217.71/24 dev br-ex
  3. # brctl addif br-ex eno16777736

再添加一个桥

[html]  view plain copy
  1. [root@localhost ~]# brctl addbr br-in
  2. [root@localhost ~]# ip link set br-in up
  3. [root@localhost ~]#

测试两个虚拟机之间的网络互通性。

首先先写一个脚本,自动桥接的。(/etc/qemu-ifup)

[html]  view plain copy
  1. #!/bin/bash
  2. bridge=br-in
  3. if [ -n "$1" ];then
  4. ip link set $1 up
  5. brctl addif $bridge $1
  6. [ $? -eq 0 ] && exit 0 || exit 1
  7. else
  8. echo 'Error: no interface specified'
  9. exit 1
  10. fi

启动一个虚拟机实例(cirros)

[html]  view plain copy
  1. [root@localhost ~]# qemu-kvm -m 256 -smp 1 -name vm2 \
  2. > -drive file=/images/cirros/test2.qcow2,if=virtio,media=disk \
  3. > -net nic,macaddr=52:54:00:aa:bb:dd \
  4. > -net tap,ifname=vif2.0,script=/etc/qemu-ifup \
  5. > --nographic

再启动一个 vm1.

在真机上查看桥接状态。

[html]  view plain copy
  1. [root@localhost ~]# brctl show
  2. bridge name bridge id       STP enabled interfaces
  3. br-ex       8000.000c294bbbd0   no      eno16777736
  4. br-in       8000.0e1d0f339fc2   no      vif1.0
  5. vif2.0

vif1.0 和vif2.0都是桥接在br-in上了。

好了,现在的情况相当于是vm1,vm2在一个交换机上。这个交换机就是br-in。为了这两个vm虚拟机可以和外界通信,必须要再创建一个虚拟的路由器。删去刚才的r1,r2。

添加路由器R1.

[html]  view plain copy
  1. [root@localhost ~]# ip netns add r1

为路由器R1添加一对网卡并且启动。

[html]  view plain copy
  1. [root@localhost ~]# ip link add rinr type veth peer name rins
  2. [root@localhost ~]# ip link set rinr up
  3. [root@localhost ~]# ip link set rins up

将网卡添加到桥上去。

[html]  view plain copy
  1. [root@localhost ~]# brctl addif br-in rins
  2. [root@localhost ~]# brctl show
  3. bridge name bridge id       STP enabled interfaces
  4. br-ex       8000.000c294bbbd0   no      eno16777736
  5. br-in       8000.0e1d0f339fc2   no      rins
  6. vif1.0
  7. vif2.0

给rinr改个名字,并且启动

[html]  view plain copy
  1. [root@localhost ~]# ip link set rinr netns r1 #将网卡rinr添加至r1
  2. [root@localhost ~]# ip netns exec r1 ip link set rinr name eth0
  3. [root@localhost ~]# ip netns exec r1 ip link set eth0 up

添加一个IP,作为网关。

[html]  view plain copy
  1. [root@localhost ~]# ip netns exec r1 ifconfig eth0 10.0.1.254/24 up
  2. [root@localhost ~]# ip netns exec r1 ifconfig
  3. eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
  4. inet 10.0.1.254  netmask 255.255.255.0  broadcast 10.0.1.255
  5. inet6 fe80::f8b4:bff:fee4:b12d  prefixlen 64  scopeid 0x20<link>
  6. ether fa:b4:0b:e4:b1:2d  txqueuelen 1000  (Ethernet)
  7. RX packets 8  bytes 648 (648.0 B)
  8. RX errors 0  dropped 0  overruns 0  frame 0
  9. TX packets 16  bytes 1296 (1.2 KiB)
  10. TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

两个虚拟机的网关都指向10.0.1.254

[html]  view plain copy
  1. $ sudo su -
  2. # ifconfig eth0 10.0.1.2/24 up
  3. # route add default gw 10.0.1.254

目前来说整幅图的左半边完全好了。

开始右半边。

添加一对网卡,再把其中一个桥接

[html]  view plain copy
  1. [root@localhost ~]# ip link add rexr type veth peer name rexs
  2. [root@localhost ~]# brctl addif br-ex rexs
  3. [root@localhost ~]# ip link set rexs up
  4. [root@localhost ~]# brctl show
  5. bridge name bridge id       STP enabled interfaces
  6. br-ex       8000.000c294bbbd0   no      eno16777736
  7. rexs
  8. br-in       8000.0e1d0f339fc2   no      rins
  9. vif1.0
  10. vif2.0

将另一个网卡添加到路由器的另一边,且给个另一个网络的地址

[html]  view plain copy
  1. [root@localhost ~]# ip link set rexr  netns r1
  2. [root@localhost ~]# ip netns exec r1 ip link set rexr name eth1
  3. [root@localhost ~]# ip netns exec r1 ifconfig eth1 192.168.217.77/24 up

利用防火墙的源地址转换,实现将内网中的地址转换。

[html]  view plain copy
  1. [root@localhost ~]# ip netns exec r1 iptables -t nat -A POSTROUTING -s 10.0.1.0/24 ! -d 10.0.1.0/24 -j SNAT --to-source 192.168.217.77
  2. [root@localhost ~]# ip netns exec r1 iptables -t nat -nL
  3. Chain PREROUTING (policy ACCEPT)
  4. target     prot opt source               destination
  5. Chain INPUT (policy ACCEPT)
  6. target     prot opt source               destination
  7. Chain OUTPUT (policy ACCEPT)
  8. target     prot opt source               destination
  9. Chain POSTROUTING (policy ACCEPT)

测试。vm1可以ping同vm2.vm1可以访问真机所在局域网的主机。

[html]  view plain copy
  1. # hostname
  2. cirros
  3. # ping 10.0.1.1
  4. PING 10.0.1.1 (10.0.1.1): 56 data bytes
  5. 64 bytes from 10.0.1.1: seq=0 ttl=64 time=4.612 ms
  6. # ping 192.168.217.2
  7. PING 192.168.217.2 (192.168.217.2): 56 data bytes
  8. 64 bytes from 192.168.217.2: seq=0 ttl=127 time=4.742 ms
  9. target prot opt source destination SNAT all -- 10.0.1.0/24 !10.0.1.0/24 to:192.168.217.77

当然。在左边那个网络中,还可以运行一个dhcp服务器,并且将网关自动指向10.0.1.254。

[html]  view plain copy
  1. [root@localhost ~]# yum -y install dnsmasq

执行命令

[html]  view plain copy
  1. [root@localhost ~]# ip netns exec r1 dnsmasq -F 10.0.1.1,10.0.1.30 --dhcp-option=option:router,10.0.1.254

出处:https://blog.csdn.net/ghost_leader/article/details/71075551

网络名称空间netns的用法相关推荐

  1. 网络名称空间 实例研究 veth处于不同网络的路由问题

    相关命令详细介绍参见 http://www.cnblogs.com/Dream-Chaser/p/7077105.html 0.问题: 两个网络名称空间中的两个接口veth0和veth1,如何配置ne ...

  2. 《2021/07/24》1 -- linux -- 网络名称空间和网桥的基本操作

    网络名称空间和网桥的基本操作 网络命名空间和网桥的基本操作命令 网络名称空间 (NET Namespace) 查看是否有iproute [root@localhost ~]# rpm -q iprou ...

  3. 学习笔记之linux网络属性配置及其命令用法

    Linux网络属性配置 先来了解一点网络的基础知识:(这些只需记住) TCP/IP:协议栈(使用中的模型) ISO,OSI:协议栈(学习中的模型) MAC:Media Access Control(介 ...

  4. Docker的名称空间

    问题:当docker的容器之间需要协作的时候,例如:搭建TESTLINK,需要两个容器进行协作,本身和mariadb,我们都知道,容器之间是隔离的,如果两个容器需要互相通信,网络该怎么做呢? 方法一: ...

  5. 动态参数 名称空间 作用域 作用域链 加载顺序 函数的嵌套 global nonlocal 等的用法总结...

    03,动态参数 *args,**kwargs # 用户传入到函数中的实参数量不定时,或者是为了以后拓展,# 此时要用到动态参数*args,**kwargs(万能参数.)# *args接收的是所有的位置 ...

  6. 网络虚拟化基础一:linux名称空间Namespaces

    一 介绍 如果把linux操作系统比作一个大房子,那命名空间指的就是这个房子中的一个个房间,住在每个房间里的人都自以为独享了整个房子的资源,但其实大家仅仅只是在共享的基础之上互相隔离,共享指的是共享全 ...

  7. Linux内核实现名称空间的创建

    1.Linux内核实现名称空间的创建 ip netns命令 可以借助ip netns命令来完成对 Network Namespace 的各种操作.ip netns命令来自于iproute安装包,一般系 ...

  8. Kubernetes基本入门-名称空间资源(三)

    名称空间级资源 名称空间在kubernetes中主要的作用是做资源隔离,因此名称空间级别的资源只在当前名称空间下有效. 工作负载型资源 工作负载(workload)是在Kubernetes上运行的应用 ...

  9. django ajax 更新表格_Django(反向解析,路由分发、名称空间、视图层、虚拟环境、Django版本、json,CBV)...

    https://www.zhihu.com/video/1249117508688711680 每日测验 """ 今日考题 1.列举你知道的orm数据的增删改查方法 2. ...

最新文章

  1. linux zip 命令详解
  2. 在CentOS 7服务器中使用Jexus发布.net core webapi
  3. vs及番茄助手快捷键使用介绍
  4. 我所知道的Ribbon库
  5. uva 12558 Egyptian Fractions (HARD version)
  6. async/await处理异步
  7. tensorflow sigmoid 如何计算训练数据的正确率_“来自蒙娜丽莎的凝视”— 结合 TensorFlow.js 和深度学习实现...
  8. 渗透测试工程师字典介绍
  9. Google搜索语法(常用篇)
  10. 批量/去掉office 2010 Word中标题前的黑点
  11. 版本控制软件Git的使用(小白版)
  12. 斜挎包长度到哪里合适_斜挎包背带一般多长 斜挎包背带太长怎么办
  13. c++ Beep函数的雪之梦
  14. 阿里云访问控制简要说明
  15. 孙源面试题试解(更新完毕)
  16. php base62,base62编码
  17. 中小型网站如何预防DDOS攻击
  18. OneDrive-5T免费云空间获取方法
  19. PHP:Maze迷宫寻址算法(附完整源码)
  20. 269、超五类线和六类线水晶头制作,打配线架方法

热门文章

  1. 纸鸢|工业路由器的定位功能有什么作用
  2. 文献阅读十——Detect Rumors on Twitter by Promoting Information Campaigns with Generative Adversarial Learn
  3. 死神(日期暴力破解问题)
  4. html5audio兼容斗战神,CSGO语音包工具SLAMv1.5.4
  5. 神经网络结构图绘图软件,绘制神经网络结构图
  6. 微信JS-SDK获取signature签名以及config配置(微信转发分享页面需要)
  7. 双系统时间错乱解决办法
  8. Linux 虚拟机和物理机实现文本复制粘贴
  9. 【浅墨Unity3D Shader编程】之七 静谧之秋篇: 表面着色器的写法(二)—— 自定义光照模式
  10. 鸿蒙碎片八零,天道天骄_第2688章 一方鸿蒙的碎片!上(1/2)_邂逅小说网