1、ip命令介紹bash
ip命令是iproute軟件的程序
網絡
[root@host1 ~]# yum install iproute -y [root@host1 ~]# rpm -q iproute iproute-4.11.0-25.el7_7.2.x86_64
經過ip能夠實現管理網絡名稱空間tcp
[root@host1 ~]# ip Usage: ip [ OPTIONS ] OBJECT { COMMAND | help } ip [ -force ] -batch filename where OBJECT := { link | address | addrlabel | route | rule | neigh | ntable | tunnel | tuntap | maddress | mroute | mrule | monitor | xfrm | netns | l2tp | fou | macsec | tcp_metrics | token | netconf | ila | vrf } OPTIONS := { -V[ersion] | -s[tatistics] | -d[etails] | -r[esolve] | -h[uman-readable] | -iec | -f[amily] { inet | inet6 | ipx | dnet | mpls | bridge | link } | -4 | -6 | -I | -D | -B | -0 | -l[oops] { maximum-addr-flush-attempts } | -br[ief] | -o[neline] | -t[imestamp] | -ts[hort] | -b[atch] [filename] | -rc[vbuf] [size] | -n[etns] name | -a[ll] | -c[olor]}
OBJECT中的netns能夠用來設置網絡名稱空間
ide
netns的使用幫助以下oop
[root@host1 ~]# ip netns help Usage: ip netns list ip netns add NAME ip netns set NAME NETNSID ip [-all] netns delete [NAME] ip netns identify [PID] ip netns pids NAME ip [-all] netns exec [NAME] cmd ... ip netns monitor ip netns list-id
2、ip命令的使用spa
建立兩個名稱空間並查看一下
token
[root@host1 ~]# ip netns add r1 [root@host1 ~]# ip netns add r2
[root@host1 ~]# ip netns list r2 r1
查看網絡名稱空間中有幾個網卡ip
其實就是在網絡名稱空間中執行ip addr命令,須要加選項-acmd
在網絡名稱空間執行命令是用exec
it
[root@host1 ~]# ip netns exec r1 ip addr 1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
建立網卡對
用的命令是ip link
[root@host1 ~]# ip link help Usage: ip link add [link DEV] [ name ] NAME [ txqueuelen PACKETS ] [ address LLADDR ] [ broadcast LLADDR ] [ mtu MTU ] [index IDX ] [ numtxqueues QUEUE_COUNT ] [ numrxqueues QUEUE_COUNT ] type TYPE [ ARGS ]
建立一對網卡,兩端分別爲veth1.1和veth1.2
[root@host1 ~]# ip link add name veth1.1 type veth peer name veth1.2 [root@host1 ~]# ip link show | grep veth1.* 7: veth1.2@veth1.1: <BROADCAST,MULTICAST,M-DOWN> mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000 8: veth1.1@veth1.2: <BROADCAST,MULTICAST,M-DOWN> mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000
type veth:指定建立的是虛擬以太網卡
爲名稱空間分配虛擬網卡
用的命令是ip link set
一旦將某個虛擬網卡分配到某個名稱空間,在物理機中就看不到這個網卡了
將veth1.1保留在物理機,將veth1.2分配到r1名稱空間
[root@host1 ~]# ip link set veth1.2 netns r1
[root@host1 ~]# ip netns exec r1 ip addr 1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 7: veth1.2@if8: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN group default qlen 1000 link/ether 4e:f2:68:33:47:a1 brd ff:ff:ff:ff:ff:ff link-netnsid 0
[root@host1 ~]# ip link show | grep veth1.* 8: veth1.1@if7: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000
也能夠修改虛擬網卡的名稱,例如將r1中的veth1.2更名爲eth0
[root@host1 ~]# ip netns exec r1 ip link set dev veth1.2 name eth0
[root@host1 ~]# ip netns exec r1 ip addr 1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 7: eth0@if8: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN group default qlen 1000 link/ether 4e:f2:68:33:47:a1 brd ff:ff:ff:ff:ff:ff link-netnsid 0
將veth1.1地址設置爲10.0.0.1/8,將r1中的eth0地址地址設置爲10.0.0.2/8
[root@host1 ~]# ip addr add 10.0.0.1/8 dev veth1.1
[root@host1 ~]# ip netns exec r1 ip addr add 10.0.0.2/8 dev eth0
[root@host1 ~]# ip addr show veth1.1 8: veth1.1@if7: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN group default qlen 1000 link/ether 1e:62:af:a2:bc:6d brd ff:ff:ff:ff:ff:ff link-netnsid 1 inet 10.0.0.1/8 scope global veth1.1 valid_lft forever preferred_lft forever [root@host1 ~]# ip netns exec r1 ip addr show eth0 7: eth0@if8: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN group default qlen 1000 link/ether 4e:f2:68:33:47:a1 brd ff:ff:ff:ff:ff:ff link-netnsid 0 inet 10.0.0.2/8 scope global eth0 valid_lft forever preferred_lft forever
此時兩個網卡都是down狀態,解決方法以下
[root@host1 ~]# ip link set veth1.1 up [root@host1 ~]# ip netns exec r1 ip link set eth0 up
也能夠將veth1.1放入另外一個名稱空間,這樣兩個名稱空間就能夠通訊了
[root@host1 ~]# ip link set veth1.1 netns r2 [root@host1 ~]# ip netns exec r2 ip link set veth1.1 up