当前位置:网站首页>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
其他
- 暂时没打通虚拟网络如何访问外网
- 主机重启,以上命令的网络设置会被重置,放心实验
边栏推荐
- AI 击败了人类,设计了更好的经济机制
- sqlite sql 异常 near “with“: syntax error
- Debian10 compile and install MySQL
- Recommend free online SMS receiving platform in 2022 (domestic and foreign)
- 体总:安全有序恢复线下体育赛事,力争做到国内赛事应办尽办
- [principle and technology of network attack and Defense] Chapter 7: password attack technology Chapter 8: network monitoring technology
- Usage of PHP interview questions foreach ($arr as $value) and foreach ($arr as $value)
- Save the memory of the model! Meta & UC Berkeley proposed memvit. The modeling time support is 30 times longer than the existing model, and the calculation amount is only increased by 4.5%
- Summary of evaluation indicators and important knowledge points of regression problems
- Static routing configuration
猜你喜欢
Simple configuration of single arm routing and layer 3 switching
C语言中匿名的最高境界
直播预约通道开启!解锁音视频应用快速上线的秘诀
嵌入式C语言程序调试和宏使用的技巧
Redis集群与扩展
【Unity Shader】插入Pass实现模型遮挡X光透视效果
Five network IO models
Classification of regression tests
Comparison and selection of kubernetes Devops CD Tools
能同时做三个分割任务的模型,性能和效率优于MaskFormer!Meta&UIUC提出通用分割模型,性能优于任务特定模型!开源!...
随机推荐
Personal best practice demo sharing of enum + validation
[trusted computing] Lesson 13: TPM extended authorization and key management
[paper sharing] where's crypto?
debian10系统问题总结
[trusted computing] Lesson 12: TPM authorization and conversation
回归问题的评价指标和重要知识点总结
云安全日报220707:思科Expressway系列和网真视频通信服务器发现远程攻击漏洞,需要尽快升级
Live broadcast software construction, canvas Text Bold
Differences between rip and OSPF and configuration commands
Improve application security through nonce field of play integrity API
The live broadcast reservation channel is open! Unlock the secret of fast launching of audio and video applications
Antisamy: a solution against XSS attack tutorial
Kirk borne's selection of learning resources this week [click the title to download directly]
能同时做三个分割任务的模型,性能和效率优于MaskFormer!Meta&UIUC提出通用分割模型,性能优于任务特定模型!开源!...
[sword finger offer] 59 - I. maximum value of sliding window
Static routing configuration
Do you really understand sticky bag and half bag? 3 minutes to understand it
体总:安全有序恢复线下体育赛事,力争做到国内赛事应办尽办
【demo】循环队列及条件锁实现goroutine间的通信
Hash, bitmap and bloom filter for mass data De duplication