ip
ip命令可以取代ifconfig命令,用于查看或修改网络信息,值得注意的是,它所设置的IP是直接修改内核的IP参数,不会修改配置文件,也就是说,等内核重新启动或重读配置文件之后,该IP将会失效。
语法:
ip [ OPTIONS ] OBJECT { COMMAND | help }
ip参数与对象详细解析;
参数选项 | 解释 |
---|---|
-s | 输出更详细的信息 |
-r | 显示主机时,不使用IP地址,而是使用主机的域名 |
object | 指定要管理的object(网络对象) object如下: link 网络设备 address 设置的IP地址 addrlabel 协议地址标签 管理 neighbor arp或ndisc缓存表 route 路由表 rule 策略路由 maddress 多播地址 tunnel IP隧道 mroute 多播路由表 xfrm IPsec协议框架 |
help | 查看帮助 |
1.ip link 查看或修改网络设备属性
[root@test2 ~]# ip link show #<---查看所有网络设备属性
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
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT qlen 1000
link/ether 00:0c:29:cf:51:02 brd ff:ff:ff:ff:ff:ff
3: ens37: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT qlen 1000
link/ether 00:0c:29:cf:51:0c brd ff:ff:ff:ff:ff:ff
如果想单独查看或修改一个网络设备,在参数后面加上指定的网络设备。
[root@test2 ~]# ip link show dev ens33
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT qlen 1000
link/ether 00:0c:29:cf:51:02 brd ff:ff:ff:ff:ff:ff
关闭和激活某一网络设备
[root@test2 ~]# ip link set ens37 down #--<关闭设备
[root@test2 ~]# ip link show ens37 #--<查看设备
3: ens37: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast state DOWN mode DEFAULT qlen 1000
link/ether 00:0c:29:cf:51:0c brd ff:ff:ff:ff:ff:ff
[root@test2 ~]# ip link show ens37 #<--激活网卡
3: ens37: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT qlen 1000
link/ether 00:0c:29:cf:51:0c brd ff:ff:ff:ff:ff:ff
ip address 管理和查看IP地址
常用例子:
- ip addr add IFADDR dev IFACE //给指定的设备添加IP
- ip addr delete IFADDR dev IFACE //删除指定的IP地址
- ip addr list [IFACE] //显示接口的地址
查看网络设备的地址
[root@test2 ~]# ip a #<--可以简写,等同于 ip address
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: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:cf:51:02 brd ff:ff:ff:ff:ff:ff
inet 192.168.30.132/24 brd 192.168.30.255 scope global dynamic ens33
valid_lft 1384sec preferred_lft 1384sec
inet6 fe80::170f:2d5:64f:bbf5/64 scope link
valid_lft forever preferred_lft forever
3: ens37: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:cf:51:0c brd ff:ff:ff:ff:ff:ff
inet 192.168.30.133/24 brd 192.168.30.255 scope global dynamic ens37
valid_lft 1615sec preferred_lft 1615sec
inet6 fe80::a1dd:b4f6:15a4:4d1c/64 scope link
valid_lft forever preferred_lft forever
给设备添加一个IP地址
[root@test2 ~]# ip addr show dev ens37
3: ens37: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:cf:51:0c brd ff:ff:ff:ff:ff:ff
inet 192.168.30.133/24 brd 192.168.30.255 scope global dynamic ens37
valid_lft 1327sec preferred_lft 1327sec
inet6 fe80::a1dd:b4f6:15a4:4d1c/64 scope link
valid_lft forever preferred_lft forever
[root@test2 ~]# ip addr add 172.16.1.1/24 dev ens37 #<---添加IP
[root@test2 ~]# ip addr show ens37
3: ens37: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:cf:51:0c brd ff:ff:ff:ff:ff:ff
inet 192.168.30.133/24 brd 192.168.30.255 scope global dynamic ens37
valid_lft 1222sec preferred_lft 1222sec
inet 172.16.1.1/24 scope global ens37
valid_lft forever preferred_lft forever
inet6 fe80::a1dd:b4f6:15a4:4d1c/64 scope link
valid_lft forever preferred_lft forever
删除网络设备的IP
[root@test2 ~]# ip addr del 172.16.1.1/24 dev ens37 #<--删除成功
[root@test2 ~]# ip addr show ens37
3: ens37: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:cf:51:0c brd ff:ff:ff:ff:ff:ff
inet 192.168.30.133/24 brd 192.168.30.255 scope global dynamic ens37
valid_lft 1075sec preferred_lft 1075sec
inet6 fe80::a1dd:b4f6:15a4:4d1c/64 scope link
valid_lft forever preferred_lft forever
添加一个别名IP
[root@test2 ~]# ip add add 172.17.4.1/24 dev ens37 label ens37:1 #<--添加别名IP,使用ifconfig也能显示
[root@test2 ~]# ip add show ens37
3: ens37: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:cf:51:0c brd ff:ff:ff:ff:ff:ff
inet 172.17.1.1/24 scope global ens37
valid_lft forever preferred_lft forever
inet 172.17.2.1/24 scope global ens37
valid_lft forever preferred_lft forever
inet 172.17.3.1/24 scope global ens37
valid_lft forever preferred_lft forever
inet 172.17.4.1/24 scope global ens37:1
valid_lft forever preferred_lft forever
inet6 fe80::a1dd:b4f6:15a4:4d1c/64 scope link
valid_lft forever preferred_lft forever
如果不使用添加别名IP的话,使用ifconfig是不能查看添加后的IP的,默认只会显示第一个。
[root@test2 ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.30.132 netmask 255.255.255.0 broadcast 192.168.30.255
inet6 fe80::170f:2d5:64f:bbf5 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:cf:51:02 txqueuelen 1000 (Ethernet)
RX packets 17802 bytes 13362440 (12.7 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 7633 bytes 714498 (697.7 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ens37: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.17.1.1 netmask 255.255.255.0 broadcast 0.0.0.0
inet6 fe80::a1dd:b4f6:15a4:4d1c prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:cf:51:0c txqueuelen 1000 (Ethernet)
RX packets 3612 bytes 241702 (236.0 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 582 bytes 75038 (73.2 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ens37:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 #<--使用别名IP后能使用ifconfig来显示
inet 172.17.4.1 netmask 255.255.255.0 broadcast 0.0.0.0
ether 00:0c:29:cf:51:0c txqueuelen 1000 (Ethernet)
ip route 查看或修改路由表
常用例子:
- ip route show //查看路由表信息
- ip route add //新增路由信息
- ip route change //修改路由信息
实例:
#查看路由表:
[root@test2 ~]# ip route show
default via 192.168.30.2 dev ens37 proto static metric 100
default via 192.168.30.2 dev ens33 proto static metric 101
192.168.30.0/24 dev ens37 proto kernel scope link src 192.168.30.134 metric 100
192.168.30.0/24 dev ens33 proto kernel scope link src 192.168.30.132 metric 101
#新增一条静态路由,目的地址192.168.40.0,下一跳192.168.30.1。
[root@test2 ~]# ip route add 192.168.40.0/24 via 192.168.30.1 dev ens33
[root@test2 ~]# ip route show
default via 192.168.30.2 dev ens37 proto static metric 100
default via 192.168.30.2 dev ens33 proto static metric 101
192.168.30.0/24 dev ens37 proto kernel scope link src 192.168.30.134 metric 100
192.168.30.0/24 dev ens33 proto kernel scope link src 192.168.30.132 metric 101
192.168.40.0/24 via 192.168.30.1 dev ens33
#删除路由表
[root@test2 ~]# ip route del 192.168.40.0/24
ss 查看网络状态
ss命令是类似netstat并取代netstat的工具,它能用来查看网络状态的信息,包括tcp,udp连接、端口等,在查看众多的网络状态时,比netstat工具更为迅速。
语法: ss [options] [ FILTER ]
-选项:
-t:TCP协议的相关连接
-u:UDP相关的连接
-w:raw socket相关的连接
-l:监听状态的连接
-a:所有状态的连接
-n:数字格式
-p:相关的程序及其PID
-e:扩展格式信息
-m:内存用量
-o:计时器信息
-s:显示socket使用统计
使用范例:
#显示所有socket连接
[root@mail ~]# ss -an
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 :::8889 :::*
LISTEN 0 128 *:8889 *:*
LISTEN 0 128 127.0.0.1:6010 *:*
LISTEN 0 128 ::1:6010 :::*
LISTEN 0 128 :::10050 :::*
LISTEN 0 128 *:10050 *:*
#显示所有正在监听的tcp连接
[root@mail ~]# ss -tuln
Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port
tcp LISTEN 0 128 :::8889 :::*
tcp LISTEN 0 128 *:8889 *:*
tcp LISTEN 0 128 127.0.0.1:6010 *:*
#配合过滤公式查看源端口8889的established连接
[root@mail ~]# ss -tan state ESTABLISHED '( sport = :8889 )'
Recv-Q Send-Q Local Address:Port Peer Address:Port
0 48 183.232.203.3:8889 120.237.101.164:50012
0 0 183.232.203.3:8889 120.237.101.164:50014
网友评论