完整教程:Linux怎么查看服务器开放和启用的端口
<style>pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", "Courier New", monospace !important; font-size: 14px !important; line-height: 1.6 !important; padding: 16px !important; margin: 16px 0 !important; background-color: rgba(248, 248, 248, 1) !important; border: 1px solid rgba(225, 228, 232, 1) !important; border-radius: 6px !important; tab-size: 4 !important; -moz-tab-size: 4 !important; max-width: 100% !important; box-sizing: border-box !important }code { font-family: "Consolas", "Monaco", "Courier New", monospace !important; font-size: 14px !important; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow-wrap: normal !important; display: inline !important; background: rgba(0, 0, 0, 0) !important; border: none !important; padding: 0 !important; margin: 0 !important; line-height: inherit !important }
pre code { background: rgba(0, 0, 0, 0) !important; border: 0 !important; border-radius: 0 !important; display: block !important; line-height: 1.6 !important; margin: 0 !important; max-width: none !important; overflow: visible !important; padding: 0 !important; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; color: inherit !important }
.token.comment, .token.prolog, .token.doctype, .token.cdata { color: rgba(112, 128, 144, 1) !important; font-style: italic !important }
.token.punctuation { color: rgba(153, 153, 153, 1) !important }
.token.atrule, .token.attr-value, .token.keyword { color: rgba(0, 119, 170, 1) !important; font-weight: bold !important }
.token.function, .token.class-name { color: rgba(221, 74, 104, 1) !important; font-weight: bold !important }
.token.selector, .token.attr-name, .token.string, .token.char, .token.builtin, .token.inserted { color: rgba(102, 153, 0, 1) !important }
.token.property, .token.tag, .token.boolean, .token.number, .token.constant, .token.symbol, .token.deleted { color: rgba(153, 0, 85, 1) !important }
.cnblogs-markdown pre, .cnblogs-post-body pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; background-color: rgba(248, 248, 248, 1) !important; border: 1px solid rgba(225, 228, 232, 1) !important; border-radius: 6px !important; padding: 16px !important; margin: 16px 0 !important }
pre, pre, pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important }</style>
<style>pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", "Courier New", monospace !important; font-size: 14px !important; line-height: 1.6 !important; padding: 16px !important; margin: 16px 0 !important; background-color: rgba(248, 248, 248, 1) !important; border: 1px solid rgba(225, 228, 232, 1) !important; border-radius: 6px !important; tab-size: 4 !important; -moz-tab-size: 4 !important; max-width: 100% !important; box-sizing: border-box !important }
code { font-family: "Consolas", "Monaco", "Courier New", monospace !important; font-size: 14px !important; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow-wrap: normal !important; display: inline !important; background: rgba(0, 0, 0, 0) !important; border: none !important; padding: 0 !important; margin: 0 !important; line-height: inherit !important }
pre code { background: rgba(0, 0, 0, 0) !important; border: 0 !important; border-radius: 0 !important; display: block !important; line-height: 1.6 !important; margin: 0 !important; max-width: none !important; overflow: visible !important; padding: 0 !important; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; color: inherit !important }
.token.comment, .token.prolog, .token.doctype, .token.cdata { color: rgba(112, 128, 144, 1) !important; font-style: italic !important }
.token.punctuation { color: rgba(153, 153, 153, 1) !important }
.token.atrule, .token.attr-value, .token.keyword { color: rgba(0, 119, 170, 1) !important; font-weight: bold !important }
.token.function, .token.class-name { color: rgba(221, 74, 104, 1) !important; font-weight: bold !important }
.token.selector, .token.attr-name, .token.string, .token.char, .token.builtin, .token.inserted { color: rgba(102, 153, 0, 1) !important }
.token.property, .token.tag, .token.boolean, .token.number, .token.constant, .token.symbol, .token.deleted { color: rgba(153, 0, 85, 1) !important }
.cnblogs-markdown pre, .cnblogs-post-body pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; background-color: rgba(248, 248, 248, 1) !important; border: 1px solid rgba(225, 228, 232, 1) !important; border-radius: 6px !important; padding: 16px !important; margin: 16px 0 !important }
pre, pre, pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important }</style><div class="htmledit_views atom-one-dark" id="content_views"><p style="text-align: center"><img alt="" src="https://i-blog.csdnimg.cn/direct/77c4e8b6554d4236aee2ab946c7aaec2.jpeg"></p><p>在 Linux 系统中,可以通过以下方法查看 <strong>服务器开放和启用的端口</strong>。以下是详细的步骤和工具,适用于不同场景。</p><hr><h3><strong>1. 使用 <code>ss</code> 查看开放的端口</strong></h3><p><code>ss</code> 是一个现代化工具,用于显示网络连接和监听的端口。</p><h4><strong>1.1 查看正在监听的端口</strong></h4><p>运行以下命令:</p><p>bash</p><p>复制</p>
<pre style="white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important"><code>ss -tuln</code></pre>
<ul><li><strong>参数说明</strong>:
<ul><li><code>-t</code>:显示 TCP 端口。</li><li><code>-u</code>:显示 UDP 端口。</li><li><code>-l</code>:仅显示监听的端口。</li><li><code>-n</code>:以数字形式显示端口号(不解析为服务名称)。</li></ul></li></ul><p><strong>示例输出</strong>:</p><p>apache</p><p>复制</p>
<pre style="white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important"><code>NetidState Recv-Q Send-Q Local Address:Port Peer Address:Port
tcp LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
tcp LISTEN 0 128 127.0.0.1:3306 0.0.0.0:*
udp UNCONN 0 0 0.0.0.0:123 0.0.0.0:*</code></pre>
<ul><li><strong>解释</strong>:
<ul><li><code>0.0.0.0:22</code>:表示所有网络接口上监听的 SSH 服务端口。</li><li><code>127.0.0.1:3306</code>:表示仅在本地监听的 MySQL 服务端口。</li></ul></li></ul><hr><h3><strong>2. 使用 <code>netstat</code> 查看开放的端口</strong></h3><p>如果 <code>ss</code> 不可用,可以使用 <code>netstat</code>(部分系统需安装 <code>net-tools</code> 包)。</p><h4><strong>2.1 查看监听的端口</strong></h4><p>运行以下命令:</p><p>bash</p><p>复制</p>
<pre style="white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important"><code>netstat -tuln</code></pre>
<ul><li><strong>参数说明</strong>:
<ul><li><code>-t</code>:显示 TCP 端口。</li><li><code>-u</code>:显示 UDP 端口。</li><li><code>-l</code>:仅显示监听的端口。</li><li><code>-n</code>:以数字形式显示端口号。</li></ul></li></ul><p><strong>示例输出</strong>:</p><p>apache</p><p>复制</p>
<pre style="white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important"><code>Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN
udp 0 0 0.0.0.0:123 0.0.0.0:*</code></pre>
<h4><strong>2.2 安装 <code>net-tools</code>(如果未安装)</strong></h4><p>在部分系统中,<code>netstat</code> 可能缺失,需要手动安装:</p><p>bash</p><p>复制</p>
<pre style="white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important"><code>sudo apt install net-tools # Ubuntu/Debian 系
sudo yum install net-tools # CentOS/RHEL 系</code></pre>
<hr><h3><strong>3. 使用 <code>lsof</code> 查看开放的端口</strong></h3><p><code>lsof</code> 是一个强大的工具,可以查看与端口相关的进程。</p><h4><strong>3.1 查看监听的端口</strong></h4><p>运行以下命令:</p><p>bash</p><p>复制</p>
<pre style="white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important"><code>sudo lsof -i -P -n | grep LISTEN</code></pre>
<ul><li><strong>参数说明</strong>:
<ul><li><code>-i</code>:显示网络连接。</li><li><code>-P</code>:显示端口号,而不是服务名称。</li><li><code>-n</code>:避免解析域名,加快显示速度。</li></ul></li></ul><p><strong>示例输出</strong>:</p><p>apache</p><p>复制</p>
<pre style="white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important"><code>sshd 1234 root 3uIPv4123456 0t0TCP *:22 (LISTEN)
mysqld 2345 mysql 10u IPv4654321 0t0TCP 127.0.0.1:3306 (LISTEN)</code></pre>
<hr><h3><strong>4. 使用 <code>firewall-cmd</code> 查看防火墙开放的端口</strong></h3><p>如果服务器使用 <strong>Firewalld</strong> 作为防火墙,可以通过以下命令查看防火墙规则。</p><h4><strong>4.1 查看已开放的端口</strong></h4><p>运行以下命令:</p><p>bash</p><p>复制</p>
<pre style="white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important"><code>sudo firewall-cmd --list-ports</code></pre>
<ul><li><strong>示例输出</strong>:
<p>复制</p>
<pre style="white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important"><code>22/tcp 80/tcp 443/tcp</code></pre>
<ul><li>表示 TCP 端口 <code>22</code>(SSH)、<code>80</code>(HTTP)、<code>443</code>(HTTPS)已开放。</li></ul></li></ul><h4><strong>4.2 查看所有防火墙规则</strong></h4><p>运行以下命令:</p><p>bash</p><p>复制</p>
<pre style="white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important"><code>sudo firewall-cmd --list-all</code></pre>
<ul><li><strong>示例输出</strong>:
<p>yaml</p><p>复制</p>
<pre style="white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important"><code>public (active)
target: default
icmp-block-inversion: no
interfaces: eth0
sources:
services: ssh http https
ports: 22/tcp 80/tcp 443/tcp
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:</code></pre>
</li></ul><h4><strong>4.3 开放新端口</strong></h4><p>如果需要开放额外的端口(如 8080 TCP),可以运行:</p><p>bash</p><p>复制</p>
<pre style="white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important"><code>sudo firewall-cmd --add-port=8080/tcp --permanent
sudo firewall-cmd --reload</code></pre>
<hr><h3><strong>5. 使用 <code>iptables</code> 查看防火墙规则</strong></h3><p>如果服务器使用的是 <code>iptables</code> 防火墙,可以通过以下命令查看规则。</p><h4><strong>5.1 查看所有规则</strong></h4><p>运行以下命令:</p><p>bash</p><p>复制</p>
<pre style="white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important"><code>sudo iptables -L -n -v</code></pre>
<ul><li><strong>示例输出</strong>:
<p>apache</p><p>复制</p>
<pre style="white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important"><code>Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
1005000 ACCEPT tcp--* * 0.0.0.0/0 0.0.0.0/0 tcp dpt:22
50 3000 ACCEPT tcp--* * 0.0.0.0/0 0.0.0.0/0 tcp dpt:80</code></pre>
</li></ul><h4><strong>5.2 开放新端口</strong></h4><p>如果需要新增开放端口(如 8080 TCP),可以运行:</p><p>bash</p><p>复制</p>
<pre style="white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important"><code>sudo iptables -A INPUT -p tcp --dport 8080 -j ACCEPT</code></pre>
<hr><h3><strong>6. 使用 <code>nmap</code> 扫描本地开放端口</strong></h3><p><code>nmap</code> 是一个网络扫描工具,可以用于检查本地或远程服务器的开放端口。</p><h4><strong>6.1 安装 nmap</strong></h4><p>bash</p><p>复制</p>
<pre style="white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important"><code>sudo apt install nmap # Ubuntu/Debian 系
sudo yum install nmap # CentOS/RHEL 系</code></pre>
<h4><strong>6.2 扫描本地开放端口</strong></h4><p>运行以下命令:</p><p>bash</p><p>复制</p>
<pre style="white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important"><code>sudo nmap -sT -p- 127.0.0.1</code></pre>
<ul><li><strong><code>-sT</code></strong>:扫描 TCP 端口。</li><li><strong><code>-p-</code></strong>:扫描所有端口(1-65535)。</li></ul><p><strong>示例输出</strong>:</p><p>复制</p>
<pre style="white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important"><code>Starting Nmap 7.80 ( https://nmap.org ) at 2023-08-16 10:00 UTC
Nmap scan report for localhost (127.0.0.1)
PORT STATE SERVICE
22/tcp openssh
80/tcp openhttp
443/tcp openhttps
3306/tcpopenmysql</code></pre>
<hr><h3><strong>7. 总结</strong></h3><table><thead><tr><th><strong>工具</strong></th><th><strong>用途</strong></th><th><strong>命令示例</strong></th></tr></thead><tbody><tr><td><code>ss</code></td><td>查看监听的端口</td><td><code>ss -tuln</code></td></tr><tr><td><code>netstat</code></td><td>查看监听的端口</td><td><code>netstat -tuln</code></td></tr><tr><td><code>lsof</code></td><td>查看端口和相关进程</td><td>`lsof -i -P -n</td></tr><tr><td><code>firewall-cmd</code></td><td>查看防火墙开放的端口</td><td><code>firewall-cmd --list-ports</code></td></tr><tr><td><code>iptables</code></td><td>查看防火墙规则</td><td><code>iptables -L -n -v</code></td></tr><tr><td><code>nmap</code></td><td>扫描本地开放端口</td><td><code>nmap -sT -p- 127.0.0.1</code></td></tr></tbody></table><p>通过以上方法,您可以轻松检查 Linux 服务器上开放和启用的端口,并根据需要调整防火墙规则或开放新端口。</p></div>
---
<div style="background: rgba(254, 252, 232, 1); padding: 15px 18px; border: 1px solid rgba(234, 179, 8, 1); border-radius: 8px; margin-top: 30px">
<h3 style="color: rgba(161, 98, 7, 1); margin-top: 0; font-size: 16px"> 技能提升</h3>
<p style="color: rgba(102, 102, 102, 1); font-size: 13px; margin-bottom: 12px">如果你觉得本文有帮助,以下资源可以帮你深入学习:</p>
<ol style="line-height: 2; font-size: 14px; padding-left: 20px">
<li><strong>趣谈Linux操作系统</strong><br>
<span style="color: rgba(136, 136, 136, 1); font-size: 13px"> 刘超 | 轻松理解Linux操作系统核心原理</span></li>
<li><strong>Linux性能优化实战</strong><br>
<span style="color: rgba(136, 136, 136, 1); font-size: 13px"> 倪朋飞 | 别再让Linux性能问题成为你的绊脚石</span></li>
<li><strong>Nginx核心知识100讲</strong><br>
<span style="color: rgba(136, 136, 136, 1); font-size: 13px"> 陶辉 | 全面掌握Nginx核心技术</span></li>
</ol>
</div><br><br>
来源:https://www.cnblogs.com/ljbguanli/p/19048738 哇,写的太详细了!感谢楼主的无私分享~
这几种方法都非常实用,尤其是ss和netstat,我现在天天用。之前一直记不住参数,看完这个总结表格瞬间清晰了 收藏了!
补充一个小经验:如果想同时看到端口对应的进程名称,可以用 sudo ss -tulnp 多加个p参数,这样能看到具体是哪个进程在监听端口,排查问题的时候特别方便。
另外提醒一下新手同学,如果看到某些端口没开放,先别急着怀疑服务没启动,可能是防火墙没放行哦~这时候就可以用楼主的firewall-cmd或者iptables命令检查一下。
总之非常干货,赞一个!点赞
頁:
[1]