linux命令netstat的几个用法


Netstat 简介
Netstat 是linux下的一款命令行工具,可用于列出系统上所有的网络套接字连接情况,包括 tcp, udp 以及 unix 套接字,还能列出处于监听状态(即等待接入请求)的套接字。如果你想查看系统某一个端口是否被占用,就可以用netstat
命令。另外netstat
还可以列出本机路由信息和网络接口信息等。
# netstat -h usage: netstat [-vWeenNcCF] [<Af>] -r netstat {-V|--version|-h|--help} netstat [-vWnNcaeol] [<Socket> ...] netstat { [-vWeenNac] -I[<Iface>] | [-veenNac] -i | [-cnNe] -M | -s [-6tuw] } [delay] -r, --route display routing table -I, --interfaces=<Iface> display interface table for <Iface> -i, --interfaces display interface table -g, --groups display multicast group memberships -s, --statistics display networking statistics (like SNMP) -M, --masquerade display masqueraded connections -v, --verbose be verbose -W, --wide don't truncate IP addresses -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 -o, --timers display timers -c, --continuous continuous listing -l, --listening display listening server sockets -a, --all display all sockets (default: connected) -F, --fib display Forwarding Information Base (default) -C, --cache display routing cache instead of FIB -Z, --context display SELinux security context for sockets <Socket>={-t|--tcp} {-u|--udp} {-U|--udplite} {-S|--sctp} {-w|--raw} {-x|--unix} --ax25 --ipx --netrom <AF>=Use '-6|-4' or '-A <af>' or '--<af>'; default: inet List of possible address families (which support routing): inet (DARPA Internet) inet6 (IPv6) ax25 (AMPR AX.25) netrom (AMPR NET/ROM) ipx (Novell IPX) ddp (Appletalk DDP) x25 (CCITT X.25)
1. 列出网络连接
# netstat -antuple
由于netstat命令参数比较多,查看网络连接相关,常用的就是上面这些, 这里为了方便记忆组合了下:an tuple, 两个简单的英文单词~
参数说明:
-a: –all, 显示所有连接的socket (默认: connected)
-n: –numeric, 不解析名称,包括主机名,端口名和用户名
-t: –tcp, 只显示tcp连接
-u: –udp, 只显示udp连接
-p: –programs, 显示连接进程的PID或进程名
-l: –listening, 显示正在监听的socket
-e: –extend, 显示更多信息,包括连接进程的用户名等
注意:
将 -n 和 -e 选项一起使用,会列出用户的 ID 号,而不是用户名。
使用 -p 选项时,netstat 必须运行在 root 权限之下,不然它就不能得到运行在 root 权限下的进程名,而很多服务包括 http 和 ftp 都运行在 root 权限之下。
2. 显示内核路由信息
# netstat -rn
使用 -r 选项打印内核路由信息。打印出来的信息与 route 命令输出的信息一样。也可以使用 -n 选项禁止名称解析。
参数说明:
-r: –route, 显示内核路由信息
-n: 与上面相同
3. 显示网络接口信息
# netstat -ie
使用 -i 选项可以输出网络接口信息, 但是输出的信息比较原始。搭配 -e 选项,可以输出用户友好的信息,输出结果与 ifconfig 一样。
参数说明:
-i: –interfaces, 显示网络接口信息
-e: 与上面相同
4. 其他选项
-s: –statistics, 显示统计数据
-g: –groups, 显示多播,组播信息
-c: –continuous, 持续打印信息
5. 用法举例
5.1 打印 active 状态的连接
active 状态的套接字连接用 "ESTABLISHED" 字段表示,所以我们可以使用 grep 命令获得 active 状态的连接:
# netstat -atnp | grep ESTA(Not all processes could be identified, non-owned process info will not be shown, you would have to be root to see it all.)tcp 0 0 192.168.1.2:49156 173.255.230.5:80 ESTABLISHED 1691/chrome tcp 0 0 192.168.1.2:33324 173.194.36.117:443 ESTABLISHED 1691/chrome
配合 watch 命令监视 active 状态的连接:
$ watch -d -n0 "netstat -atnp | grep ESTA"
5.2 查看服务是否在运行
比如查看mysql服务:
# netstat -anple | grep mysql tcp6 0 0 :::3306 :::* LISTEN 122 26837 1041/mysqld unix 2 [ ACC ] STREAM LISTENING 26838 1041/mysqld /var/run/mysqld/mysqld.sock unix 3 [ ] STREAM CONNECTED 26817 1041/mysqld
