Ubuntu netstat 命令详解


netstat 用于显示与 IP TCP UDP ICMP 协议相关的统计信息, 一般用于检验本机各端口的网络连接情况。

使用 netstat 时若不带参数, 可显示活动 TCP 连接。

另请参阅: arp     ifconfig

基本用法     详细用法

netstat -ntpl
netstat -anp | grep 1080
			

基本语法

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>'

可能 (支持路由) 的地址族

地址族 说明 备注
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 解释 中文翻译 备注
-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

基本用法

功能 命令 文字解释 示例 示例解释
帮助 netstat -h 展示帮助信息
netstat --help
统计 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 地址

 

版权声明: 本文为独家原创稿件,版权归 乐数软件 ,未经许可不得转载。