网络名称空间和网桥的基本操作

  1. 网络命名空间和网桥的基本操作命令

网络名称空间 (NET Namespace)

  1. 查看是否有iproute
[root@localhost ~]# rpm -q iproute
iproute-4.11.0-14.el7.x86_64
  1. 创建n1, n2 网络名称空间
[root@localhost ~]# ip netns add n1
[root@localhost ~]# ip netns add n2

这种方式创建只有网络名称空间是独立的, 其他名称空间不是独立的。

  1. 查看网络名称空间
[root@localhost ~]# ip netns list
n2
n1
  1. n1网络空间里执行查看网卡接口命令
[root@localhost ~]# ip netns exec n1 ifconfig -a
lo: flags=8<LOOPBACK>  mtu 65536loop  txqueuelen 1000  (Local Loopback)RX packets 0  bytes 0 (0.0 B)RX errors 0  dropped 0  overruns 0  frame 0TX packets 0  bytes 0 (0.0 B)TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
  1. 创建两个以太网网卡对并查看设备
[root@localhost ~]# ip link add name veth1.1 type veth peer name veth1.2
[root@localhost ~]# ip link show | grep 'veth'
13: veth1.2@veth1.1: <BROADCAST,MULTICAST,M-DOWN> mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000
14: veth1.1@veth1.2: <BROADCAST,MULTICAST,M-DOWN> mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000
  1. 把veth1.2放到n1里
[root@localhost ~]# ip link set dev veth1.2 netns n1
[root@localhost ~]# ip link show | grep 'veth'
14: veth1.1@if13: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000
  1. 查看n1
[root@localhost ~]# ip netns exec n1 ifconfig -a | grep 'veth'
veth1.2: flags=4098<BROADCAST,MULTICAST>  mtu 1500
  1. 在n1把veth1.2改成eth0
[root@localhost ~]# ip netns exec n1 ip link set dev veth1.2 name eth0
[root@localhost ~]# ip netns exec n1 ifconfig -a | grep 'veth'
[root@localhost ~]# ip netns exec n1 ifconfig -a | grep 'eth0'
eth0: flags=4098<BROADCAST,MULTICAST>  mtu 1500
  1. 启动veth1.1并赋予IP
[root@localhost ~]# ifconfig -a | grep 'veth'
veth1.1: flags=4098<BROADCAST,MULTICAST>  mtu 1500
[root@localhost ~]# ifconfig veth1.1 10.1.0.1/24 up
[root@localhost ~]# ifconfig -a | grep 'veth'
veth1.1: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
  1. 把n1里的也启动并赋予IP
[root@localhost ~]# ip netns exec n1 ifconfig eth0 10.1.0.2/24 up
[root@localhost ~]# ip netns exec n1 ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500inet 10.1.0.2  netmask 255.255.255.0  broadcast 10.1.0.255inet6 fe80::a0dd:c4ff:fe13:38f0  prefixlen 64  scopeid 0x20<link>ether a2:dd:c4:13:38:f0  txqueuelen 1000  (Ethernet)RX packets 8  bytes 648 (648.0 B)RX errors 0  dropped 0  overruns 0  frame 0TX packets 8  bytes 648 (648.0 B)TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
  1. 主机和n1通信
[root@localhost ~]# ping -c3 10.1.0.2
PING 10.1.0.2 (10.1.0.2) 56(84) bytes of data.
64 bytes from 10.1.0.2: icmp_seq=1 ttl=64 time=0.075 ms
64 bytes from 10.1.0.2: icmp_seq=2 ttl=64 time=0.038 ms
64 bytes from 10.1.0.2: icmp_seq=3 ttl=64 time=0.041 ms--- 10.1.0.2 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2001ms
rtt min/avg/max/mdev = 0.038/0.051/0.075/0.017 ms
  1. 把veth1.1放到n2
[root@localhost ~]# ip link set dev veth1.1 netns n2
[root@localhost ~]# ip netns exec n2 ifconfig veth1.1 10.1.0.1/24 up
[root@localhost ~]# ip netns exec n2 ifconfig
veth1.1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500inet 10.1.0.1  netmask 255.255.255.0  broadcast 10.1.0.255inet6 fe80::1097:c8ff:fe37:2e36  prefixlen 64  scopeid 0x20<link>ether 12:97:c8:37:2e:36  txqueuelen 1000  (Ethernet)RX packets 13  bytes 1026 (1.0 KiB)RX errors 0  dropped 0  overruns 0  frame 0TX packets 21  bytes 1674 (1.6 KiB)TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
  1. n2通信n1
[root@localhost ~]# ip netns exec n2 ping -c3 10.1.0.2
PING 10.1.0.2 (10.1.0.2) 56(84) bytes of data.
64 bytes from 10.1.0.2: icmp_seq=1 ttl=64 time=0.102 ms
64 bytes from 10.1.0.2: icmp_seq=2 ttl=64 time=0.037 ms
64 bytes from 10.1.0.2: icmp_seq=3 ttl=64 time=0.038 ms--- 10.1.0.2 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 1999ms
rtt min/avg/max/mdev = 0.037/0.059/0.102/0.030 ms
  1. 主机通信n1或者n2都不通

网桥

  1. 创建br0网桥并启动
[root@localhost ~]# ip link add name br0 type bridge
[root@localhost ~]# ip link set br0 up
[root@localhost ~]# ifconfig br0
br0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500inet6 fe80::463:74ff:fe4c:10cf  prefixlen 64  scopeid 0x20<link>ether 06:63:74:4c:10:cf  txqueuelen 1000  (Ethernet)RX packets 0  bytes 0 (0.0 B)RX errors 0  dropped 0  overruns 0  frame 0TX packets 6  bytes 508 (508.0 B)TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
  1. 创建以太网(veth)类型的网卡对 veth0 和 veth1
[root@localhost ~]# ip link add veth0 type veth peer name veth1
[root@localhost ~]# ip addr add 10.20.1.10/24 dev veth0
[root@localhost ~]# ip link set veth0 up
[root@localhost ~]# ifconfig | grep -A 2 'veth'
veth0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500inet 10.20.1.10  netmask 255.255.255.0  broadcast 0.0.0.0ether d6:b7:5a:b2:5b:a6  txqueuelen 1000  (Ethernet)
  1. 创建ns1网络名称空间并把veth1加入
[root@localhost ~]# ip netns add ns1
[root@localhost ~]# ip link set dev veth1 netns ns1
[root@localhost ~]# ip netns exec ns1 ip addr add 10.20.1.20/24 dev veth1
[root@localhost ~]# ip netns exec ns1 ip link set veth1 up
[root@localhost ~]# ip netns exec ns1 ifconfig
veth1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500inet 10.20.1.20  netmask 255.255.255.0  broadcast 0.0.0.0inet6 fe80::e018:ebff:fe42:18df  prefixlen 64  scopeid 0x20<link>ether e2:18:eb:42:18:df  txqueuelen 1000  (Ethernet)RX packets 8  bytes 648 (648.0 B)RX errors 0  dropped 0  overruns 0  frame 0TX packets 8  bytes 648 (648.0 B)TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
  1. veth0和veth1通信成功
[root@localhost ~]# ping -c1 10.20.1.20
PING 10.20.1.20 (10.20.1.20) 56(84) bytes of data.
64 bytes from 10.20.1.20: icmp_seq=1 ttl=64 time=0.056 ms--- 10.20.1.20 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.056/0.056/0.056/0.000 ms
  1. 把veth0连接上网桥br0
[root@localhost ~]# ip link set dev veth0 master br0
[root@localhost ~]# bridge link
17: veth0 state UP @(null): <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 master br0 state forwarding priority 32 cost 2

此时br0的mac地址为veth0的mac地址且veth0不再转发数据给内核(协议栈),而是br0来转发。

  1. veth0和veth1通信失败
[root@localhost ~]# ping -c1 10.20.1.20
PING 10.20.1.20 (10.20.1.20) 56(84) bytes of data.--- 10.20.1.20 ping statistics ---
1 packets transmitted, 0 received, 100% packet loss, time 0ms
  1. 给br0配置ip来转发数据给内核(协议栈),接着通信还是失败
[root@localhost ~]# ip addr add 10.20.1.15/24 dev br0
[root@localhost ~]# ifconfig br0
br0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500inet 10.20.1.15  netmask 255.255.255.0  broadcast 0.0.0.0inet6 fe80::463:74ff:fe4c:10cf  prefixlen 64  scopeid 0x20<link>ether d6:b7:5a:b2:5b:a6  txqueuelen 1000  (Ethernet)RX packets 5  bytes 196 (196.0 B)RX errors 0  dropped 0  overruns 0  frame 0TX packets 8  bytes 648 (648.0 B)TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0[root@localhost ~]# ping -c1 10.20.1.20
PING 10.20.1.20 (10.20.1.20) 56(84) bytes of data.
From 10.20.1.10 icmp_seq=1 Destination Host Unreachable--- 10.20.1.20 ping statistics ---
1 packets transmitted, 0 received, +1 errors, 100% packet loss, time 0ms
  1. 删除veth0的默认路由,接着通信成功
[root@localhost ~]# ip route show | grep '10.20.1.0'
10.20.1.0/24 dev veth0 proto kernel scope link src 10.20.1.10
10.20.1.0/24 dev br0 proto kernel scope link src 10.20.1.15
[root@localhost ~]# ip route del 10.20.1.0/24 dev veth0
[root@localhost ~]# ip route show | grep '10.20.1.0'
10.20.1.0/24 dev br0 proto kernel scope link src 10.20.1.15
[root@localhost ~]# ping -c1 10.20.1.20
PING 10.20.1.20 (10.20.1.20) 56(84) bytes of data.
64 bytes from 10.20.1.20: icmp_seq=1 ttl=64 time=0.062 ms--- 10.20.1.20 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.062/0.062/0.062/0.000 ms
  1. 同理可把物理网卡桥接到网桥

《2021/07/24》1 -- linux -- 网络名称空间和网桥的基本操作相关推荐

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

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

  2. 网络名称空间netns的用法

    ip命令 linux 的强大的网络配置命令'ip'. netns可以让一台机器上模拟多个网络设备,是网络虚拟化的重要组成,将不同类型的网络应用隔离. 一个net namespace有自己独立的路由表, ...

  3. 对电影题材分析的案例-电影类型与电影利润之间的关系(2021/07/24)

    如题,分析内容是电影题材和电影利润之间的关系. 其中电影题材的数据格式比较特殊,那么就用这个案例体现怎么处理此类数据格式. 从数据中看,每个电影对应好几种题材(genres). 那么,我们的大概思路是 ...

  4. Linux网络与进程管理

    本文主要介绍了网络与进程的相关内容以及在Linux上如何进行网络与进程管理,包括以太网介绍,TCP/IP,网络分层模型,IP地址的规划:同时介绍了Linux内核在进程管理方面的相关机制 文章目录 一. ...

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

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

  6. Docker的名称空间

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

  7. linux 网络命名空间 Network namespaces

    Linux命名空间是一个相对较新的内核功能,对于实现容器至关重要. 命名空间将全局系统资源包装到一个抽象中,该抽象只会与命名空间中的进程绑定,从而提供资源隔离. 在本文中,我将讨论网络命名空间并展示一 ...

  8. linux进程及网络报告,linux网络和进程管理简述

    OSI七层模型和TCP/IP模型简述 OSI模型 OSI模型,即开放式通信系统互联参考模型(Open System Interconnection Reference Model),是国际标准化组织( ...

  9. 理解 Linux 网络命名空间

    如果你使用过 Docker 和 Kubernetes,那么可能应该听说过 network namespace(网络命名空间),最近在我们的 <Kubernetes 网络训练营>课程中学习到 ...

最新文章

  1. VS2010没有Intellisense(智能感知)的解决办法
  2. 推荐系统resys小组线下活动见闻2009-08-22
  3. 第二章matlab数据及其运算,第2章 MATLAB数据及其运算_习题答案
  4. DateTimePicker 日期时间选择器报错 Cannot read property ‘getHours‘ of undefined, 无法选中`[__ob_: observer__]`时做判断
  5. EKF优化:协方差coff公式、意义、SLAM中的Code优化
  6. 科密a1无法连接数据库_支持无线图传的稳定器——致迅A1-pro图传稳定器
  7. 增加字库 安卓_【Android】Android4.4添加新语言和字库
  8. c hello world
  9. 毕设日志——Faster RCNN
  10. linux安装vnc4server,Ubuntu 18.04安装vnc4server
  11. 二维数组七行七列C语言,C语言中级教程 再谈数组-7.ppt
  12. list类型的extend与append方法
  13. composer错误Could not find package 的解决方法
  14. 【分享】5G+北斗RTK高精度人员定位解决方案
  15. IntelliJ IDEA开发Java web项目,设置JSP代码自动补全的方法
  16. 信号与系统 基础知识点整理 01(文末可下载PDF格式)
  17. 模式识别与机器学习的关系
  18. BPM平台在企业业务系统中使用的价值讨论
  19. ps4如何通过虚拟服务器联机,PS4联机路由电信用自定义host
  20. c语言编译作用,叙述 C语言编译

热门文章

  1. 运营之光2.0 我的互联网运营方法论与自白
  2. 暗金色 rgb_杜伽TAURUS K310樱桃RGB红轴体验:做工精良、手感优秀
  3. Verilog消除毛刺
  4. Java彩信接口开发经验及具体开发实现
  5. 鲁大师12月新机性能/流畅榜:小米系包揽性能榜前三,流畅榜上限再突破!
  6. 「GoTeam 招聘时间」深信服科技 Go 开发工程师(成都)
  7. 音视频直播流程及常见视频流协议介绍
  8. 作为一名iOS开发者—面对音视频这个新风口应该怎样学习才能乘风而起?
  9. 基于DHT网络的最强BT资源搜索引擎engiy.com
  10. WES分析2-分析流程