{"id":240639,"date":"2022-05-01T09:50:48","date_gmt":"2022-05-01T01:50:48","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=240639"},"modified":"2022-04-19T11:53:20","modified_gmt":"2022-04-19T03:53:20","slug":"netstat-linux-content","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/netstat-linux-content.html","title":{"rendered":"\u4f7f\u7528netstat\u67e5\u770b\u4f60\u7684Linux\u670d\u52a1\u5668\u7f51\u7edc\u8fde\u63a5"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\nNetstat \u548c\u7c7b\u4f3c\u7684\u4e00\u4e9b\u7f51\u7edc\u76d1\u6d4b\u5de5\u5177\u88ab\u5f52\u5165 net-tools \u8f6f\u4ef6\u5305 \u91cc\uff0c\u7528\u6765\u663e\u793a\u5173\u4e8e\u6d3b\u52a8\u8fde\u63a5\u7684\u4fe1\u606f\u3002\u56e0\u4e3a\u8fd0\u884c\u5728\u5f00\u653e\u7684\u7aef\u53e3\u7684\u670d\u52a1\u5f80\u5f80\u5bb9\u6613\u88ab\u5229\u7528\uff0c\u5b9a\u671f\u8fdb\u884c\u7f51\u7edc\u76d1\u6d4b\u80fd\u591f\u5e2e\u52a9\u4f60\u53ca\u65e9\u63a2\u6d4b\u5230\u53ef\u7591\u7684\u6d3b\u52a8\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"<\/a>
\n\u5728 \u4e4b\u524d\u7684\u6587\u7ae0\u4e2d\uff0c\u6211\u5206\u4eab\u4e86\u4e00\u4e9b\u7ba1\u7406\u4f60\u7684\u4e2a\u4eba Linux \u670d\u52a1\u5668\u7684\u9996\u8981\u6b65\u9aa4\u3002\u6211\u7b80\u8981\u7684\u63d0\u5230\u4e86\u76d1\u6d4b\u7f51\u7edc\u8fde\u63a5\u7684\u76d1\u542c\u7aef\u53e3\uff0c\u6211\u60f3\u901a\u8fc7 Linux \u7cfb\u7edf\u7684 netstat \u547d\u4ee4\u6765\u6269\u5c55\u5f00\u8bb2\u8bb2\u3002<\/p>\n

\u670d\u52a1\u76d1\u6d4b\u548c\u7aef\u53e3\u626b\u63cf\u662f\u6807\u51c6\u7684\u884c\u4e1a\u60ef\u4f8b\u3002\u6709\u5f88\u597d\u7684\u8f6f\u4ef6\uff0c\u5982 Prometheus \u534f\u52a9\u8fd9\u4e2a\u8fc7\u7a0b\u81ea\u52a8\u5316\uff0cSELinux \u534f\u52a9\u4e0a\u4e0b\u6587\u548c\u4fdd\u62a4\u7cfb\u7edf\u8bbf\u95ee\u6743\u9650\u3002\u7136\u800c\uff0c\u6211\u76f8\u4fe1\u4e86\u89e3\u4f60\u7684\u670d\u52a1\u5668\u662f\u5982\u4f55\u8fde\u63a5\u5230\u5176\u4ed6\u7f51\u7edc\u548c\u8bbe\u5907\u7684\uff0c\u662f\u5efa\u7acb\u6b63\u5e38\u670d\u52a1\u5668\u57fa\u51c6\u7684\u5173\u952e\uff0c\u80fd\u591f\u5e2e\u52a9\u4f60\u8bc6\u522b\u6709\u53ef\u80fd\u8868\u660e\u9519\u8bef\u548c\u5165\u4fb5\u7b49\u5f02\u5e38\u60c5\u51b5\u3002\u4f5c\u4e3a\u4e00\u4e2a\u521d\u5b66\u8005\uff0c\u6211\u5df2\u7ecf\u638c\u63e1\u4e86 netstat \u547d\u4ee4\u4e3a\u6211\u7684\u670d\u52a1\u5668\u63d0\u4f9b\u4e86\u76d1\u6d4b\u548c\u7f51\u7edc\u6545\u969c\u6392\u9664\u7684\u91cd\u8981\u89c2\u5bdf\u624b\u6bb5\u3002<\/p>\n

Netstat \u548c\u7c7b\u4f3c\u7684\u4e00\u4e9b\u7f51\u7edc\u76d1\u6d4b\u5de5\u5177\u88ab\u5f52\u5165 net-tools \u8f6f\u4ef6\u5305 \u91cc\uff0c\u7528\u6765\u663e\u793a\u5173\u4e8e\u6d3b\u52a8\u8fde\u63a5\u7684\u4fe1\u606f\u3002\u56e0\u4e3a\u8fd0\u884c\u5728\u5f00\u653e\u7684\u7aef\u53e3\u7684\u670d\u52a1\u5f80\u5f80\u5bb9\u6613\u88ab\u5229\u7528\uff0c\u5b9a\u671f\u8fdb\u884c\u7f51\u7edc\u76d1\u6d4b\u80fd\u591f\u5e2e\u52a9\u4f60\u53ca\u65e9\u63a2\u6d4b\u5230\u53ef\u7591\u7684\u6d3b\u52a8\u3002<\/p>\n

\u5b89\u88c5 netstat<\/strong><\/span><\/div>\n

netstat \u7ecf\u5e38\u9884\u88c5\u5728 Linux \u53d1\u884c\u7248\u4e0a\u3002\u5982\u679c netstat \u6ca1\u6709\u5728\u4f60\u7684\u670d\u52a1\u5668\u4e0a\u5b89\u88c5\uff0c\u7528\u4f60\u7684\u5305\u7ba1\u7406\u5668\u5b89\u88c5\u5b83\u3002\u4e0b\u9762\u662f\u5728\u57fa\u4e8e Debian \u7684\u7cfb\u7edf\u4e0a\uff1a<\/p>\n

$ sudo apt-get install net-tools\r\n<\/pre>\n

\u5728\u57fa\u4e8e Fedora \u7684\u7cfb\u7edf\u4e0a\uff1a<\/p>\n

$ dnf install net-tools\r\n<\/pre>\n
\u4f7f\u7528 netstat<\/strong><\/span><\/div>\n

\u5c31\u5176\u672c\u8eab\u800c\u8a00\uff0cnetstat \u547d\u4ee4\u663e\u793a\u4e86\u5168\u90e8\u5efa\u7acb\u6210\u529f\u7684\u8fde\u63a5\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528 netstat \u7684\u53c2\u6570\u6307\u5b9a\u8fdb\u4e00\u6b65\u9884\u671f\u7684\u8f93\u51fa\u3002\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u8981\u663e\u793a\u6240\u6709\u76d1\u542c\u548c\u975e\u76d1\u542c\u7684\u8fde\u63a5\uff0c\u4f7f\u7528 --all(-a \u4e3a\u7b80\u5199)\u7684\u53c2\u6570\u3002\u8fd9\u5c06\u8fd4\u56de\u8bb8\u591a\u7ed3\u679c\uff0c\u6240\u4ee5\u5728\u4f8b\u5b50\u4e2d\u6211\u7528\u7ba1\u9053\u7b26\u8f93\u51fa\u7ed9 head \u547d\u4ee4\u6765\u663e\u793a\u8f93\u51fa\u7684\u524d 15 \u884c\uff1a<\/p>\n

    $ netstat --all | head -n 15\r\n    Active Internet connections (servers and established)\r\n    Proto Recv-Q Send-Q Local Address           Foreign Address         State      \r\n    tcp        0      0 *:27036                 *:*                     LISTEN      \r\n    tcp        0      0 localhost:27060         *:*                     LISTEN      \r\n    tcp        0      0 *:16001                 *:*                     LISTEN      \r\n    tcp        0      0 localhost:6463          *:*                     LISTEN      \r\n    tcp        0      0 *:ssh                   *:*                     LISTEN      \r\n    tcp        0      0 localhost:57343         *:*                     LISTEN      \r\n    tcp        0      0 *:ipp                   *:*                     LISTEN      \r\n    tcp        0      0 *:4713                  *:*                     LISTEN      \r\n    tcp        0      0 10.0.1.222:48388        syd15s17-in-f5.1e:https ESTABLISHED\r\n    tcp        0      0 10.0.1.222:48194        ec2-35-86-38-2.us:https ESTABLISHED\r\n    tcp        0      0 10.0.1.222:56075        103-10-125-164.va:27024 ESTABLISHED\r\n    tcp        0      0 10.0.1.222:46680        syd15s20-in-f10.1:https ESTABLISHED\r\n    tcp        0      0 10.0.1.222:52730        syd09s23-in-f3.1e:https ESTABLISHED\r\n<\/pre>\n

\u8981\u53ea\u663e\u793a TCP \u7aef\u53e3\uff0c\u4f7f\u7528 --all \u548c --tcp \u53c2\u6570\uff0c\u6216\u8005\u7b80\u5199\u6210 -at \uff1a<\/p>\n

    $ netstat -at | head -n 5\r\n    Active Internet connections (servers and established)\r\n    Proto Recv-Q Send-Q Local Address   Foreign Address  State      \r\n    tcp        0      0 *:27036         *:*              LISTEN      \r\n    tcp        0      0 localhost:27060 *:*              LISTEN      \r\n    tcp        0      0 *:16001         *:*              LISTEN\r\n<\/pre>\n

\u8981\u53ea\u663e\u793a UDP \u7aef\u53e3\uff0c\u4f7f\u7528 --all \u548c --udp \u53c2\u6570\uff0c\u6216\u8005\u7b80\u5199\u6210 -au \uff1a<\/p>\n

    $ netstat -au | head -n 5\r\n    Active Internet connections (servers and established)\r\n    Proto Recv-Q Send-Q Local Address     Foreign Address    State      \r\n    udp        0      0 *:27036           *:*                                \r\n    udp        0      0 10.0.1.222:44741  224.0.0.56:46164   ESTABLISHED\r\n    udp        0      0 *:bootpc     \r\n<\/pre>\n

netstat \u547d\u4ee4\u53c2\u6570\u5e38\u5e38\u662f\u7b80\u5355\u6613\u61c2\u7684\u3002\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u8981\u663e\u793a\u5e26\u6709\u5168\u90e8\u8fdb\u7a0b ID(PID)\u548c\u6570\u5b57\u5730\u5740\u7684\u76d1\u542c TCP \u548c UDP \u7684\u7aef\u53e3\uff1a<\/p>\n

    $ sudo netstat --tcp --udp --listening --programs --numeric\r\n    Active Internet connections (only servers)\r\n    Proto Recv-Q Send-Q Local Address      Foreign Addr  State   PID\/Program name    \r\n    tcp        0      0 0.0.0.0:111        0.0.0.0:*     LISTEN  1\/systemd            \r\n    tcp        0      0 192.168.122.1:53   0.0.0.0:*     LISTEN  2500\/dnsmasq        \r\n    tcp        0      0 0.0.0.0:22         0.0.0.0:*     LISTEN  1726\/sshd            \r\n    tcp        0      0 127.0.0.1:631      0.0.0.0:*     LISTEN  1721\/cupsd          \r\n    tcp        0      0 127.0.0.1:6010     0.0.0.0:*     LISTEN  4023\/sshd: tux@  \r\n    tcp6       0      0 :::111             :::*          LISTEN  1\/systemd            \r\n    tcp6       0      0 :::22              :::*          LISTEN  1726\/sshd            \r\n    tcp6       0      0 ::1:631            :::*          LISTEN  1721\/cupsd          \r\n    tcp6       0      0 ::1:6010           :::*          LISTEN  4023\/sshd: tux@  \r\n    udp        0      0 0.0.0.0:40514      0.0.0.0:*             1499\/avahi-daemon:  \r\n    udp        0      0 192.168.122.1:53   0.0.0.0:*             2500\/dnsmasq        \r\n    udp        0      0 0.0.0.0:67         0.0.0.0:*             2500\/dnsmasq        \r\n    udp        0      0 0.0.0.0:111        0.0.0.0:*             1\/systemd            \r\n    udp        0      0 0.0.0.0:5353       0.0.0.0:*             1499\/avahi-daemon:  \r\n    udp6       0      0 :::111             :::*                  1\/systemd            \r\n    udp6       0      0 :::44235           :::*                  1499\/avahi-daemon:  \r\n    udp6       0      0 :::5353            :::*                  1499\/avahi-daemon:\r\n<\/pre>\n

\u8fd9\u4e2a\u5e38\u7528\u7ec4\u5408\u7b80\u5199\u7248\u672c\u662f -tulpn \u3002<\/p>\n

\u8981\u663e\u793a\u4e00\u4e2a\u6307\u5b9a\u670d\u52a1\u7684\u4fe1\u606f\uff0c\u4f7f\u7528 grep \u547d\u4ee4\u8fc7\u6ee4\uff1a<\/p>\n

    $ sudo netstat -anlp | grep cups\r\n    tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      1721\/cupsd           tcp6       0      0 ::1:631                 :::*                    LISTEN      1721\/cupsd\r\n    unix  2      [ ACC ]     STREAM     LISTENING     27251    1\/systemd \/var\/run\/cups\/cups.sock\r\n    unix  2      [ ]         DGRAM                    59530    1721\/cupsd\r\n    unix  3      [ ]         STREAM     CONNECTED     55196    1721\/cupsd \/var\/run\/cups\/cups.sock\r\n<\/pre>\n

\u63a5\u4e0b\u6765\u7684\u6b65\u9aa4
\n\u4e00\u65e6\u4f60\u8fd0\u884c\u4e86 netstat \u547d\u4ee4\uff0c\u4f60\u5c31\u53ef\u4ee5\u91c7\u53d6\u63aa\u65bd\u6765\u4fdd\u62a4\u4f60\u7684\u7cfb\u7edf\uff0c\u786e\u4fdd\u53ea\u6709\u4f60\u4e3b\u52a8\u4f7f\u7528\u7684\u670d\u52a1\u5728\u4f60\u7684\u7f51\u7edc\u4e0a\u88ab\u76d1\u542c\u3002<\/p>\n

\u8bc6\u522b\u901a\u5e38\u88ab\u5229\u7528\u7684\u7aef\u53e3\u548c\u670d\u52a1\u3002\u4e00\u822c\u6765\u8bf4\uff0c\u5173\u95ed\u4f60\u5b9e\u9645\u4e0d\u4f7f\u7528\u7684\u7aef\u53e3\u3002
\n\u7559\u610f\u4e0d\u5e38\u89c1\u7684\u7aef\u53e3\u53f7\uff0c\u8ba4\u8bc6\u4e86\u89e3\u5728\u4f60\u7cfb\u7edf\u4e0a\u4f7f\u7528\u7684\u5408\u6cd5\u7aef\u53e3\u3002
\n\u5bc6\u5207\u6ce8\u610f SELinux \u9519\u8bef\u3002\u6709\u65f6\u4f60\u9700\u8981\u505a\u7684\u53ea\u662f\u66f4\u65b0\u4e0a\u4e0b\u6587\uff0c\u4ee5\u5339\u914d\u4f60\u5bf9\u7cfb\u7edf\u505a\u7684\u5408\u6cd5\u66f4\u6539\uff0c\u4f46\u662f\u8981\u9605\u8bfb\u9519\u8bef\u8b66\u544a\uff0c\u4ee5\u786e\u4fdd SELinux \u63d0\u9192\u4f60\u7684\u4e0d\u662f\u53ef\u7591\u6216\u8005\u6076\u610f\u7684\u6d3b\u52a8\u3002
\n\u5982\u679c\u4f60\u53d1\u73b0\u4e00\u4e2a\u7aef\u53e3\u6b63\u5728\u8fd0\u884c\u4e00\u4e2a\u53ef\u7591\u7684\u670d\u52a1\uff0c\u6216\u8005\u4f60\u53ea\u662f\u7b80\u5355\u7684\u60f3\u8981\u5173\u95ed\u4f60\u4e0d\u518d\u4f7f\u7528\u7684\u7aef\u53e3\uff0c\u4f60\u53ef\u4ee5\u9075\u4ece\u4ee5\u4e0b\u8fd9\u4e9b\u6b65\u9aa4\uff0c\u901a\u8fc7\u9632\u706b\u5899\u89c4\u5219\u624b\u52a8\u62d2\u7edd\u7aef\u53e3\u8bbf\u95ee\uff1a<\/p>\n

\u5982\u679c\u4f60\u5728\u4f7f\u7528 firewall-cmd \uff0c\u8fd0\u884c\u8fd9\u4e9b\u547d\u4ee4\uff1a<\/p>\n

    $ sudo firewall-cmd \u2013remove-port=<port number>\/tcp\r\n    $ sudo firewall-cmd \u2013runtime-to-permanent\r\n<\/pre>\n

\u5982\u679c\u4f60\u5728\u4f7f\u7528 UFW\uff0c\u8fd0\u884c\u4ee5\u4e0b\u7684\u547d\u4ee4\uff1a<\/p>\n

 $ sudo ufw deny \r\n<\/pre>\n

\u4e0b\u4e00\u6b65\uff0c\u4f7f\u7528 systemctl \u6765\u505c\u6b62\u8fd9\u4e2a\u670d\u52a1\uff1a<\/p>\n

$ systemctl stop\r\n<\/pre>\n
\u7406\u89e3 netstat<\/strong><\/span><\/div>\n

netstat \u662f\u4e00\u4e2a\u5feb\u901f\u6536\u96c6\u4f60\u7684\u670d\u52a1\u5668\u7f51\u7edc\u8fde\u63a5\u4fe1\u606f\u7684\u6709\u7528\u5de5\u5177\u3002\u5b9a\u671f\u7f51\u7edc\u76d1\u6d4b\u662f\u4e86\u89e3\u4f60\u7684\u7cfb\u7edf\u7684\u91cd\u8981\u7ec4\u6210\u90e8\u5206\uff0c\u5bf9\u5e2e\u52a9\u4f60\u4fdd\u6301\u4f60\u7684\u7cfb\u7edf\u5b89\u5168\u6709\u7740\u91cd\u8981\u610f\u4e49\u3002\u5c06\u8fd9\u4e00\u6b65\u7eb3\u5165\u4f60\u7684\u65e5\u5e38\u7ba1\u7406\u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u7c7b\u4f3c netstat \u6216\u8005 ss \uff0c\u4ee5\u53ca Nmap \u7b49\u5f00\u6e90\u7aef\u53e3\u626b\u63cf\u5668\u6216\u8005 Wireshark \u7b49\u55c5\u63a2\u5668 \uff0c\u5b83\u4eec\u90fd\u5141\u8bb8\u8bbe\u5b9a \u8ba1\u5212\u4efb\u52a1\u3002<\/p>\n

\u968f\u7740\u670d\u52a1\u5668\u5b58\u50a8\u4e86\u5927\u91cf\u7684\u4e2a\u4eba\u6570\u636e\uff0c\u786e\u4fdd\u4e2a\u4eba\u670d\u52a1\u5668\u7684\u5b89\u5168\u65e5\u76ca\u91cd\u8981\u3002\u901a\u8fc7\u4e86\u89e3\u4f60\u7684\u670d\u52a1\u5668\u600e\u6837\u8fde\u63a5\u5230\u4e92\u8054\u7f51\uff0c\u4f60\u53ef\u4ee5\u964d\u4f4e\u4f60\u7684\u673a\u5668\u7684\u98ce\u9669\uff0c\u540c\u65f6\u4f60\u4ecd\u53ef\u4ee5\u5728\u6570\u5b57\u65f6\u4ee3\u5927\u91cf\u7684\u8fde\u63a5\u4e2d\u83b7\u5f97\u76ca\u5904\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

\u5728 \u4e4b\u524d\u7684\u6587\u7ae0\u4e2d\uff0c\u6211\u5206\u4eab\u4e86\u4e00\u4e9b\u7ba1\u7406\u4f60\u7684\u4e2a\u4eba Linux \u670d\u52a1\u5668\u7684\u9996\u8981\u6b65\u9aa4\u3002\u6211\u7b80\u8981\u7684\u63d0\u5230\u4e86\u76d1\u6d4b\u7f51\u7edc\u8fde\u63a5\u7684\u76d1\u542c\u7aef\u53e3 […]<\/p>\n","protected":false},"author":643,"featured_media":240642,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-240639","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/240639","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/643"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=240639"}],"version-history":[{"count":5,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/240639\/revisions"}],"predecessor-version":[{"id":240818,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/240639\/revisions\/240818"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/240642"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=240639"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=240639"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=240639"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}