07
2019
05
17:57:37

网络名称空间netns的用法



推荐点击下面图片,通过本站淘宝优惠价购买:

image.png

ip命令

       linux 的强大的网络配置命令‘ip’。


bridge id STP enabled interfaces

br-ex 8000.000c294bbbd0 no eno16777736

br-in 8000.0e1d0f339fc2 no vif1.0

vif2.0



vif1.0 和vif2.0都是桥接在br-in上了。


好了,现在的情况相当于是vm1,vm2在一个交换机上。这个交换机就是br-in。为了这两个vm虚拟机可以和外界通信,必须要再创建一个虚拟的路由器。删去刚才的r1,r2。


添加路由器R1.



[root@localhost ~]# ip netns add r1


为路由器R1添加一对网卡并且启动。


[root@localhost ~]# ip link add rinr type veth peer name rins

[root@localhost ~]# ip link set rinr up

[root@localhost ~]# ip link set rins up


将网卡添加到桥上去。


[root@localhost ~]# brctl addif br-in rins

[root@localhost ~]# brctl show

bridge name bridge id STP enabled interfaces

br-ex 8000.000c294bbbd0 no eno16777736

br-in 8000.0e1d0f339fc2 no rins

vif1.0

vif2.0


给rinr改个名字,并且启动


[root@localhost ~]# ip link set rinr netns r1 #将网卡rinr添加至r1

[root@localhost ~]# ip netns exec r1 ip link set rinr name eth0

[root@localhost ~]# ip netns exec r1 ip link set eth0 up


添加一个IP,作为网关。


[root@localhost ~]# ip netns exec r1 ifconfig eth0 10.0.1.254/24 up

[root@localhost ~]# ip netns exec r1 ifconfig

eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500

        inet 10.0.1.254  netmask 255.255.255.0  broadcast 10.0.1.255

        inet6 fe80::f8b4:bff:fee4:b12d  prefixlen 64  scopeid 0x20<link>

        ether fa:b4:0b:e4:b1:2d  txqueuelen 1000  (Ethernet)

        RX packets 8  bytes 648 (648.0 B)

        RX errors 0  dropped 0  overruns 0  frame 0

        TX packets 16  bytes 1296 (1.2 KiB)

        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0


两个虚拟机的网关都指向10.0.1.254


$ sudo su -

# ifconfig eth0 10.0.1.2/24 up

# route add default gw 10.0.1.254


目前来说整幅图的左半边完全好了。

开始右半边。


添加一对网卡,再把其中一个桥接



[root@localhost ~]# ip link add rexr type veth peer name rexs

[root@localhost ~]# brctl addif br-ex rexs

[root@localhost ~]# ip link set rexs up

[root@localhost ~]# brctl show 

bridge name bridge id STP enabled interfaces

br-ex 8000.000c294bbbd0 no eno16777736

rexs

br-in 8000.0e1d0f339fc2 no rins

vif1.0

vif2.0



将另一个网卡添加到路由器的另一边,且给个另一个网络的地址



[root@localhost ~]# ip link set rexr  netns r1

[root@localhost ~]# ip netns exec r1 ip link set rexr name eth1

[root@localhost ~]# ip netns exec r1 ifconfig eth1 192.168.217.77/24 up


利用防火墙的源地址转换,实现将内网中的地址转换。


[root@localhost ~]# ip netns exec r1 iptables -t nat -A POSTROUTING -s 10.0.1.0/24 ! -d 10.0.1.0/24 -j SNAT --to-source 192.168.217.77

[root@localhost ~]# ip netns exec r1 iptables -t nat -nL

Chain PREROUTING (policy ACCEPT)

target     prot opt source               destination         

 

Chain INPUT (policy ACCEPT)

target     prot opt source               destination         

 

Chain OUTPUT (policy ACCEPT)

target     prot opt source               destination         

 

Chain POSTROUTING (policy ACCEPT)

测试。vm1可以ping同vm2.vm1可以访问真机所在局域网的主机。


# hostname 

cirros

# ping 10.0.1.1

PING 10.0.1.1 (10.0.1.1): 56 data bytes

64 bytes from 10.0.1.1: seq=0 ttl=64 time=4.612 ms

# ping 192.168.217.2

PING 192.168.217.2 (192.168.217.2): 56 data bytes

64 bytes from 192.168.217.2: seq=0 ttl=127 time=4.742 ms

 

target prot opt source destination SNAT all -- 10.0.1.0/24 !10.0.1.0/24 to:192.168.217.77


当然。在左边那个网络中,还可以运行一个dhcp服务器,并且将网关自动指向10.0.1.254。 



[root@localhost ~]# yum -y install dnsmasq


执行命令



[root@localhost ~]# ip netns exec r1 dnsmasq -F 10.0.1.1,10.0.1.30 --dhcp-option=option:router,10.0.1.254


--------------------- 

作者:Ghost_02 

来源:CSDN 

原文:https://blog.csdn.net/ghost_leader/article/details/71075551 

版权声明:本文为博主原创文章,转载请附上博文链接!


本文链接:https://hqyman.cn/post/650.html 非本站原创文章欢迎转载,原创文章需保留本站地址!

分享到:





休息一下,本站随机推荐观看栏目:


« 上一篇 下一篇 »

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

您的IP地址是: