通过示例学习使用 netstat

找不到分类 evilven 4个月前 (03-15) 51次浏览 未收录 0个评论 扫描二维码
通过示例学习使用 netstat

netstat 是一个告诉我们系统中所有 tcp/udp/unix socket 连接状态的命令行工具。它会列出所有已经连接或者等待连接状态的连接。 该工具在识别某个应用监听哪个端口时特别有用,我们也能用它来判断某个应用是否正常的在监听某个端口。
— Shusain


本文导航
编译自 | http://linuxtechlab.com/learn-use-netstat-with-examples/ 
 作者 | Shusain
 译者 | lujun9972

netstat 是一个告诉我们系统中所有 tcp/udp/unix socket 连接状态的命令行工具。它会列出所有已经连接或者等待连接状态的连接。 该工具在识别某个应用监听哪个端口时特别有用,我们也能用它来判断某个应用是否正常的在监听某个端口。

netstat 命令还能显示其它各种各样的网络相关信息,例如路由表, 网卡统计信息, 虚假连接以及多播成员等。

本文中,我们会通过几个例子来学习 netstat。

(推荐阅读: 通过示例学习使用 CURL 命令[1] )

1 – 检查所有的连接

使用 a 选项可以列出系统中的所有连接,

<ol class="linenums list-paddingleft-2" style="margin-left: 2em;margin-right: 2em;"><li><p><code style="word-wrap: break-word;background: none;color: rgb(33, 150, 243);line-height: 1.2em;padding-left: 10px !important;border-radius: 0px !important;margin-top: 1em !important;margin-bottom: 1em !important;border-width: initial !important;border-style: none !important;border-color: initial !important;"><span class="pln" style="word-wrap: break-word;color: rgb(184, 255, 184);">$ </span><span class="kwd" style="word-wrap: break-word;color: rgb(226, 137, 100);">netstat</span><span class="pln" style="word-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="pun" style="word-wrap: break-word;color: rgb(184, 255, 184);">-</span><span class="pln" style="word-wrap: break-word;color: rgb(184, 255, 184);">a</span></code></p></li></ol>

这会显示系统所有的 tcp、udp 以及 unix 连接。

2 – 检查所有的 tcp/udp/unix socket 连接

使用 t 选项只列出 tcp 连接,

<ol class="linenums list-paddingleft-2" style="margin-left: 2em;margin-right: 2em;"><li><p><code style="word-wrap: break-word;background: none;color: rgb(33, 150, 243);line-height: 1.2em;padding-left: 10px !important;border-radius: 0px !important;margin-top: 1em !important;margin-bottom: 1em !important;border-width: initial !important;border-style: none !important;border-color: initial !important;"><span class="pln" style="word-wrap: break-word;color: rgb(184, 255, 184);">$ </span><span class="kwd" style="word-wrap: break-word;color: rgb(226, 137, 100);">netstat</span><span class="pln" style="word-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="pun" style="word-wrap: break-word;color: rgb(184, 255, 184);">-</span><span class="pln" style="word-wrap: break-word;color: rgb(184, 255, 184);">at</span></code></p></li></ol>

类似的,使用 u 选项只列出 udp 连接,

<ol class="linenums list-paddingleft-2" style="margin-left: 2em;margin-right: 2em;"><li><p><code style="word-wrap: break-word;background: none;color: rgb(33, 150, 243);line-height: 1.2em;padding-left: 10px !important;border-radius: 0px !important;margin-top: 1em !important;margin-bottom: 1em !important;border-width: initial !important;border-style: none !important;border-color: initial !important;"><span class="pln" style="word-wrap: break-word;color: rgb(184, 255, 184);">$ </span><span class="kwd" style="word-wrap: break-word;color: rgb(226, 137, 100);">netstat</span><span class="pln" style="word-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="pun" style="word-wrap: break-word;color: rgb(184, 255, 184);">-</span><span class="pln" style="word-wrap: break-word;color: rgb(184, 255, 184);">au</span></code></p></li></ol>

使用 x 选项只列出 Unix socket 连接,

<ol class="linenums list-paddingleft-2" style="margin-left: 2em;margin-right: 2em;"><li><p><code style="word-wrap: break-word;background: none;color: rgb(33, 150, 243);line-height: 1.2em;padding-left: 10px !important;border-radius: 0px !important;margin-top: 1em !important;margin-bottom: 1em !important;border-width: initial !important;border-style: none !important;border-color: initial !important;"><span class="pln" style="word-wrap: break-word;color: rgb(184, 255, 184);">$ </span><span class="kwd" style="word-wrap: break-word;color: rgb(226, 137, 100);">netstat</span><span class="pln" style="word-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="pun" style="word-wrap: break-word;color: rgb(184, 255, 184);">-</span><span class="pln" style="word-wrap: break-word;color: rgb(184, 255, 184);">ax</span></code></p></li></ol>

3 – 同时列出进程 ID/进程名称

使用 p 选项可以在列出连接的同时也显示 PID 或者进程名称,而且它还能与其他选项连用,

<ol class="linenums list-paddingleft-2" style="margin-left: 2em;margin-right: 2em;"><li><p><code style="word-wrap: break-word;background: none;color: rgb(33, 150, 243);line-height: 1.2em;padding-left: 10px !important;border-radius: 0px !important;margin-top: 1em !important;margin-bottom: 1em !important;border-width: initial !important;border-style: none !important;border-color: initial !important;"><span class="pln" style="word-wrap: break-word;color: rgb(184, 255, 184);">$ </span><span class="kwd" style="word-wrap: break-word;color: rgb(226, 137, 100);">netstat</span><span class="pln" style="word-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="pun" style="word-wrap: break-word;color: rgb(184, 255, 184);">-</span><span class="pln" style="word-wrap: break-word;color: rgb(184, 255, 184);">ap</span></code></p></li></ol>

4 – 列出端口号而不是服务名

使用 n 选项可以加快输出,它不会执行任何反向查询(LCTT 译注:这里原文有误),而是直接输出数字。 由于无需查询,因此结果输出会快很多。

<ol class="linenums list-paddingleft-2" style="margin-left: 2em;margin-right: 2em;"><li><p><code style="word-wrap: break-word;background: none;color: rgb(33, 150, 243);line-height: 1.2em;padding-left: 10px !important;border-radius: 0px !important;margin-top: 1em !important;margin-bottom: 1em !important;border-width: initial !important;border-style: none !important;border-color: initial !important;"><span class="pln" style="word-wrap: break-word;color: rgb(184, 255, 184);">$ </span><span class="kwd" style="word-wrap: break-word;color: rgb(226, 137, 100);">netstat</span><span class="pln" style="word-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="pun" style="word-wrap: break-word;color: rgb(184, 255, 184);">-</span><span class="pln" style="word-wrap: break-word;color: rgb(184, 255, 184);">an</span></code></p></li></ol>

5 – 只输出监听端口

使用 l 选项只输出监听端口。它不能与 a 选项连用,因为 a 会输出所有端口,

<ol class="linenums list-paddingleft-2" style="margin-left: 2em;margin-right: 2em;"><li><p><code style="word-wrap: break-word;background: none;color: rgb(33, 150, 243);line-height: 1.2em;padding-left: 10px !important;border-radius: 0px !important;margin-top: 1em !important;margin-bottom: 1em !important;border-width: initial !important;border-style: none !important;border-color: initial !important;"><span class="pln" style="word-wrap: break-word;color: rgb(184, 255, 184);">$ </span><span class="kwd" style="word-wrap: break-word;color: rgb(226, 137, 100);">netstat</span><span class="pln" style="word-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="pun" style="word-wrap: break-word;color: rgb(184, 255, 184);">-</span><span class="pln" style="word-wrap: break-word;color: rgb(184, 255, 184);">l</span></code></p></li></ol>

6 – 输出网络状态

使用 s 选项输出每个协议的统计信息,包括接收/发送的包数量,

<ol class="linenums list-paddingleft-2" style="margin-left: 2em;margin-right: 2em;"><li><p><code style="word-wrap: break-word;background: none;color: rgb(33, 150, 243);line-height: 1.2em;padding-left: 10px !important;border-radius: 0px !important;margin-top: 1em !important;margin-bottom: 1em !important;border-width: initial !important;border-style: none !important;border-color: initial !important;"><span class="pln" style="word-wrap: break-word;color: rgb(184, 255, 184);">$ </span><span class="kwd" style="word-wrap: break-word;color: rgb(226, 137, 100);">netstat</span><span class="pln" style="word-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="pun" style="word-wrap: break-word;color: rgb(184, 255, 184);">-</span><span class="pln" style="word-wrap: break-word;color: rgb(184, 255, 184);">s</span></code></p></li></ol>

7 – 输出网卡状态

使用 I 选项只显示网卡的统计信息,

<ol class="linenums list-paddingleft-2" style="margin-left: 2em;margin-right: 2em;"><li><p><code style="word-wrap: break-word;background: none;color: rgb(33, 150, 243);line-height: 1.2em;padding-left: 10px !important;border-radius: 0px !important;margin-top: 1em !important;margin-bottom: 1em !important;border-width: initial !important;border-style: none !important;border-color: initial !important;"><span class="pln" style="word-wrap: break-word;color: rgb(184, 255, 184);">$ </span><span class="kwd" style="word-wrap: break-word;color: rgb(226, 137, 100);">netstat</span><span class="pln" style="word-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="pun" style="word-wrap: break-word;color: rgb(184, 255, 184);">-</span><span class="pln" style="word-wrap: break-word;color: rgb(184, 255, 184);">i</span></code></p></li></ol>

8 – 显示多播组multicast group信息

使用 g 选项输出 IPV4 以及 IPV6 的多播组信息,

<ol class="linenums list-paddingleft-2" style="margin-left: 2em;margin-right: 2em;"><li><p><code style="word-wrap: break-word;background: none;color: rgb(33, 150, 243);line-height: 1.2em;padding-left: 10px !important;border-radius: 0px !important;margin-top: 1em !important;margin-bottom: 1em !important;border-width: initial !important;border-style: none !important;border-color: initial !important;"><span class="pln" style="word-wrap: break-word;color: rgb(184, 255, 184);">$ </span><span class="kwd" style="word-wrap: break-word;color: rgb(226, 137, 100);">netstat</span><span class="pln" style="word-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="pun" style="word-wrap: break-word;color: rgb(184, 255, 184);">-</span><span class="pln" style="word-wrap: break-word;color: rgb(184, 255, 184);">g</span></code></p></li></ol>

9 – 显示网络路由信息

使用 r 输出网络路由信息,

<ol class="linenums list-paddingleft-2" style="margin-left: 2em;margin-right: 2em;"><li><p><code style="word-wrap: break-word;background: none;color: rgb(33, 150, 243);line-height: 1.2em;padding-left: 10px !important;border-radius: 0px !important;margin-top: 1em !important;margin-bottom: 1em !important;border-width: initial !important;border-style: none !important;border-color: initial !important;"><span class="pln" style="word-wrap: break-word;color: rgb(184, 255, 184);">$ </span><span class="kwd" style="word-wrap: break-word;color: rgb(226, 137, 100);">netstat</span><span class="pln" style="word-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="pun" style="word-wrap: break-word;color: rgb(184, 255, 184);">-</span><span class="pln" style="word-wrap: break-word;color: rgb(184, 255, 184);">r</span></code></p></li></ol>

10 – 持续输出

使用 c 选项持续输出结果

<ol class="linenums list-paddingleft-2" style="margin-left: 2em;margin-right: 2em;"><li><p><code style="word-wrap: break-word;background: none;color: rgb(33, 150, 243);line-height: 1.2em;padding-left: 10px !important;border-radius: 0px !important;margin-top: 1em !important;margin-bottom: 1em !important;border-width: initial !important;border-style: none !important;border-color: initial !important;"><span class="pln" style="word-wrap: break-word;color: rgb(184, 255, 184);">$ </span><span class="kwd" style="word-wrap: break-word;color: rgb(226, 137, 100);">netstat</span><span class="pln" style="word-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="pun" style="word-wrap: break-word;color: rgb(184, 255, 184);">-</span><span class="pln" style="word-wrap: break-word;color: rgb(184, 255, 184);">c</span></code></p></li></ol>

11 – 过滤出某个端口

与 grep 连用来过滤出某个端口的连接,

<ol class="linenums list-paddingleft-2" style="margin-left: 2em;margin-right: 2em;"><li><p><code style="word-wrap: break-word;background: none;color: rgb(33, 150, 243);line-height: 1.2em;padding-left: 10px !important;border-radius: 0px !important;margin-top: 1em !important;margin-bottom: 1em !important;border-width: initial !important;border-style: none !important;border-color: initial !important;"><span class="pln" style="word-wrap: break-word;color: rgb(184, 255, 184);">$ </span><span class="kwd" style="word-wrap: break-word;color: rgb(226, 137, 100);">netstat</span><span class="pln" style="word-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="pun" style="word-wrap: break-word;color: rgb(184, 255, 184);">-</span><span class="pln" style="word-wrap: break-word;color: rgb(184, 255, 184);">anp </span><span class="pun" style="word-wrap: break-word;color: rgb(184, 255, 184);">|</span><span class="pln" style="word-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="kwd" style="word-wrap: break-word;color: rgb(226, 137, 100);">grep</span><span class="pln" style="word-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="lit" style="word-wrap: break-word;color: rgb(51, 135, 204);">3306</span></code></p></li></ol>

12 – 统计连接个数

通过与 wc 和 grep 命令连用,可以统计指定端口的连接数量

<ol class="linenums list-paddingleft-2" style="margin-left: 2em;margin-right: 2em;"><li><p><code style="word-wrap: break-word;background: none;color: rgb(33, 150, 243);line-height: 1.2em;padding-left: 10px !important;border-radius: 0px !important;margin-top: 1em !important;margin-bottom: 1em !important;border-width: initial !important;border-style: none !important;border-color: initial !important;"><span class="pln" style="word-wrap: break-word;color: rgb(184, 255, 184);">$ </span><span class="kwd" style="word-wrap: break-word;color: rgb(226, 137, 100);">netstat</span><span class="pln" style="word-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="pun" style="word-wrap: break-word;color: rgb(184, 255, 184);">-</span><span class="pln" style="word-wrap: break-word;color: rgb(184, 255, 184);">anp </span><span class="pun" style="word-wrap: break-word;color: rgb(184, 255, 184);">|</span><span class="pln" style="word-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="kwd" style="word-wrap: break-word;color: rgb(226, 137, 100);">grep</span><span class="pln" style="word-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="lit" style="word-wrap: break-word;color: rgb(51, 135, 204);">3306</span><span class="pln" style="word-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="pun" style="word-wrap: break-word;color: rgb(184, 255, 184);">|</span><span class="pln" style="word-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="kwd" style="word-wrap: break-word;color: rgb(226, 137, 100);">wc</span><span class="pln" style="word-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="pun" style="word-wrap: break-word;color: rgb(184, 255, 184);">-</span><span class="pln" style="word-wrap: break-word;color: rgb(184, 255, 184);">l</span></code></p></li></ol>

这会输出 mysql 服务端口(即 3306)的连接数。

这就是我们简短的案例指南了,希望它带给你的信息量足够。 有任何疑问欢迎提出。


via: http://linuxtechlab.com/learn-use-netstat-with-examples/

作者:Shusain[3] 译者:lujun9972 校对:wxy

本文由 LCTT 原创编译,Linux中国 荣誉推出

LCTT 译者
通过示例学习使用 netstat

lujun9972 🌟 🌟
共计翻译:13 篇
贡献时间:12 天


推荐文章

< 左右滑动查看相关文章 >

通过示例学习使用 netstat通过示例学习使用 netstat通过示例学习使用 netstat通过示例学习使用 netstat通过示例学习使用 netstat通过示例学习使用 netstat

点击图片、输入文章 ID 或识别二维码直达


Hacking For Fun , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:通过示例学习使用 netstat
喜欢 (0)
发表我的评论
取消评论
表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址