netstat 用于显示与 IP TCP UDP ICMP 协议相关的统计信息, 一般用于检验本机各端口的网络连接情况。
使用 netstat 时若不带参数, 可显示活动 TCP 连接。
Voir Aussi: arp ifconfig nginx
netstat [-vWeenNcCF] [<Af>] -r
netstat {-V|--version|-h|--help}
netstat [-vWnNcaeol] [ <Socket>...]
netstat { [-vWeenNac] -i | [-cWnNe] -M | -s }
<Socket> = {-t|--tcp} {-u|--udp} {-w|--raw} {-x|--unix} --ax25 --ipx --netrom
<AF>=Use '-6|-4' 或 '-A <af>' 或 '--<af>'
可能 (支持路由) 的地址族
| 地址族 | Explication | Remarque |
|---|---|---|
| ax25 | AMPR AX.25 | |
| ddp | Appletalk DDP | |
| inet | DARPA Internet | 默认 |
| SCTP | 仅 Ubuntu 20.04 桌面版 | |
| inet6 | IPv6 | |
| ipx | Novell IPX | |
| netrom | AMPR NET/ROM | |
| rose | 仅 Ubuntu 20.04 桌面版 | |
| x25 | CCITT X.25 |
常用参数
| 参数 | EN 解释 | 中文翻译 | Remarque |
|---|---|---|---|
| -r, --route | display routing table | 显示路由表 | |
| -i, --interfaces | display interface table | 显示接口表 | |
| -g, --groups | display multicast group memberships | 显示多点播送组成员 | |
| -s, --statistics | display networking statistics (like SNMP) | 显示网络统计信息 (像 SNMP) | |
| -M, --masquerade | display masqueraded connections | 显示伪装连接 | |
| -v, --verbose | be verbose | 冗余 | |
| -W, --wide | don't truncate IP addresses | 不截断 IP 地址 | |
| -n, --numeric | don't resolve names | 不解析名称 | |
| --numeric-hosts | don't resolve host names | 不解析主机名称 | |
| --numeric-ports | don't resolve port names | 不解析端口名称 | |
| --numeric-users | don't resolve user names | 不解析用户名 | |
| -N, --symbolic | resolve hardware names | 解析硬件名称 | |
| -e, --extend | display other/more information | 显示其它/更多信息 | |
| -p, --programs | display PID/Program name for sockets | 显示套接字 PID/程序名称 | |
| -c, --continuous | continuous listing | 连续列表 | |
| -l, --listening | display listening server sockets | 显示监听服务器套接字 | |
| -a, --all, --listening | display all sockets (default: connected) | 显示所有套接字 (默认:连接) | |
| -o, --timers | display timers | 显示计时器 | |
| -F, --fib | display Forwarding Information Base (default) | 显示转发信息库 (默认) | |
| -C, --cache | display routing cache instead of FIB | 显示路由缓存而不是 FIB |
| Fonction | Commande | 文字解释 | 示例 | 示例解释 |
|---|---|---|---|---|
| 统计 | netstat --verbose | 显示详细统计信息及不支持地址族 | ||
| netstat -c | 每隔一秒输出网络信息 | |||
| netstat -s | 显示所有端口的统计信息 | |||
| netstat -st | 显示 TCP 端口的统计信息 | |||
| netstat -su | 显示 UDP 端口的统计信息 | |||
| 进程 | netstat -p | 显示 PID/进程名称 | ||
| 端口 | netstat -a | 列出所有端口 | ||
| netstat -at | 列出所有 TCP 端口 | |||
| netstat -au | 列出所有 UDP 端口 | |||
| netstat -ap | grep ssh |
找出 SSH 运行端口。 并不能找到所有进程,没有权限的不会显示,使用 root 权限可查看所有信息。 |
|||
| netstat -an | grep ':80' | 找出指定端口运行进程 | |||
| 监听 | netstat -l | 显示监听端口 | ||
| netstat -lt | 列出所有监听 TCP 端口 | |||
| netstat -lu | 列出所有监听 UDP 端口 | |||
| netstat -lx | 列出所有监听 UNIX 端口 | |||
| 数字 | netstat -n | 以数字代替显示主机 端口 用户名 | ||
| netstat -an | 以数字代替显示所有主机 端口 用户名 | |||
| netsat -a --numeric-ports | 以数字代替显示所有主机 用户名,不显示端口 | |||
| netsat -a --numeric-hosts | 以数字代替显示所有端口 用户名,不显示主机 | |||
| netsat -a --numeric-users | 以数字代替显示所有主机 端口,不显示用户名 | |||
| 路由 | netstat -r | 显示核心路由信息 | ||
| netstat -rn | 以数字格式显示,不显示主机名 | |||
| 接口 | netstat -i | 显示网络接口 | ||
| netstat -ie | 显示更多网络接口信息 | |||
| 其它 | netstat -nat |awk '{print $6}' | 显示 TCP 的各种状态列表 | ||
| netstat -nat | grep "192.168.1.15:22" |awk '{print $5}'|awk -F: '{print $1}'|sort|uniq -c|sort -nr|head -20 | 查看连接某服务端口的最多 IP 地址 | |||
|
netstat -nat |awk '{print $6}'|sort|uniq -c netstat -nat |awk '{print $6}'|sort|uniq -c|sort -rn |
先把状态全取出来,再用 uniq -c 统计,最后再进行排序。 | |||
| awk '{print $1}' access.log |sort|uniq -c|sort -nr|head -10 | 分析 access.log 获得访问前 10 位的 IP 地址 | |||
| 帮助 | netstat -h | 展示帮助信息 | ||
| netstat --help | ||||
| Version | netstat -V | 展示版本信息 | ||
| netstat --version |
以数字方式,查看所有 TCP 监听端口进程
Root:/# netstat -tlnp Active Internet Connections (Only Servers) Proto Recv-Q Send-Q Local Address Foreign Address State Pid/Program Name Tcp 0 0 0.0.0.0:80 0.0.0.0:* Listen 3722/Nginx -G Daemo Tcp 0 0 0.0.0.0:21 0.0.0.0:* Listen 743/Vsftpd Tcp 0 0 0.0.0.0:22 0.0.0.0:* Listen 921/Sshd Tcp 0 0 17.31.73.14:1080 0.0.0.0:* Listen 573/Danted Tcp6 0 0 :::3306 :::* Listen 764/Mysqld Tcp6 0 0 :::80 :::* Listen 3722/Nginx -G Daemo Root:/#
以数字方式,查看所有 TCP UDP 监听端口 nginx 进程
root:~# netstat -tulnp | grep nginx tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 3722/nginx -g daemo tcp6 0 0 :::80 :::* LISTEN 3722/nginx -g daemo root:~#
以数字方式,查看所有 TCP 1080 监听端口进程
root:/# netstat -anp | grep 1080 tcp 0 0 17.31.73.14:1080 0.0.0.0:* LISTEN 573/danted root:/#
查看所有 TCP UDP 监听端口
root:/# netstat -tuln Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:21 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN tcp 0 0 17.31.73.14:1080 0.0.0.0:* LISTEN tcp6 0 0 :::3306 :::* LISTEN tcp6 0 0 :::80 :::* LISTEN udp 0 0 0.0.0.0:68 0.0.0.0:* udp 0 0 17.31.73.14:123 0.0.0.0:* udp 0 0 127.0.0.1:123 0.0.0.0:* udp 0 0 0.0.0.0:123 0.0.0.0:* udp6 0 0 :::123 :::* root:/#
版权声明: 本文为独家原创稿件,版权归 Logiciel Numérique Heureux ,未经许可不得转载。