layout: post
title: 网络管理命令
date: 2017-12-16
tags: [“Linux”,”网络相关”]


netstat:

DESCRIPTION
Netstat prints information about the Linux networking subsystem. The type of information printed is controlled by the first argument
This program is obsolete. Replacement for netstat is ss. Replacement for netstat -r is ip route. Replacement for netstat -i is ip -s link. Replacement for netstat -g is ip maddr.(ss比netstat执行速度要快很多,但是看着不太舒服。)

Options:

-t: tcp协议相关
-u: udp协议相关
-w: raw socket相关
-l: 处于监听状态
-a: 所有状态
-n: 以数字显示IP和端口;
-e:扩展格式
-p: 显示相关进程及PID

我喜欢的命令只有一个 netstat -lantp 然后grep取值

显示路由表:

netstat {—route’-r} [—numeric’-n]
-r: 显示内核路由表
-n: 数字格式

netstat -r 这条命令好慢啊,从面上来看只是把0.0.0.0变成了default

netstat -rn 显示路由表很快,但是还是习惯用route -n

显示接口统计数据

netstat -i 显示所有的流量信息

netstat -I显示指定网卡的流量信息(这里有个坑,netstat -I eth0 是错误的,netstat -Ieth0 这个才是对的

例:

  1. [root@joker-7-01 rules.d]# netstat -Iens33
  2. Kernel Interface table
  3. Iface MTU RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TX-OVR Flg
  4. ens33 1500 95576 0 0 0 370 0 0 0 BMRU

RX-OK:成功接收的数据包数量

TX-OK:成功发送的数据包数量

ss 命令

DESCRIPTION
ss is used to dump socket statistics. It allows showing information similar to netstat. It can display more TCP and state informations than
other tools.

netstat通过遍历proc来获取socket信息,ss使用netlink与内核tcp_diag模块通信获取socket信息,ss比netstat速度更快,是netstat的替代品,但是展示效果不太好,个人感觉。

选项(和netstat差不多):

-t: tcp协议相关
-u: udp协议相关
-w: 裸套接字相关
-x:unix sock相关
-l: listen状态的连接
-a: 所有
-n: 数字格式
-p: 相关的程序及PID
-e: 扩展的信息
-m:内存用量
-o:计时器信息

常用命令:

  1. [root@newhostname fd]# ss -lantp
  2. State Recv-Q Send-Q Local Address:Port Peer Address:Port
  3. LISTEN 0 128 *:111 *:* users:(("systemd",pid=1,fd=39))
  4. LISTEN 0 128 *:22 *:* users:(("sshd",pid=1240,fd=3))
  5. LISTEN 0 128 127.0.0.1:631 *:* users:(("cupsd",pid=1234,fd=12))
  6. LISTEN 0 100 127.0.0.1:25 *:* users:(("master",pid=1389,fd=13))
  7. ESTAB 0 52 192.168.253.181:22 192.168.253.1:59589 users:(("sshd",pid=1400,fd=3))
  8. ESTAB 0 0 192.168.253.181:22 192.168.253.1:64463 users:(("sshd",pid=2205,fd=3))
  9. LISTEN 0 128 :::111 :::* users:(("systemd",pid=1,fd=38))
  10. LISTEN 0 32 :::21 :::* users:(("vsftpd",pid=1250,fd=3))
  11. LISTEN 0 128 :::22 :::* users:(("sshd",pid=1240,fd=4))
  12. LISTEN 0 128 ::1:631 :::* users:(("cupsd",pid=1234,fd=11))
  13. LISTEN 0 100 ::1:25 :::* users:(("master",pid=1389,fd=14))
  14. [root@newhostname fd]# ss -s #列出当前socket详细信息
  15. Total: 624 (kernel 1071)
  16. TCP: 11 (estab 2, closed 0, orphaned 0, synrecv 0, timewait 0/0), ports 0
  17.  
  18. Transport Total IP IPv6
  19. * 1071 - -
  20. RAW 2 0 2
  21. UDP 8 5 3
  22. TCP 11 6 5
  23. INET 21 11 10
  24. FRAG 0 0 0
  ## ip 命令 DESCRIPTION ip - show / manipulate routing, devices, policy routing and tunnels Specifies the action to perform on the object. The set of possible actions depends on the object type. As a rule, it is possible to add, delete and show (or list ) objects, but some objects do not allow all of these operations or have some additional commands. The help command is available for all objects. It prints out a list of available commands and argument syntax conventions. If no command is given, some default command is assumed. Usually it is list or, if the objects of this class cannot be listed, help. ip link 数据链路层操作
  1. [root@newhostname ~]# ip link
  2. 1: lo: mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT qlen 1
  3. link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
  4. 2: ens33: mtu 1500 qdisc pfifo_fast state UP mode DEFAULT qlen 1000
  5. link/ether 00:0c:29:ff:3f:ce brd ff:ff:ff:ff:ff:ff
  6. 3: ens37: mtu 1500 qdisc pfifo_fast state UP mode DEFAULT qlen 1000
  7. link/ether 00:0c:29:ff:3f:d8 brd ff:ff:ff:ff:ff:ff

可以看到MAC和广播地址,因为IP地址不是链路层的,所以看不到IP的信息

操作网卡:

ip link set eth0 up/down 开关网卡
ip link set eth0 multicast on/off开关多播

  1. [root@newhostname ~]# ip link set ens33
  2. address allmulticast broadcast dynamic multicast netns trailers up
  3. alias arp down mtu name promisc txqueuelen

以上为 ip link set 可以设置的对象

ip address /ip a 网络层操作 (尽量用 ip address 来操作,ifconfig 是一个过时的命令)

  1. ip a #显示网卡ip地址信息
  2. ip a s eth0 #显示eth0网卡的信息 (可以缩写成 ip a s eth0)
  3. ip a {add'del} 1.1.1.1/8 dev eth0 {label name01} #添加删除ip地址并添加或删除label 标签 (这个命令没有补全啊,装了bash-complete包也无法补全;可以缩写成 ip a a ' ip a d
  4. ip addr flush dev eth0 label eth0:0 #清除协议地址 警告这个命令(和后面讨论的所有flush命令)非常危险。如果出现错误,将无法恢复(flush可以缩写成f),它会清除被操作的地址。
  5. ip addr flush dev eth0 #清除eth0上的所有ip地址和标签(flush可以缩写成f)
  6.  

ip route / ip r 对路由的操作 ip route - routing table management (尽量用 ip route ,route 是一个过时的命令)

  1. ip route {adddel}
    [root@newhostname ~]# ip r #显示路由表
    default via 172.18.0.1 dev ens33 proto static metric 100
    default via 192.168.253.2 dev ens37 proto static metric 101
    172.18.0.0/16 dev ens33 proto kernel scope link src 172.18.7.110
    172.18.0.0/16 dev ens33 proto kernel scope link src 172.18.7.110 metric 100
    192.168.253.0/24 dev ens37 proto kernel scope link src 192.168.253.181 metric 100

  2. ip r a 1.0.0.0/8 via 172.18.0.1 #添加静态路由,via表示通过哪个网关,网关至少和一个网卡的ip地址同网段
    ip r a default via 172.18.0.2 #添加默认路由
    ip r d 1.0.0.0/8 #删除路由
    ip r d default #删除默认路由
    ip route flush #删除路由表
    ip route flush dev eth0 删除eth0上面的路由表


ifconfig 命令用烂了且即将被淘汰的命令,不想写它了,ip是它的替代品

文档更新时间: 2022-06-10 10:57   作者:张尚