歡迎訪問我的個(gè)人博客網(wǎng)站:http://www.yanmin99.com/
一、netstat簡介
- netstat 命令用于顯示各種網(wǎng)絡(luò)相關(guān)信息,如網(wǎng)絡(luò)連接,路由表,接口狀態(tài) (Interface Statistics),masquerade 連接,多播成員 (Multicast Memberships)等等。
二、netstat使用
- 1、命令的一般格式
netstat [選項(xiàng)]
- 2、參數(shù)介紹
-a (all)顯示所有選項(xiàng),默認(rèn)不顯示LISTEN相關(guān)
-t (tcp)僅顯示tcp相關(guān)選項(xiàng)
-u (udp)僅顯示udp相關(guān)選項(xiàng)
-n 拒絕顯示別名,能顯示數(shù)字的全部轉(zhuǎn)化成數(shù)字。
-l 僅列出有在 Listen (監(jiān)聽) 的服務(wù)狀態(tài)
-p 顯示建立相關(guān)鏈接的程序名
-r 顯示路由信息,路由表
-e 顯示擴(kuò)展信息,例如uid等
-s 按各個(gè)協(xié)議進(jìn)行統(tǒng)計(jì)
-c 每隔一個(gè)固定時(shí)間,執(zhí)行該netstat命令
3、實(shí)例
-
A、列出所有端口
- 列出所有端口
root@iZ235ubl1arZ:/home/yanmin/blog# netstat -a | more Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 *:http *:* LISTEN tcp 0 0 *:ssh *:* LISTEN
- 列出所有tcp端口
root@iZ235ubl1arZ:/home/yanmin/blog# netstat -at Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 *:http *:* LISTEN tcp 0 0 *:ssh *:* LISTEN tcp 0 0 121.41.120.65:http 140.205.201.20:35425 TIME_WAIT
- 列出所有udp端口
root@iZ235ubl1arZ:/home/yanmin/blog# netstat -au Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State udp 0 0 121.41.120.65:ntp *:* udp 0 0 iZ235ubl1arZ:ntp *:* udp 0 0 localhost:ntp *:* udp 0 0 *:ntp *:* udp6 0 0 [::]:ntp [::]:*
-
B、列出所有處于監(jiān)聽狀態(tài)的Sockets
- 只顯示監(jiān)聽端口netstat -l
root@iZ235ubl1arZ:/home/yanmin/blog# netstat -l Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 *:http *:* LISTEN tcp 0 0 *:ssh *:* LISTEN udp 0 0 121.41.120.65:ntp *:* udp 0 0 iZ235ubl1arZ:ntp *:* udp 0 0 localhost:ntp *:*
- 只列出所有監(jiān)聽tcp端口netstat -lt
root@iZ235ubl1arZ:/home/yanmin/blog# netstat -lt Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 *:http *:* LISTEN tcp 0 0 *:ssh *:* LISTEN
- 只列出所有監(jiān)聽udp端口 netstat -lu
root@iZ235ubl1arZ:/home/yanmin/blog# netstat -lu Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State udp 0 0 121.41.120.65:ntp *:* udp 0 0 iZ235ubl1arZ:ntp *:* udp 0 0 localhost:ntp *:* udp 0 0 *:ntp *:* udp6 0 0 [::]:ntp [::]:*
- 只列出所有監(jiān)聽UNIX端口 netstat -lx
root@iZ235ubl1arZ:/home/yanmin/blog# netstat -lx Active UNIX domain sockets (only servers) Proto RefCnt Flags Type State I-Node Path unix 2 [ ACC ] STREAM LISTENING 9506 /var/run/nscd/socket unix 2 [ ACC ] STREAM LISTENING 8267 /var/run/dbus/system_bus_socket unix 2 [ ACC ] SEQPACKET LISTENING 7769 /run/udev/control unix 2 [ ACC ] STREAM LISTENING 7592 @/com/ubuntu/upstart
-
C、在netstat 輸出中顯示PID和進(jìn)程名稱netstat -p
root@iZ235ubl1arZ:/home/yanmin/blog# netstat -p Active Internet connections (w/o servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 316 121.41.120.65:ssh 58.246.62.146:54175 ESTABLISHED 17558/sshd: yanmin tcp 0 0 121.41.120.65:ssh 58.246.62.146:52592 ESTABLISHED 14538/sshd: yanmin Active UNIX domain sockets (w/o servers) Proto RefCnt Flags Type State I-Node PID/Program name Path unix 10 [ ] DGRAM 8286 682/rsyslogd /dev/log unix 3 [ ] STREAM CONNECTED 307127 17558/sshd: yanmin
-
D、顯示核心路由信息 netstat -r
yanmin:blog yanmin$ netstat -r Routing tables Internet: Destination Gateway Flags Refs Use Netif Expire default 10.96.150.1 UGSc 21 0 en0 10.16/14 10.254.254.81 UGSc 0 0 utun1 10.16.0.222/32 10.254.254.81 UGSc 1 0 utun1