作为一个非职业运维,不时需要查看Linux服务器上的网络流量状况。本文介绍几个常用的Linux查看网络流量命令,并简要介绍其用法。部分命令系统已经内置,某些命令则需要从软件仓库或者自行编译安装,本文以CentOS系统为例介绍软件安装方法。
查看总流量
ip
ip
命令来自 iproute2util
包,是查看、配置网络/路由的工具。作为 ifconfig
的替代品,ip
命令功能更强大,用法更语义化。
ip
命令在大多数系统上都已经默认安装,也可通过 yum install -y iproute
安装。ip
的 “-s -h” 参数查看各网卡的总流量,下图是 ip -s -h link
的输出结果:

nload
nload
命令可以查看各个网络设备的当前网络速率,也会展示流经设备的总流量。
nload
来自EPEL软件库,CentOS安装命令为:yum install -y epel-release && yum install -y nload
。查看eth0网卡流量 nload eth0
命令输出如下:

PS:netstat -s
和 ifconfig -s
都可以查看收发的总包(netstat
分协议给出),但不会给出字节数据。
查看实时网速网速
nload
命令可以输出当前网速,上面已经介绍过,本节不再赘述。
dstat
dstat
是一个用来替换vmstat
、iostat
、netstat
、nfsstat
和ifstat
的全能系统信息统计工具,支持数据实时刷新,输出直观易懂。
dstat
默认没有安装,安装命令为:yum install -y dstat
。dstat -tnf 1 10
输出接下来10秒内每秒的网络数据:

sar
sar
是System Activity Report的缩写,是一款全面的Linux系统运行状态统计和性能分析工具,可从磁盘IO、CPU负载、内存使用等多个维度对系统活动进行报告。
sar
命令来自 sysstat
包,可使用这个命令安装:yum install -y sysstat
。sar -n TCP 1 10
可查看接下来10秒内的tcp数据:

查看连接/进程流量
ss/netstat
ss
和 netstat
是查看活动链接/监听端口的常用命令。ss
是 netstat
的替代,性能更好,建议使用。
ss
是 iproute2util
包的一部分,因此在大多数系统上默认安装,也可通过yum install -y iproute
安装。netstat
来自 net-tools
包,新版系统上需要自行安装:yum install -y net-tools
。
下图是用ss
查看tcp连接的输出:

iftop
iftop
是一款实时流量监控工具,可以查看每个连接的实时速率。
iftop
来自EPEL软件库,安装命令是:yum install -y epel-release && yum install -y iftop
。iftop -nN -i eth0
实时查看eth0网卡的各个连接和网速:

nethogs
nethogs
是为了查看单个进程流量而设计的工具,按照进程进行带宽分组。
nethogs
来自EPEL软件库,安装命令是:yum install -y epel-release && yum install -y nethogs
。nethogs -d 2 eth0
每2秒刷新流经eth0网卡的进程流量信息:

其他
tcpdump
可以用来抓包,保存的数据可以用wireshark打开和查看。