netstat工具

在以前的学习工作中,我常常遇到下面的情况:

  • xx启动失败,端口被占用
  • 网络连接数太多
  • 监听的端口没有生效
  • .....

这些和网络相关的问题的排查,linux提供的netstat程序很好的辅助我们排查问题。

  1. 端口查看问题

netstat 命令的用法:

  • netstat [-vWnNcaeol] [ ...] 查看socket连接信息
  • netstat { [-vWeenNac] -i | [-cWnNe] -M | -s }:查看概要信息,例如网络接口、统计信息等

OPIONS:

  • -i 显示接口列表

  • -s 显示网络统计信息,类似SNMP

  • -v --verbose 输出额外的信息

  • -n --numberic 不要解析成名字,显示数字信息,输出IP地址而非主机名

    • --numeric-hosts
    • --numeric--ports
    • --numberic--users
  • -N:解析为名字

  • -e --extend 显示其他信息,显示(userid, inode等信息)

  • -p --programs 显示该socket对应的pid/pname

  • -c --continues 持续列出 ,隔几秒刷新一次

  • -l --listening 显示监听的服务器socket

  • -a --all, --listening 显示所有的socket

  • -o --times 显示timers

查看监听的tcp端口

netstat -tlnp

image.png | left | 683x570

  1. socket连接情况

查看tcp连接情况

netstat -tanp

image.png | left | 607x356

统计本地的tcp端口的连接数:

查看本地的33211端口的连接数:

netstat -tanp | grep 33211 | awk '{print $5}' | sort | uniq | wc -l
posted @ 2018-09-06 12:02  Spground  阅读(751)  评论(0编辑  收藏  举报