当前位置:网站首页>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
其他
- 暂时没打通虚拟网络如何访问外网
- 主机重启,以上命令的网络设置会被重置,放心实验
边栏推荐
- 备份阿里云实例-oss-browser
- 用存储过程、定时器、触发器来解决数据分析问题
- 磁盘存储链式的B树与B+树
- 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%
- [principle and technology of network attack and Defense] Chapter 1: Introduction
- 3.关于cookie
- [trusted computing] Lesson 12: TPM authorization and conversation
- 云安全日报220707:思科Expressway系列和网真视频通信服务器发现远程攻击漏洞,需要尽快升级
- 来了!GaussDB(for Cassandra)新特性亮相
- [network attack and defense principle and technology] Chapter 4: network scanning technology
猜你喜欢

【C语言】字符串函数

debian10编译安装mysql

Wireshark分析抓包数据*.cap

Download, installation and development environment construction of "harmonyos" deveco

小试牛刀之NunJucks模板引擎
![[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

The live broadcast reservation channel is open! Unlock the secret of fast launching of audio and video applications
![[paper sharing] where's crypto?](/img/27/9b47bfcdff8307e63f2699d6a4e1b4.png)
[paper sharing] where's crypto?

磁盘存储链式的B树与B+树

直播预约通道开启!解锁音视频应用快速上线的秘诀
随机推荐
6.关于jwt
Yunjing network technology interview question [Hangzhou multi tester] [Hangzhou multi tester _ Wang Sir]
海量数据去重的hash,bitmap与布隆过滤器Bloom Filter
能同时做三个分割任务的模型,性能和效率优于MaskFormer!Meta&UIUC提出通用分割模型,性能优于任务特定模型!开源!...
体总:安全有序恢复线下体育赛事,力争做到国内赛事应办尽办
Thread factory in thread pool
socket編程之常用api介紹與socket、select、poll、epoll高並發服務器模型代碼實現
Improve application security through nonce field of play integrity API
Industry case | digital operation base helps the transformation of life insurance industry
小程序中实现付款功能
coming! Gaussdb (for Cassandra) new features appear
[paper sharing] where's crypto?
debian10系统问题总结
Summary of evaluation indicators and important knowledge points of regression problems
AntiSamy:防 XSS 攻击的一种解决方案使用教程
Skills of embedded C language program debugging and macro use
Is it safe to open an online futures account now? How many regular futures companies are there in China?
Calculation of torque target value (ftorque) in servo torque control mode
Yearning-SQL审核平台
Backup Alibaba cloud instance OSS browser