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开启多台虚拟机时需要 ...
随机推荐
- [egret]白鹭引擎打包安卓包体积太大减小
萌新第一次用egret打安卓包,发现裸包22M+,吃惊到吃手手. 上网搜查无果. 可能原因是egret优化过一波打包,变得更便利了,网上对新版本打包比较少讨论. 解决方法: egret-android ...
- K-SVD字典学习及其实现(Python)
算法思想 算法求解思路为交替迭代的进行稀疏编码和字典更新两个步骤. K-SVD在构建字典步骤中,K-SVD不仅仅将原子依次更新,对于原子对应的稀疏矩阵中行向量也依次进行了修正. 不像MOP,K-SVD ...
- markdown | Latex | 书写测试
我永远喜欢markdown! 建图 graph TD; 1-->2 1-->3 1-->4 2-->5 2-->6 3-->7 3-->8 4-->9 ...
- linux下自己安装软件做成命令
安装nodejs,从官网下下来的是一个压缩包,解压下里面有可执行文件.教程上用ln -s [可执行文件的绝对路径] [/usr/local/bin/]建了一个软连接,这样就能直接无视路径,用命令行调用 ...
- Bing词典vs有道词典比对测试报告——体验篇之软件适应性
联网情况: 在联网情况下,针对每一次查询,有道词典的反应速度明显比必应词典快得多.据我推测有以下两个原因: 有道词典有本地词库而必应词典更多依赖联网. 有道词典的服务器在国内而必应的在国外. 断网情况 ...
- Daily Scrum2 11.4
昨天的任务大家都已经完成,daily scrum记录的是当日已经完成的任务. 今日任务列表: 杨伊:完成团队作业之软件测评的功能部分 徐钧鸿:CodingCook的model和helper部分 张艺: ...
- [BUAA OO]第一次博客作业
第一次作业 第一次进行面向对象的编程,不论是针对数据设计类还是对方法进行合适的归于不同类中,都不是很熟悉.所写出来的程序还是面向过程+有函数的类(虽然现在很大程度上感觉起来也是这样).索性作业难度并不 ...
- Task 9 从用户界面和体验分析“360极速浏览器”
我目前使用的浏览器是360极速浏览器,下面将针对用户界面.记住用户选择.短期刺激.长期使用的好处坏处.不要让用户犯简单的错误四个方面对其进行评估: 1.用户界面: 01 可视性原则--网络没有连接或者 ...
- Pl/sql学习笔记2
-- declare type vsal_table is table of emp.sal%type; a vsal_table; begin --必须得初始化 并且有数量上的区分 从一开的 a ...
- DocX插件
DocX是一个用C#编写的.NET库,它允许开发人员以简单直观的方式操作Word文件.