mirror of
https://github.com/fofolee/uTools-Manuals.git
synced 2025-06-08 23:14:06 +08:00
87 lines
6.1 KiB
HTML
87 lines
6.1 KiB
HTML
<h1 id="netstat">netstat</h1>
|
||
<p>查看Linux中网络系统状态信息</p>
|
||
<h2 id="补充说明">补充说明</h2>
|
||
<p><strong>netstat命令</strong> 用来打印Linux中网络系统的状态信息,可让你得知整个Linux系统的网络情况。</p>
|
||
<h3 id="语法">语法</h3>
|
||
<pre><code class="language-bash">netstat(选项)</code></pre>
|
||
<h3 id="选项">选项</h3>
|
||
<pre><code class="language-bash">-a或--all:显示所有连线中的Socket;
|
||
-A<网络类型>或--<网络类型>:列出该网络类型连线中的相关地址;
|
||
-c或--continuous:持续列出网络状态;
|
||
-C或--cache:显示路由器配置的快取信息;
|
||
-e或--extend:显示网络其他相关信息;
|
||
-F或--fib:显示FIB;
|
||
-g或--groups:显示多重广播功能群组组员名单;
|
||
-h或--help:在线帮助;
|
||
-i或--interfaces:显示网络界面信息表单;
|
||
-l或--listening:显示监控中的服务器的Socket;
|
||
-M或--masquerade:显示伪装的网络连线;
|
||
-n或--numeric:直接使用ip地址,而不通过域名服务器;
|
||
-N或--netlink或--symbolic:显示网络硬件外围设备的符号连接名称;
|
||
-o或--timers:显示计时器;
|
||
-p或--programs:显示正在使用Socket的程序识别码和程序名称;
|
||
-r或--route:显示Routing Table;
|
||
-s或--statistice:显示网络工作信息统计表;
|
||
-t或--tcp:显示TCP传输协议的连线状况;
|
||
-u或--udp:显示UDP传输协议的连线状况;
|
||
-v或--verbose:显示指令执行过程;
|
||
-V或--version:显示版本信息;
|
||
-w或--raw:显示RAW传输协议的连线状况;
|
||
-x或--unix:此参数的效果和指定"-A unix"参数相同;
|
||
--ip或--inet:此参数的效果和指定"-A inet"参数相同。</code></pre>
|
||
<h3 id="实例">实例</h3>
|
||
<p><strong>列出所有端口 (包括监听和未监听的)</strong></p>
|
||
<pre><code class="language-bash">netstat -a #列出所有端口
|
||
netstat -at #列出所有tcp端口
|
||
netstat -au #列出所有udp端口 </code></pre>
|
||
<p><strong>列出所有处于监听状态的 Sockets</strong></p>
|
||
<pre><code class="language-bash">netstat -l #只显示监听端口
|
||
netstat -lt #只列出所有监听 tcp 端口
|
||
netstat -lu #只列出所有监听 udp 端口
|
||
netstat -lx #只列出所有监听 UNIX 端口</code></pre>
|
||
<p><strong>显示每个协议的统计信息</strong></p>
|
||
<pre><code class="language-bash">netstat -s 显示所有端口的统计信息
|
||
netstat -st 显示TCP端口的统计信息
|
||
netstat -su 显示UDP端口的统计信息
|
||
</code></pre>
|
||
<p><strong>在netstat输出中显示 PID 和进程名称</strong></p>
|
||
<pre><code class="language-bash">netstat -pt</code></pre>
|
||
<p><code>netstat -p</code>可以与其它开关一起使用,就可以添加“PID/进程名称”到netstat输出中,这样debugging的时候可以很方便的发现特定端口运行的程序。</p>
|
||
<p><strong>在netstat输出中不显示主机,端口和用户名(host, port or user)</strong></p>
|
||
<p>当你不想让主机,端口和用户名显示,使用<code>netstat -n</code>。将会使用数字代替那些名称。同样可以加速输出,因为不用进行比对查询。</p>
|
||
<pre><code class="language-bash">netstat -an</code></pre>
|
||
<p>如果只是不想让这三个名称中的一个被显示,使用以下命令:</p>
|
||
<pre><code class="language-bash">netsat -a --numeric-ports
|
||
netsat -a --numeric-hosts
|
||
netsat -a --numeric-users</code></pre>
|
||
<p><strong>持续输出netstat信息</strong></p>
|
||
<pre><code class="language-bash">netstat -c #每隔一秒输出网络信息</code></pre>
|
||
<p><strong>显示系统不支持的地址族(Address Families)</strong></p>
|
||
<pre><code class="language-bash">netstat --verbose</code></pre>
|
||
<p>在输出的末尾,会有如下的信息:</p>
|
||
<pre><code class="language-bash">netstat: no support for `AF IPX' on this system.
|
||
netstat: no support for `AF AX25' on this system.
|
||
netstat: no support for `AF X25' on this system.
|
||
netstat: no support for `AF NETROM' on this system.</code></pre>
|
||
<p><strong>显示核心路由信息</strong></p>
|
||
<pre><code class="language-bash">netstat -r</code></pre>
|
||
<p>使用<code>netstat -rn</code>显示数字格式,不查询主机名称。</p>
|
||
<p><strong>找出程序运行的端口</strong></p>
|
||
<p>并不是所有的进程都能找到,没有权限的会不显示,使用 root 权限查看所有的信息。</p>
|
||
<pre><code class="language-bash">netstat -ap | grep ssh</code></pre>
|
||
<p>找出运行在指定端口的进程:</p>
|
||
<pre><code class="language-bash">netstat -an | grep ':80'</code></pre>
|
||
<p><strong>通过端口找进程ID</strong></p>
|
||
<div class="sourceCode" id="cb15"><pre><code class="language-bash"><a class="sourceLine" id="cb15-1" data-line-number="1"><span class="fu">netstat</span> -anp<span class="kw">|</span><span class="fu">grep</span> 8081 <span class="kw">|</span> <span class="fu">grep</span> LISTEN<span class="kw">|</span><span class="fu">awk</span> <span class="st">'{printf $7}'</span><span class="kw">|</span><span class="fu">cut</span> -d/ -f1</a></code></pre></div>
|
||
<p><strong>显示网络接口列表</strong></p>
|
||
<pre><code class="language-bash">netstat -i</code></pre>
|
||
<p>显示详细信息,像是ifconfig使用<code>netstat -ie</code>。</p>
|
||
<p><strong>IP和TCP分析</strong></p>
|
||
<p>查看连接某服务端口最多的的IP地址:</p>
|
||
<pre><code class="language-bash">netstat -ntu | grep :80 | awk '{print $5}' | cut -d: -f1 | awk '{++ip[$1]} END {for(i in ip) print ip[i],"\t",i}' | sort -nr</code></pre>
|
||
<p>TCP各种状态列表:</p>
|
||
<pre><code class="language-bash">netstat -nt | grep -e 127.0.0.1 -e 0.0.0.0 -e ::: -v | awk '/^tcp/ {++state[$NF]} END {for(i in state) print i,"\t",state[i]}'</code></pre>
|
||
<p>查看phpcgi进程数,如果接近预设值,说明不够用,需要增加:</p>
|
||
<pre><code class="language-bash">netstat -anpo | grep "php-cgi" | wc -l</code></pre>
|
||
<!-- Linux命令行搜索引擎:https://jaywcjlove.github.io/linux-command/ -->
|