显示linux操作系统当前打开的端口(port)

本文介绍如何使用lsof-i命令查看Linux系统中所有开放的端口及其对应的进程。通过该命令,可以详细了解到每个端口上的监听状态和连接情况,对于系统管理和故障排查具有重要意义。

参考自:

https://access.redhat.com/solutions/1987

How do I list the open ports on my system and the process that owns them?

 

使用lsof -i命令即可:
 

[root@abc ~]# lsof -i
COMMAND     PID      USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
rpcbind    1644       rpc    6u  IPv4  11565      0t0  UDP *:sunrpc 
rpcbind    1644       rpc    7u  IPv4  11567      0t0  UDP *:971 
rpcbind    1644       rpc    8u  IPv4  11568      0t0  TCP *:sunrpc (LISTEN)
rpcbind    1644       rpc    9u  IPv6  11570      0t0  UDP *:sunrpc 
rpcbind    1644       rpc   10u  IPv6  11572      0t0  UDP *:971 
rpcbind    1644       rpc   11u  IPv6  11573      0t0  TCP *:sunrpc (LISTEN)
rpc.statd  1701   rpcuser    7u  IPv4  11839      0t0  UDP *:48976 
rpc.statd  1701   rpcuser    8u  IPv4  11843      0t0  TCP *:63124 (LISTEN)
rpc.statd  1701   rpcuser    9u  IPv6  11847      0t0  UDP *:48394 
rpc.statd  1701   rpcuser   10u  IPv6  11851      0t0  TCP *:57162 (LISTEN)
rpc.statd  1701   rpcuser   59u  IPv4  11834      0t0  UDP localhost:659 
cupsd      1746      root    6u  IPv6  12130      0t0  TCP localhost:ipp (LISTEN)
cupsd      1746      root    7u  IPv4  12131      0t0  TCP localhost:ipp (LISTEN)
cupsd      1746      root    9u  IPv4  12134      0t0  UDP *:ipp 
sshd       1937      root    3u  IPv4  12699      0t0  TCP *:ssh (LISTEN)
sshd       1937      root    4u  IPv6  12701      0t0  TCP *:ssh (LISTEN)
master     2016      root   12u  IPv4  12911      0t0  TCP localhost:smtp (LISTEN)
master     2016      root   13u  IPv6  12913      0t0  TCP localhost:smtp (LISTEN)
postgres   2072 highgo432    3u  IPv4  13145      0t0  TCP *:postgres (LISTEN)
postgres   2072 highgo432    4u  IPv6  13146      0t0  TCP *:postgres (LISTEN)
postgres   2072 highgo432    8u  IPv6  13161      0t0  UDP localhost:59722->localhost:59722 
postgres   2074 highgo432    8u  IPv6  13161      0t0  UDP localhost:59722->localhost:59722 
postgres   2075 highgo432    8u  IPv6  13161      0t0  UDP localhost:59722->localhost:59722 
postgres   2076 highgo432    8u  IPv6  13161      0t0  UDP localhost:59722->localhost:59722 
postgres   2077 highgo432    8u  IPv6  13161      0t0  UDP localhost:59722->localhost:59722 
postgres   2078 highgo432    8u  IPv6  13161      0t0  UDP localhost:59722->localhost:59722 
postgres   4877 highgo475    3u  IPv4  18837      0t0  TCP *:5899 (LISTEN)
postgres   4877 highgo475    4u  IPv6  18838      0t0  TCP *:5899 (LISTEN)
postgres   4877 highgo475    8u  IPv6  18853      0t0  UDP localhost:60657->localhost:60657 
postgres   4879 highgo475    8u  IPv6  18853      0t0  UDP localhost:60657->localhost:60657 
postgres   4880 highgo475    8u  IPv6  18853      0t0  UDP localhost:60657->localhost:60657 
postgres   4881 highgo475    8u  IPv6  18853      0t0  UDP localhost:60657->localhost:60657 
postgres   4882 highgo475    8u  IPv6  18853      0t0  UDP localhost:60657->localhost:60657 
postgres   4883 highgo475    8u  IPv6  18853      0t0  UDP localhost:60657->localhost:60657 
sshd      14619      root    3r  IPv4  46824      0t0  TCP abc:ssh->192.168.80.1:64586 (ESTABLISHED)
[root@abc ~]# 

 

### 查看 Linux 系统中开放端口的方法 在 Linux 操作系统中,有多种方法可以查看当前系统上开放的端口。这些方法包括使用防火墙管理工具(如 `firewalld`、`iptables` 和 `ufw`)以及网络工具(如 `netstat` 和 `ss`)。以下是具体的命令和说明: #### 使用 `firewalld` 查看开放端口 如果系统正在使用 `firewalld` 作为防火墙管理工具,可以通过以下命令查看开放的端口: ```bash firewall-cmd --zone=public --list-ports ``` 该命令会列出当前 `public` 区域中所有开放的端口[^3]。 #### 使用 `iptables` 查看规则中的开放端口 对于使用 `iptables` 的系统,可以通过以下命令查看当前的规则并分析哪些端口是开放的: ```bash iptables -L -n | grep ACCEPT ``` 这将显示所有允许通过的端口及对应的协议[^2]。 #### 使用 `netstat` 或 `ss` 查看开放端口 `netstat` 和 `ss` 是常用的网络工具,用于查看当前系统的网络连接状态和开放端口。 1. **使用 `netstat`**: ```bash netstat -ntulp | grep LISTEN ``` 该命令会列出所有处于监听状态的 TCP 和 UDP 端口,并显示对应的进程信息[^1]。 2. **使用 `ss`**(推荐,性能更优): ```bash ss -ntulp | grep LISTEN ``` 这是一个更现代的替代方案,功能与 `netstat` 类似,但执行速度更快[^1]。 #### 检查特定端口是否开放 如果需要检查某个特定端口是否开放,可以使用以下命令: ```bash firewall-cmd --query-port=666/tcp ``` 如果端口已开放,命令会返回 `yes`;否则返回 `no`[^3]。 #### 使用 `lsof` 查看开放端口 `lsof` 是一个强大的工具,可以列出打开的文件和网络连接: ```bash lsof -i -P -n | grep LISTEN ``` 此命令会显示所有监听中的端口及其关联的进程[^4]。 --- ### 注意事项 不同 Linux 发行版可能使用不同的防火墙管理工具或默认配置。例如,Ubuntu 默认使用 `ufw`,而 CentOS 和 Fedora 更倾向于使用 `firewalld`。因此,在选择工具时需根据实际环境进行调整。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值