当前位置:网站首页>ip netns 命令(备忘)
ip netns 命令(备忘)
2022-07-07 16:51:00 【fananchong2】
Linux Network Namespace
Linux Network Namespace 是实现容器化的基础
ip netns 是基于 Linux Network Namespace 的一个实用工具
这里熟悉下 ip netns 的使用,来理解如何 Linux 下的网络虚拟化
创建 1 个虚拟网络
# 创建一个名为 netns1 的 network namespace
sudo ip netns add netns1
# 使用 ip netns exec 命令进入 network namespace
sudo ip netns exec netns1 ip link list
# 进入 netns1 这个 network namespace ,把设备状态设置成 UP
sudo ip netns exec netns1 ip link set dev lo up
# 尝试 ping netns1 这个 network namespace 的 127.0.0.1
sudo ip netns exec netns1 ping 127.0.0.1
# 查看系统中有哪些 network namespace
ip netns list
# 删除 network namespace
sudo ip netns delete netns1
2 个虚拟网络互通
#!/bin/bash
# 创建 network namespace ns0 ns1
sudo ip netns add ns0
sudo ip netns add ns1
# 使用 veth pair 创建 2 张虚拟网卡;分别加到 ns0 ns1 wetowrk namespace
sudo ip link add veth0 type veth peer name veth1
sudo ip link set veth0 netns ns0
sudo ip link set veth1 netns ns1
# 绑定 ip
sudo ip netns exec ns0 ip link set dev lo up
sudo ip netns exec ns1 ip link set dev lo up
sudo ip netns exec ns0 ifconfig veth0 10.1.1.1/24 up
sudo ip netns exec ns1 ifconfig veth1 10.1.1.2/24 up
# 删除 network namespace
sudo ip netns delete ns0
sudo ip netns delete ns1
多个虚拟网络互通
#!/bin/bash
### centos
## yum install -y bridge-utils
### ubuntu
## apt-get install -y bridge-utils
# 添加网桥 br0
sudo brctl addbr br0
# 启动网桥 br0
sudo ip link set br0 up
# 创建 network namespace ns0 ns1 ns2
sudo ip netns add ns0
sudo ip netns add ns1
sudo ip netns add ns2
# 创建 veth peer
sudo ip link add veth0-ns type veth peer name veth0-br
sudo ip link add veth1-ns type veth peer name veth1-br
sudo ip link add veth2-ns type veth peer name veth2-br
# 将 veth 的一端移动到netns中
sudo ip link set veth0-ns netns ns0
sudo ip link set veth1-ns netns ns1
sudo ip link set veth2-ns netns ns2
# 绑定 ip
sudo ip netns exec ns0 ip link set dev lo up
sudo ip netns exec ns1 ip link set dev lo up
sudo ip netns exec ns2 ip link set dev lo up
sudo ip netns exec ns0 ifconfig veth0-ns 10.1.1.1/24 up
sudo ip netns exec ns1 ifconfig veth1-ns 10.1.1.2/24 up
sudo ip netns exec ns2 ifconfig veth2-ns 10.1.1.3/24 up
# 设置默认路由,可以通宿主机
sudo ip netns exec ns0 route add default gw 10.1.1.254 veth0-ns
sudo ip netns exec ns1 route add default gw 10.1.1.254 veth1-ns
sudo ip netns exec ns2 route add default gw 10.1.1.254 veth2-ns
# 将 veth 的另一端启动并挂载到网桥上
sudo ip link set veth0-br up
sudo ip link set veth1-br up
sudo ip link set veth2-br up
sudo brctl addif br0 veth0-br
sudo brctl addif br0 veth1-br
sudo brctl addif br0 veth2-br
# 给网桥设置 IP
sudo ip addr add 10.1.1.254/24 dev br0
# 删除网桥
sudo ifconfig veth0-br 0
sudo ifconfig veth1-br 0
sudo ifconfig veth2-br 0
sudo brctl delif br0 veth0-br
sudo brctl delif br0 veth1-br
sudo brctl delif br0 veth2-br
sudo ip link set br0 down
sudo brctl delbr br0
其他
- 暂时没打通虚拟网络如何访问外网
- 主机重启,以上命令的网络设置会被重置,放心实验
边栏推荐
- Some key points in the analysis of spot Silver
- 3.关于cookie
- “解密”华为机器视觉军团:华为向上,产业向前
- Hash, bitmap and bloom filter for mass data De duplication
- Is it safe to open an online futures account now? How many regular futures companies are there in China?
- Static routing configuration
- Redis的发布与订阅
- How to clean when win11 C disk is full? Win11 method of cleaning C disk
- 五种网络IO模型
- 企业展厅设计中常用的三种多媒体技术形式
猜你喜欢

标准ACL与扩展ACL

String type, constant type and container type of go language

Year SQL audit platform

Discuss | what preparations should be made before ar application is launched?

Industry case | digital operation base helps the transformation of life insurance industry

Simple configuration of single arm routing and layer 3 switching
![[PaddleSeg源码阅读] PaddleSeg Validation 中添加 Boundary IoU的计算(1)——val.py文件细节提示](/img/f2/b6a0e5512b35cf1b695a8feecd0895.png)
[PaddleSeg源码阅读] PaddleSeg Validation 中添加 Boundary IoU的计算(1)——val.py文件细节提示

强化学习-学习笔记8 | Q-learning

备份阿里云实例-oss-browser
![[tpm2.0 principle and Application guide] Chapter 5, 7 and 8](/img/38/93fd986916193803bbd90805f832fa.png)
[tpm2.0 principle and Application guide] Chapter 5, 7 and 8
随机推荐
Yunjing network technology interview question [Hangzhou multi tester] [Hangzhou multi tester _ Wang Sir]
PHP面试题 foreach($arr as &$value)与foreach($arr as $value)的用法
小试牛刀之NunJucks模板引擎
五种网络IO模型
3.关于cookie
Wireshark analyzes packet capture data * cap
云安全日报220707:思科Expressway系列和网真视频通信服务器发现远程攻击漏洞,需要尽快升级
Tips for short-term operation of spot silver that cannot be ignored
现货白银分析中的一些要点
高考填志愿规则
伺服力矩控制模式下的力矩目标值(fTorque)计算
用存储过程、定时器、触发器来解决数据分析问题
NAT地址转换
手撕Nacos源码(先撕客户端源码)
sqlite sql 异常 near “with“: syntax error
云景网络科技面试题【杭州多测师】【杭州多测师_王sir】
【剑指 Offer】59 - I. 滑动窗口的最大值
[principle and technology of network attack and Defense] Chapter 1: Introduction
[PaddleSeg源码阅读] PaddleSeg Validation 中添加 Boundary IoU的计算(1)——val.py文件细节提示
Recommend free online SMS receiving platform in 2022 (domestic and foreign)