一、ip ss指令替代 ifconfig route arp netstat
1、ip 指令入门
ip [ OPTIONS ] OBJECT { COMMAND | help }
OBJECT 和 COMMAND可以简写到一个字母
ip help 可以查到OBJECT列表和OPTIONS,简写 ip h
ip <OBJECT> help 查看针对该OBJECT的帮助,比如 ip addr help,简写 ip a h
ip addr 查看网络接口地址,简写 ip a
查看网络接口地址,替代ifconfig:
[root@centos7 ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
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: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 08:00:27:15:35:d2 brd ff:ff:ff:ff:ff:ff
inet 192.168.150.110/24 brd 192.168.150.255 scope global enp0s3
valid_lft forever preferred_lft forever
inet6 fe80::a00:27ff:fe15:35d2/64 scope link
valid_lft forever preferred_lft forever
网络接口统计信息
[root@centos7 ~]# ip -s link
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
RX: bytes packets errors dropped overrun mcast
0 0 0 0 0 0
TX: bytes packets errors dropped carrier collsns
0 0 0 0 0 0
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT qlen 1000
link/ether 08:00:27:15:35:d2 brd ff:ff:ff:ff:ff:ff
RX: bytes packets errors dropped overrun mcast
8135366 131454 0 0 0 456
TX: bytes packets errors dropped carrier collsns
646297 2441 0 0 0 0
2、ip route显示和设定路由
显示路由表
[root@centos7 ~]# ip route
default via 192.168.150.254 dev enp0s3 proto static metric 1024
192.168.150.0/24 dev enp0s3 proto kernel scope link src 192.168.150.110
太难看了,格式化一下(显示的是默认网关和局域网路由,两行的内容没有共通性):
[root@centos7 tmp]# ip route|column -t
default via 192.168.150.254 dev enp0s3 proto static metric 1024
192.168.150.0/24 dev enp0s3 proto kernel scope link src 192.168.150.110
添加静态路由
[root@centos7 ~]# ip route add 10.15.150.0/24 via 192.168.150.253 dev enp0s3
[root@centos7 ~]#
[root@centos7 ~]# ip route|column -t
default via 192.168.150.254 dev enp0s3 proto static metric 1024
10.15.150.0/24 via 192.168.150.253 dev enp0s3 proto static metric 1
192.168.150.0/24 dev enp0s3 proto kernel scope link src 192.168.150.110
[root@centos7 ~]#
[root@centos7 ~]# ping 10.15.150.1
PING 10.15.150.1 (10.15.150.1) 56(84) bytes of data.
64 bytes from 10.15.150.1: icmp_seq=1 ttl=63 time=1.77 ms
64 bytes from 10.15.150.1: icmp_seq=1 ttl=63 time=1.08 ms
64 bytes from 10.15.150.1: icmp_seq=1 ttl=63 time=1.57 ms
删除静态路由只需要把 add 替换成 del,或者更简单的只写目标网络
[root@centos7 ~]# ip route del 10.15.150.0/24
但是,ip route 指令对路由的修改不能保存,重启就没了。
设置永久的静态路由的方法RHEL官网文档讲了几种,试验成功的只有一种:
[root@centos7 ~]#echo "10.15.150.0/24 via 192.168.150.253 dev enp0s3" > /etc/sysconfig/network-scripts/route-enp0s3
重启计算机,或者禁用再启用设备enp0s3才能生效,
注意:/etc/sysconfig/static-routes,/etc/sysconfig/network 配置文件都不好用。
3、用 ip neighbor 代替 arp -n
[root@centos7 ~]# ip nei
192.168.150.254 dev enp0s3 lladdr b8:a3:86:37:bd:f8 STALE
192.168.150.100 dev enp0s3 lladdr 90:b1:1c:94:a1:20 DELAY
192.168.150.253 dev enp0s3 lladdr 00:09:0f:85:86:b9 STALE
4、用ss 代替 netstat
对应netstat -ant
[root@centos7 ~]# ss -ant
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 *:22 *:*
ESTAB 0 0 192.168.150.110:22 192.168.150.100:53233
LISTEN 0 100 ::1:25 :::*
LISTEN 0 128 :::22 :::*
对应netstat -antp
[root@centos7 tmp]# ss -antp
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 100 127.0.0.1:25 *:*
users:(("master",1817,13))
LISTEN 0 128 *:22 *:*
users:(("sshd",1288,3))
ESTAB 0 0 192.168.150.110:22 192.168.150.100:59413
users:(("sshd",2299,3))
LISTEN 0 100 ::1:25 :::*
users:(("master",1817,14))
LISTEN 0 128 :::22 :::*
users:(("sshd",1288,4))
[root@centos7 tmp]#
看着真的很别扭,不管多宽的终端屏,users:部分都会折到下一行,其实是在一行的。
格式化一下,内容整齐了,但是标题行串了:
[root@centos7 tmp]# ss -antp|column -t
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 100 127.0.0.1:25 *:* users:(("master",1817,13))
LISTEN 0 128 *:22 *:* users:(("sshd",1288,3))
ESTAB 0 0 192.168.150.110:22 192.168.150.100:59413 users:(("sshd",2299,3))
LISTEN 0 100 ::1:25 :::* users:(("master",1817,14))
LISTEN 0 128 :::22 :::* users:(("sshd",1288,4))
5、旧的network脚本和ifcfg文件
Centos7 开始,网络由 NetworkManager 服务负责管理,相对于旧的 /etc/init.d/network 脚本,NetworkManager是动态的、事件驱动的网络管理服务。旧的 /etc/init.d/network 以及 ifup,ifdown 等依然存在,但是处于备用状态,即:NetworkManager运行时,多数情况下这些脚本会调用NetworkManager去完成网络配置任务;NetworkManager么有运行时,这些脚本就按照老传统管理网络。
[root@centos7 ~]# /etc/init.d/network start
Starting network (via systemctl): [ OK ]
注意(via systemctl)。
6、网络配置文件:
/etc/sysconfig/network 说是全局设置,默认里面啥也没有
/etc/hostname 用nmtui修改hostname后,主机名保存在这里
/etc/resolv.conf 保存DNS设置,不需要手工改,nmtui里面设置的DNS会出现在这里
/etc/sysconfig/network-scripts/ 连接配置信息 ifcfg 文件
/etc/NetworkManager/system-connections/ VPN、移动宽带、PPPoE连接
本文链接:https://my.lmcjl.com/post/2564.html
4 评论