前言
linux的ip命令和ifconfig类似,但前者功能更强大,并旨在取代后者。
功能介绍
1、查看ip地址
[root@localhost ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
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: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:d9:45:56 brd ff:ff:ff:ff:ff:ff
inet 192.168.10.220/24 brd 192.168.10.255 scope global dynamic eno16777736 #eno16777736 是网卡名称
valid_lft 6701sec preferred_lft 6701sec
inet6 fe80::20c:29ff:fed9:4556/64 scope link
valid_lft forever preferred_lft forever
2、设置和删除Ip地址
2.1、设置一个IP地址为 192.168.10.111
[root@localhost ~]# sudo ip addr add 192.168.10.111/24 dev eno16777736
注:IP地址要有一个后缀,比如/24。这种用法用于在无类域内路由选择(CIDR)中来显示所用的子网掩码。在这个例子中,子网掩码是255.255.255.0
2.2、查看地址是否生效
[root@localhost ~]# ip addr show eno16777736
2: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:d9:45:56 brd ff:ff:ff:ff:ff:ff
inet 192.168.10.220/24 brd 192.168.10.255 scope global dynamic eno16777736
valid_lft 5359sec preferred_lft 5359sec
inet 192.168.10.111/24 scope global secondary eno16777736 #可以看到设置的地址生效了
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fed9:4556/64 scope link
valid_lft forever preferred_lft forever
2.3、删除Ip地址
[root@localhost ~]# sudo ip addr del 192.168.10.111/24 dev eno16777736
[root@localhost ~]# ip addr show eno16777736
2: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:d9:45:56 brd ff:ff:ff:ff:ff:ff
inet 192.168.10.220/24 brd 192.168.10.255 scope global dynamic eno16777736
valid_lft 4998sec preferred_lft 4998sec
inet6 fe80::20c:29ff:fed9:4556/64 scope link
valid_lft forever preferred_lft forever
3、路由
3.1、列出路由表条目
[root@localhost ~]# ip route show
default via 192.168.10.1 dev eno16777736 proto static metric 100 #默认的路由条目,你可以随意改动它。
192.168.10.0/24 dev eno16777736 proto kernel scope link src 192.168.10.220 metric 100
3.2、查看ip对应的路由包
[root@localhost ~]# ip route get 192.168.10.220
local 192.168.10.220 dev lo src 192.168.10.220
cache <local>
3.3、更改默认路由
[root@localhost ~]# sudo ip route add default via 192.168.10.3
[root@localhost ~]# ip route show
default via 192.168.10.3 dev eno16777736
default via 192.168.10.1 dev eno16777736 proto static metric 100
192.168.10.0/24 dev eno16777736 proto kernel scope link src 192.168.10.220 metric 100
4、显示网络统计数据
4.1、显示不同网络接口的统计数据
[root@localhost ~]# ip -s link
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT qlen 1
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
RX: bytes packets errors dropped overrun mcast
37238 652 0 0 0 0
TX: bytes packets errors dropped carrier collsns
37238 652 0 0 0 0
2: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT qlen 1000
link/ether 00:0c:29:d9:45:56 brd ff:ff:ff:ff:ff:ff
RX: bytes packets errors dropped overrun mcast
1418487 15704 0 0 0 0
TX: bytes packets errors dropped carrier collsns
87284 741 0 0 0 0
4.2、获取一个特定网络接口的信息(在排除网络连接故障时,这会非常有用)
[root@localhost ~]# ip -s -s link ls eno16777736
2: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT qlen 1000
link/ether 00:0c:29:d9:45:56 brd ff:ff:ff:ff:ff:ff
RX: bytes packets errors dropped overrun mcast
1511519 16680 0 0 0 0
RX errors: length crc frame fifo missed
0 0 0 0 0
TX: bytes packets errors dropped carrier collsns
92184 784 0 0 0 0
TX errors: aborted fifo window heartbeat transns
0 0 0 0 2
5、ARP条目 [root@localhost ~]# ip neighbour
192.168.10.192 dev eno16777736 lladdr f4:f5:db:cb:45:9f STALE
192.168.10.1 dev eno16777736 lladdr 34:96:72:91:90:76 STALE
192.168.10.124 dev eno16777736 lladdr 98:29:a6:06:22:58 STALE
192.168.10.68 dev eno16777736 lladdr 00:e0:70:65:61:8a DELAY
192.168.10.140 dev eno16777736 lladdr 00:ec:0a:dd:e7:a1 STALE
[root@localhost ~]# ip neighbour
192.168.10.192 dev eno16777736 lladdr f4:f5:db:cb:45:9f STALE
192.168.10.1 dev eno16777736 lladdr 34:96:72:91:90:76 STALE
192.168.10.124 dev eno16777736 lladdr 98:29:a6:06:22:58 STALE
192.168.10.68 dev eno16777736 lladdr 00:e0:70:65:61:8a DELAY
192.168.10.140 dev eno16777736 lladdr 00:ec:0a:dd:e7:a1 STALE
注:ARP(地址解析协议)用于将一个IP地址转换成它对应的物理地址,也就是通常所说的MAC地址
6、监控netlink消息 [root@localhost ~]# ip monitor all
[nsid current]192.168.10.68 dev eno16777736 lladdr 00:e0:70:65:61:8a STALE
[nsid current]192.168.10.68 dev eno16777736 lladdr 00:e0:70:65:61:8a STALE
[nsid current]192.168.10.68 dev eno16777736 lladdr 00:e0:70:65:61:8a REACHABLE
[nsid current]192.168.10.68 dev eno16777736 lladdr 00:e0:70:65:61:8a STALE
[nsid current]192.168.10.68 dev eno16777736 lladdr 00:e0:70:65:61:8a REACHABLE
[nsid current]192.168.10.68 dev eno16777736 lladdr 00:e0:70:65:61:8a STALE
[nsid current]192.168.10.68 dev eno16777736 lladdr 00:e0:70:65:61:8a REACHABLE
[root@localhost ~]# ip monitor all
[nsid current]192.168.10.68 dev eno16777736 lladdr 00:e0:70:65:61:8a STALE
[nsid current]192.168.10.68 dev eno16777736 lladdr 00:e0:70:65:61:8a STALE
[nsid current]192.168.10.68 dev eno16777736 lladdr 00:e0:70:65:61:8a REACHABLE
[nsid current]192.168.10.68 dev eno16777736 lladdr 00:e0:70:65:61:8a STALE
[nsid current]192.168.10.68 dev eno16777736 lladdr 00:e0:70:65:61:8a REACHABLE
[nsid current]192.168.10.68 dev eno16777736 lladdr 00:e0:70:65:61:8a STALE
[nsid current]192.168.10.68 dev eno16777736 lladdr 00:e0:70:65:61:8a REACHABLE
7、激活和停止网络接口 [root@localhost ~]# ip route show
default via 192.168.10.1 dev eno16777736 proto static metric 100
192.168.10.0/24 dev eno16777736 proto kernel scope link src 192.168.10.220 metric 100
[root@localhost ~]# sudo ip link set eno16777736 down
[root@localhost ~]# ip route show
You have new mail in /var/spool/mail/root
[root@localhost ~]# sudo ip link set eno16777736 up
[root@localhost ~]# ip route show
default via 192.168.10.1 dev eno16777736 proto static metric 100
192.168.10.0/24 dev eno16777736 proto kernel scope link src 192.168.10.220 metric 100
[root@localhost ~]# ip route show
default via 192.168.10.1 dev eno16777736 proto static metric 100
192.168.10.0/24 dev eno16777736 proto kernel scope link src 192.168.10.220 metric 100
[root@localhost ~]# sudo ip link set eno16777736 down
[root@localhost ~]# ip route show
You have new mail in /var/spool/mail/root
[root@localhost ~]# sudo ip link set eno16777736 up
[root@localhost ~]# ip route show
default via 192.168.10.1 dev eno16777736 proto static metric 100
192.168.10.0/24 dev eno16777736 proto kernel scope link src 192.168.10.220 metric 100
8、获取帮助 [root@localhost ~]# ip route help
[root@localhost ~]# ip route help
结束语
如果本篇对您有帮助的话,请点赞加关注。