本文章內容:
1. ifconfig家族
1.1 ifconfig
1.2 route
1.3 netstat
2. iproute家族
2.1 ip
2.2 ss
3. nm家族
環境:
[root@localhost ~]# cat /etc/redhat-release
CentOS Linux release 7.8.2003 (Core)
1. ifconfig家族
1.1 ifconfig
1.2 route
1.3 netstat
2. iproute家族
2.1 ip
2.1.1 語法
ip [OPTIONS] OBJECT {COMMAND | help}
OBJECT:link | address | route | netns
2.1.2 ip link - network device
man ip link可以查看幫助。
子命令:show | list | set
● ip link show | list:查看二層設備的相關屬性
● ip link set:改變二層設備的相關屬性
範例:
a. 查看二層設備的相關屬性
[root@localhost ~]# ip link show #可簡寫為ip li sh
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
link/ether 00:0c:29:b7:e8:f8 brd ff:ff:ff:ff:ff:ff
3: ens37: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
link/ether 00:0c:29:b7:e8:02 brd ff:ff:ff:ff:ff:ff
qdisc pfifo_fast:隊列類型為先進先出隊列。
[root@localhost ~]# ip link list #可簡寫為ip li l,只要能唯一識別就能簡寫
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
link/ether 00:0c:29:b7:e8:f8 brd ff:ff:ff:ff:ff:ff
3: ens37: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
link/ether 00:0c:29:b7:e8:02 brd ff:ff:ff:ff:ff:ff
#state UP:當前狀態為UP。
#qlen:支持的隊列長度。
#link/ether:網卡MAC地址。
#brd:網卡MAC的廣播地址。
b. 啟用、停用網卡
[root@localhost ~]# ip link show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
link/ether 00:0c:29:b7:e8:f8 brd ff:ff:ff:ff:ff:ff
3: ens37: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
link/ether 00:0c:29:b7:e8:02 brd ff:ff:ff:ff:ff:ff #UP狀態
[root@localhost ~]# ip link set dev ens37 down #設為DOWN狀態,dev為default可不寫
[root@localhost ~]# ip link show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
link/ether 00:0c:29:b7:e8:f8 brd ff:ff:ff:ff:ff:ff
3: ens37: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast state DOWN mode DEFAULT group default qlen 1000
link/ether 00:0c:29:b7:e8:02 brd ff:ff:ff:ff:ff:ff #DOWN狀態
[root@localhost ~]# ip link set ens37 up #再設為UP狀態
[root@localhost ~]# ip link show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
link/ether 00:0c:29:b7:e8:f8 brd ff:ff:ff:ff:ff:ff
3: ens37: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
link/ether 00:0c:29:b7:e8:02 brd ff:ff:ff:ff:ff:ff #UP狀態
c. 啟用、停用網卡ARP地址解析
[root@localhost ~]# ip link sho
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
link/ether 00:0c:29:b7:e8:f8 brd ff:ff:ff:ff:ff:ff
3: ens37: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
link/ether 00:0c:29:b7:e8:02 brd ff:ff:ff:ff:ff:ff
[root@localhost ~]# ip link set ens37 arp off #關閉ARP地址解析功能
[root@localhost ~]# ip link sho
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
link/ether 00:0c:29:b7:e8:f8 brd ff:ff:ff:ff:ff:ff
3: ens37: <BROADCAST,MULTICAST,NOARP,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
link/ether 00:0c:29:b7:e8:02 brd ff:ff:ff:ff:ff:ff #多了NOARP
[root@localhost ~]# ip link set ens37 arp on #開啟ARP地址解析功能
[root@localhost ~]# ip link sho
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
link/ether 00:0c:29:b7:e8:f8 brd ff:ff:ff:ff:ff:ff
3: ens37: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
link/ether 00:0c:29:b7:e8:02 brd ff:ff:ff:ff:ff:ff
d. 啟用、停用網卡多播功能
[root@localhost ~]# ip link sh
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
link/ether 00:0c:29:b7:e8:f8 brd ff:ff:ff:ff:ff:ff
3: ens37: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
link/ether 00:0c:29:b7:e8:02 brd ff:ff:ff:ff:ff:ff
[root@localhost ~]# ip link set ens37 multicast off #關閉多播功能
[root@localhost ~]# ip link sh
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
link/ether 00:0c:29:b7:e8:f8 brd ff:ff:ff:ff:ff:ff
3: ens37: <BROADCAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
link/ether 00:0c:29:b7:e8:02 brd ff:ff:ff:ff:ff:ff #MULTICAST不見了
[root@localhost ~]# ip link set ens37 multicast on #啟用多播功能
[root@localhost ~]# ip link sh
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
link/ether 00:0c:29:b7:e8:f8 brd ff:ff:ff:ff:ff:ff
3: ens37: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
link/ether 00:0c:29:b7:e8:02 brd ff:ff:ff:ff:ff:ff #MULTICAST出現了
e. 改變網卡名稱
[root@localhost ~]# ip link sh
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
link/ether 00:0c:29:b7:e8:f8 brd ff:ff:ff:ff:ff:ff
3: ens37: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
link/ether 00:0c:29:b7:e8:02 brd ff:ff:ff:ff:ff:ff
[root@localhost ~]# ip link set ens37 down #先停用ens37網卡才能改變其名稱
[root@localhost ~]# ip link set ens37 name eth1 #將ens37網卡改名為eth1
[root@localhost ~]# ip link sh
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
link/ether 00:0c:29:b7:e8:f8 brd ff:ff:ff:ff:ff:ff
3: eth1: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast state DOWN mode DEFAULT group default qlen 1000
link/ether 00:0c:29:b7:e8:02 brd ff:ff:ff:ff:ff:ff #ens37網卡名變為eth1了
[root@localhost ~]# ip link set eth1 name ens37 #再改回ens37
[root@localhost ~]# ip link sh
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
link/ether 00:0c:29:b7:e8:f8 brd ff:ff:ff:ff:ff:ff
3: ens37: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast state DOWN mode DEFAULT group default qlen 1000
link/ether 00:0c:29:b7:e8:02 brd ff:ff:ff:ff:ff:ff
[root@localhost ~]# ip link set ens37 up #啟用ens37網卡
f. 改變MTU值
ip link set DEVICE mtu MTU
2.1.3 ip address - protocol (IP or IPv6) address on a device
man ip address可以查看幫助。
子命令:show | list | add | del | flush
● ip address show | list:查看二、三層設備的相關屬性
● ip address add:添加IP地址
● ip address del:刪除IP地址
● ip address flush:刪除所有IP地址 (primary、secondary、... IP地址)
範例:
[root@localhost ~]# ip a #顯示所有網卡二、三層設備的相關屬性
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:b7:e8:f8 brd ff:ff:ff:ff:ff:ff
inet 192.168.128.178/24 brd 192.168.128.255 scope global noprefixroute dynamic ens33
valid_lft 1298sec preferred_lft 1298sec
inet6 fe80::12da:f368:4b08:ba51/64 scope link noprefixroute
valid_lft forever preferred_lft forever
3: ens37: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:b7:e8:02 brd ff:ff:ff:ff:ff:ff
inet 192.168.169.135/24 brd 192.168.169.255 scope global noprefixroute dynamic ens37
valid_lft 1679sec preferred_lft 1679sec
inet6 fe80::9b35:a29a:bb91:21a2/64 scope link noprefixroute
valid_lft forever preferred_lft forever
[root@localhost ~]# ip address show ens37 #僅顯示ens37網卡設備的二、三層相關屬性
3: ens37: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:b7:e8:02 brd ff:ff:ff:ff:ff:ff
inet 192.168.169.135/24 brd 192.168.169.255 scope global noprefixroute dynamic ens37
valid_lft 1608sec preferred_lft 1608sec
inet6 fe80::9b35:a29a:bb91:21a2/64 scope link noprefixroute
valid_lft forever preferred_lft forever
[root@localhost ~]# ip addr flush dev ens37 #刪除ens37網卡的IP地址,也可以使用ifconfig ens37 0
[root@localhost ~]# ip address show ens37
3: ens37: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:b7:e8:02 brd ff:ff:ff:ff:ff:ff
inet6 fe80::9b35:a29a:bb91:21a2/64 scope link noprefixroute
valid_lft forever preferred_lft forever
[root@localhost ~]# ip addr add 192.168.10.100/24 dev ens37 #添加ens37網卡IP
[root@localhost ~]# ip address show ens37
3: ens37: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:b7:e8:02 brd ff:ff:ff:ff:ff:ff
inet 192.168.10.100/24 scope global ens37
valid_lft forever preferred_lft forever
inet6 fe80::9b35:a29a:bb91:21a2/64 scope link noprefixroute
valid_lft forever preferred_lft forever
[root@localhost ~]# ip addr add 192.168.10.101/24 dev ens37 #添加ens37網卡第二個IP
[root@localhost ~]# ip address show ens37
3: ens37: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:b7:e8:02 brd ff:ff:ff:ff:ff:ff
inet 192.168.10.100/24 scope global ens37
valid_lft forever preferred_lft forever
inet 192.168.10.101/24 scope global secondary ens37
valid_lft forever preferred_lft forever
inet6 fe80::9b35:a29a:bb91:21a2/64 scope link noprefixroute
valid_lft forever preferred_lft forever
[root@localhost ~]# ip addr add 10.0.10.100/8 dev ens37 #添加ens37網卡第三個IP
[root@localhost ~]# ip address show ens37
3: ens37: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:b7:e8:02 brd ff:ff:ff:ff:ff:ff
inet 192.168.10.100/24 scope global ens37
valid_lft forever preferred_lft forever
inet 10.0.10.100/8 scope global ens37 #第三個IP
valid_lft forever preferred_lft forever
inet 192.168.10.101/24 scope global secondary ens37 #同段網路中才會顯示主要、次要
valid_lft forever preferred_lft forever
inet6 fe80::9b35:a29a:bb91:21a2/64 scope link noprefixroute
valid_lft forever preferred_lft forever
2.1.4 ip route
2.1.5 ip netns - manage network namespaces
man ip netns可以查看幫助。
子命令:show | list | add | exec | del
● ip netns show | list:查看當前的網路名稱空間
● ip netns add:添加網路名稱空間
● ip netns exec:在指定的網路名稱空間中執行命令
● ip netns del:刪除網路名稱空間
範例:
[root@localhost ~]# ip netns add mynet #添加一網路名稱空間
[root@localhost ~]# ip netns list
mynet
[root@localhost ~]# ip link set ens37 netns mynet #將ens37網卡移到mynet網路名稱空間中
[root@localhost ~]# ip link sh #ens37網卡已被移到mynet網路名稱空間中,所以不見了
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
link/ether 00:0c:29:b7:e8:f8 brd ff:ff:ff:ff:ff:ff
[root@localhost ~]# ip netns exec mynet ip link sh #在mynet網路名稱空間中執行命令,可以看到ens37網卡在mynet中
1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN mode DEFAULT group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
3: ens37: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000
link/ether 00:0c:29:b7:e8:02 brd ff:ff:ff:ff:ff:ff
[root@localhost ~]# ip netns del mynet #刪除mynet網路名稱空間
[root@localhost ~]# ip link sh #mynet網路名稱空間被刪除後,ens37網卡就回來了
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
link/ether 00:0c:29:b7:e8:f8 brd ff:ff:ff:ff:ff:ff
3: ens37: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
link/ether 00:0c:29:b7:e8:02 brd ff:ff:ff:ff:ff:ff
2.2 ss