ip netns
虚拟化网络都是基于netns实现,不管是昨日的openstack,还是今日的docker。
ip netns
ip-netns - process network namespace management
A network namespace is logically another copy of the network stack, with it's own routes, firewall rules, and network devices.
网络名称空间是网络栈的一个逻辑副本,有自己的路由、防火墙规则、和网络设备。
By convention a named network namespace is an object at /var/run/netns/NAME that can be opened. The file descriptor resulting from opening /var/run/netns/NAME refers to the specified network namespace. Holding that file descriptor open keeps the network namespace alive.
通常网络名称空间是一个位于/var/run/netns/NAME的可打开对象,保持这个文件可打开可以保持网络名称空间存活。
The convention for network namespace aware applications is to look for global network configuration files first in /etc/netns/NAME/ then in /etc/.
For example, if you want a different version of /etc/resolv.conf for a network namespace used to isolate your vpn you would name it /etc/netns/myvpn/resolv.conf.
用法
ip [ OPTIONS ] netns  { COMMAND | help }
ip netns list - show all of the named network namespaces 列出所有名称空间
ip netns add NETNSNAME - create a new named network namespace 创建一个新的名称空间
ip netns delete NETNSNAME - delete the name of a network namespace 删除一个名称空间
ip netns exec NETNSNAME cmd ... - Run cmd in the named network namespace 在网络名称空间中执行系统命令
ip [-all] netns exec [ NAME ] cmd ... - Run cmd in the named network namespace.
  If -all option was specified then cmd will be executed synchronously on the each named network namespace.
ip link
用到netns,就不得不说到ip-link
ip-link - network device configuration
用法
ip [ OPTIONS ] link  { COMMAND | help }
显示设备属性
ip link show - display device attributes
  dev NAME:specifies the network device to show. (default) 如果不指定设备,则显示所有设备信息
  up:only display running interfaces. 只显示启动的接口
添加虚拟设备
ip link add - add virtual link
link DEVICE:specifies the physical device to act operate on. 指定在哪个物理设备上操作
NAME:specifies the name of the new virtual device. 指定新虚拟设备的名称
TYPE:specifies the type of the new device. 指定新设备的类型
Link types:
  vlan - 802.1q tagged virtual LAN interface
  veth - Virtual ethernet interface 虚拟网络接口(一对)
  vcan - Virtual Local CAN interface
  dummy - Dummy network interface
  ifb - Intermediate Functional Block device
  macvlan - virtual interface base on link layer address (MAC)
  can - Controller Area Network interface
  bridge - Ethernet Bridge device 网桥设备
删除虚拟设备
ip link delete - delete virtual link
  DEVICE:specifies the virtual  device to act operate on.
  TYPE:specifies the type of the device.
  dev DEVICE:specifies the physical device to act operate on.
设置设备属性
ip link set - change device attributes
  dev DEVICE:specifies network device to operate on.
  up and down:change the state of the device to UP or DOWN. 启动或关闭网卡
  multicast on or multicast off:change the MULTICAST flag on the device. 启用或禁用组播
  name NAME:change the name of the device. 修改网卡名称。需要先donw掉网卡,不然会提示busy。
    This operation is not recommended if the device is running or has some addresses already configured.
  alias NAME:give the device a symbolic name for easy reference. 为网卡设置别名
  mtu NUMBER:change the MTU of the device. 设置MTU大小,默认为1500
  netns NETNSNAME:move the device to the network namespace associated with name NETNSNAME. 将接口移动到指定的网络名称空间
这里单独说一下veth设备
veth设备是成对出现的,一端连接的是内核协议栈,一端彼此相连。一个设备收到协议栈的数据,会将数据发送另一个设备上去。
大概结构如下:
+----------------------------------------------------------------+
|                                                                |
|       +------------------------------------------------+       |
|       |             Newwork Protocol Stack             |       |
|       +------------------------------------------------+       |
|              ↑               ↑               ↑                 |
|..............|...............|...............|.................|
|              ↓               ↓               ↓                 |
|        +----------+    +-----------+   +-----------+           |
|        |   eth0   |    |   veth0   |   |   veth1   |           |
|        +----------+    +-----------+   +-----------+           |
|192.168.1.11  ↑               ↑               ↑                 |
|              |               +---------------+                 |
|              |         192.168.2.11     192.168.2.1            |
+--------------|-------------------------------------------------+
               ↓
         Physical Network
示例
创建一对虚拟网卡,新建的网卡默认都是关闭的,名称空间里的回环网卡lo默认也是关闭的。
# ip link add kk type veth peer name vkk
# ifconfig -a
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.251.53.190  netmask 255.255.248.0  broadcast 10.251.55.255
        ether 00:16:3e:00:3c:24  txqueuelen 1000  (Ethernet)
        RX packets 516917  bytes 522231637 (498.0 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 410017  bytes 29372384 (28.0 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
kk: flags=4098<BROADCAST,MULTICAST>  mtu 1500
        ether 32:24:c6:46:81:ac  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
lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        loop  txqueuelen 0  (Local Loopback)
        RX packets 37561  bytes 3506524 (3.3 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 37561  bytes 3506524 (3.3 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
vkk: flags=4098<BROADCAST,MULTICAST>  mtu 1500
        ether 92:92:2c:df:be:bd  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 netns add test
# ip netns list
将虚拟网卡vkk移动到名称空间,这时vkk就查看不到了
# ip link set vkk netns test
# ifconfig -a
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.251.53.190  netmask 255.255.248.0  broadcast 10.251.55.255
        ether 00:16:3e:00:3c:24  txqueuelen 1000  (Ethernet)
        RX packets 516917  bytes 522231637 (498.0 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 410017  bytes 29372384 (28.0 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
kk: flags=4098<BROADCAST,MULTICAST>  mtu 1500
        ether 32:24:c6:46:81:ac  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
lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        loop  txqueuelen 0  (Local Loopback)
        RX packets 37561  bytes 3506524 (3.3 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 37561  bytes 3506524 (3.3 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
进入名称空间,发现刚才的虚拟网卡已经在名称空间里面了。
# ip netns exec test bash
# ifconfig -a
lo: flags=8<LOOPBACK>  mtu 65536
        loop  txqueuelen 0  (Local Loopback)
        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
vkk: flags=4098<BROADCAST,MULTICAST>  mtu 1500
        ether 92:92:2c:df:be:bd  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 netns的更多相关文章
- ip netns相关命令
		1.增加虚拟网络命名空间 ip netns add net0 2.显示所有的虚拟网络命名空间 EULER:~ # ip netns list net0 也可通过查看/var/run/netns ... 
- Linux ip netns 命令
		ip netns 命令用来管理 network namespace.它可以创建命名的 network namespace,然后通过名字来引用 network namespace,所以使用起来很方便. ... 
- 虚拟化技术及ip netns简介
		虚拟化技术: Iass:infrastructure as a server 直接启动一个虚拟机,需要什么程序自己安装 Paas:platform as a servicce 启动一个虚拟机,并安装了 ... 
- Linux 中 ip netns 命令
		通过 ip netns help 可以查看所有关于ip netns的命令: network namespace 在逻辑上是网络堆栈的一个副本,它有自己的路由.防火墙规则和网络设备. ip netns ... 
- Linux下使用ip netns命令进行网口的隔离和配置ip地址
		1. 添加隔离标记符: ip netns add fd 2. 将指定网卡放入隔离中: ip link set eth1 netns fd 3. 在隔离环境下执行命令: ip netns exec fd ... 
- 【Docker】Docker主机为什么ip nets 查不到网络空间
		创建Docker容器后本来应该有新的命名空间(如果有独立网络的话),那么可以通过 ip netns 命令查看到命名空间,但是实际上却看不到. 查过资料才发现,ip netns 只能查看到 /var/r ... 
- 为docker配置固定ip
		docker默认使用bridge模式,通过网桥连接到宿主机,而容器内部的ip则从网桥所在的ip段取未用的ip.这样做一个不方便的地方在于容器内部的ip不是固定的,想要连接容器时只能通过映射到宿主机的端 ... 
- docker容器分配静态IP
		最近因为工作要求需要用学习使用docker,最后卡在了网络配置这一块.默认情况下启动容器的时候,docker容器使用的是bridge策略比如: docker run -ti ubuntu:latest ... 
- docker学习笔记一:基本安装和设置容器静态ip
		docker是一个lxc升级版的容器类虚拟环境,具有快速部署,灵活,易迁移的虚拟机模式,现在各大公司已经开始广泛使用为了自己方便学习linux,需要多台虚拟机环境,但是vmware开启多台虚拟机时需要 ... 
随机推荐
- GearCase UI - 自己构建一套基于 Vue 的简易开源组件库
			最近 1 ~ 2 月除了开发小程序之外,还一直在继续深入的学习 Vuejs.利用零碎.闲暇的时间整合了一套基于 Vue 的 UI 组件库.命名为 GearCase UI,意为齿轮盒.现在把该项目进行开 ... 
- JAVA之异常处理(一)
			JAVA之异常处理(一) 1.异常概述 在程序的开发过程中,可能存在各种各样的错误,有些错误是可以避免的,而有些错误却是意想不到的,在Java中把这些可能发生的错误称为异常.异常类的继承关系如下图. ... 
- VMware两台虚拟机之间文件共享
			虚拟机A的文件拷贝到虚拟机B scp[参数][原路径][目标路径] eg: scp -r root@192.168.0.172:/home/rookie/下载/ /home/rooookie/下载/ ... 
- Sentence | Never underestimate yourself.
			"\(Our\) \(deepest\) \(fear\) \(is\) \(not\) \(that\) \(we\) \(are\) $inadequate. $ \(Our\) \(d ... 
- 图片人脸检测(OpenCV版)
			图片人脸检测 人脸检测使用到的技术是OpenCV,上一节已经介绍了OpenCV的环境安装,点击查看. 功能展示 识别一种图上的所有人的脸,并且标出人脸的位置,画出人眼以及嘴的位置,展示效果图如下: 多 ... 
- sprint2(第九天)
			今天是sprint2的最后一天,已经完成功能有可以实现点餐功能.菜品的添加和删减.菜品数量的增减.添加备注.查看订单详情.订单状态.提交订单.后厨可以查看订单信息,对菜品的状态进行操作,是否完成烹饪, ... 
- TeamWork#1,Week 5,Suggestions for Team Project
			我们团队联系到了我们六班的直系学长,并向他咨询了软件工程基础这门课的团队项目相关的问题.他们团队的名字命名为Z-XML,团队中的几个学长也都是我平时所熟识的.虽然学长已经大四,忙着考研工作等各种事务, ... 
- [buaa-SE-2017]个人作业-Week1
			个人作业-Week1 Part1:教材中不懂的问题 1.根据书中"除了前20的学校之外,计科和软工没有区别"所以计算机科学这个专业也许在我们学校是和软件工程有区别的,但是可以料想的 ... 
- scanf()  scanf_s()  区别
			写博原因:这几天由于小学期的缘故,接触到了好多C代码,在VS2013中编译的时候,遇到了如下问题: 错误 1 error C4996: 'scanf': This function or variab ... 
- 16_常用API_第16天(正则表达式、Date、DateFormat、Calendar)_讲义
			今日内容介绍 1.正则表达式的定义及使用 2.Date类的用法 3.Calendar类的用法 ==========================================第一阶段======= ... 
