在服务器上查看开放端口有多种方法,以下是一些常用的命令和方法:
netstat
命令netstat
是一个网络统计工具,可以显示网络连接、路由表、接口统计等。
netstat -tuln
-t
:显示TCP连接。-u
:显示UDP连接。-l
:仅显示监听套接字。-n
:以数字形式显示地址和端口号。示例输出:
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp6 0 0 :::80 :::* LISTEN
udp 0 0 0.0.0.0:123 0.0.0.0:*
ss
命令ss
是 netstat
的替代品,通常更快且更高效。
ss -tuln
示例输出:
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 5 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 *:80 *:*
lsof
命令lsof
可以列出当前系统打开的文件,包括网络端口。
lsof -i -P -n | grep LISTEN
-i
:显示网络文件。-P
:不将端口号转换为服务名。-n
:不将IP地址转换为主机名。示例输出:
sshd 1234 root 3u IPv4 12345 0t0 TCP *:22 (LISTEN)
nginx 5678 www-data 6u IPv4 23456 0t0 TCP *:80 (LISTEN)
nmap
工具nmap
是一个网络扫描工具,可以用来探测主机上的开放端口。
nmap localhost
示例输出:
Starting Nmap 7.80 ( https://nmap.org ) at 2023-04-01 12:34:56 UTC
Nmap scan report for localhost (127.0.0.1)
Host is up (0.00013s latency).
Not shown: 997 closed ports
PORT STATE SERVICE
22/tcp open ssh
80/tcp open http
sudo
提升权限。net-tools
或 nmap
。sudo apt-get install net-tools
sudo apt-get install nmap
iptables
或 ufw
管理防火墙规则。sudo ufw status
sudo ufw allow 80/tcp
通过以上方法,你可以有效地查看服务器上的开放端口,并根据需要进行相应的管理和调整。
领取专属 10元无门槛券
手把手带您无忧上云