如何检查并保护Linux系统中的开放端口

如何检查并保护Linux系统中的开放端口

理解开放端口对管理应用程序和保障系统安全至关重要,开放端口既支持服务间通信,也可能使系统面临安全威胁。本文将介绍如何通过防火墙规则和最佳实践来检查并保护Linux系统中的开放端口。

端口号范围0 到 65535,分为三类:

  • 知名端口(Well-Known Ports,0-1023):预留给系统级通用服务。
  • 注册端口(Registered Ports,1024-49151):分配给用户级应用程序或服务。
  • 动态/私有端口(Dynamic/Private Ports,49152-65535):由操作系统临时分配给客户端程序(如浏览器访问网站时),通信结束后立即释放。

最常用的端口号列表如下

端口号 服务 功能
20, 21 FTP 文件传输
22 SSH 安全远程登录
25 SMTP 发送电子邮件
53 DNS 域名解析
80 HTTP 提供网页
110 POP3 检索电子邮件
123 NTP 时间同步
143 IMAP 管理电子邮件
161, 162 SNMP 网络监控
443 HTTPS 保护网页
631 CPUS 打印机管理和联网
3306 MySQL 数据库管理
3389 RDP 远程桌面访问
5432 PostgreSQL 数据库管理
5900 VNC 远程桌面访问
8080 HTTP Alternate Web 服务器或代理

前面作为端口号的补充知识,下面开始进入正文:

1.检查Linux系统中的开放端口

可通过多种方式查看系统中的开放端口及监听状态:

1.1使用netstat命令

netstat可显示活动端口及其关联服务:

1
sudo netstat -tulnp

参数说明:

  • -t:显示TCP端口
  • -u:显示UDP端口
  • -l:仅显示监听端口
  • -n:以数字形式显示地址(不解析主机名)
  • -p:显示进程ID和名称

示例输出:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
[root@localhost ~]# netstat -tulnp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 3517/mariadbd
tcp 0 0 0.0.0.0:7502 0.0.0.0:* LISTEN 310063/sshd: /usr/s
tcp 0 0 0.0.0.0:139 0.0.0.0:* LISTEN 313627/smbd
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 3564/httpd
tcp 0 0 0.0.0.0:445 0.0.0.0:* LISTEN 313627/smbd
tcp 0 0 127.0.0.1:6010 0.0.0.0:* LISTEN 1234080/sshd: root@
tcp 0 0 127.0.0.1:199 0.0.0.0:* LISTEN 1262/snmpd
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 313639/cupsd
tcp6 0 0 ::1:631 :::* LISTEN 313639/cupsd
tcp6 0 0 :::3306 :::* LISTEN 3517/mariadbd
tcp6 0 0 :::3128 :::* LISTEN 1225182/(squid-1)
tcp6 0 0 ::1:6010 :::* LISTEN 1234080/sshd: root@
tcp6 0 0 :::7502 :::* LISTEN 310063/sshd: /usr/s
tcp6 0 0 :::139 :::* LISTEN 313627/smbd
tcp6 0 0 :::445 :::* LISTEN 313627/smbd
udp 0 0 0.0.0.0:5353 0.0.0.0:* 313569/avahi-daemon
udp 0 0 0.0.0.0:47221 0.0.0.0:* 313569/avahi-daemon
udp 0 0 0.0.0.0:161 0.0.0.0:* 1262/snmpd
udp 0 0 0.0.0.0:32947 0.0.0.0:* 1225182/(squid-1)
udp 0 0 127.0.0.1:323 0.0.0.0:* 313603/chronyd
udp6 0 0 :::5353 :::* 313569/avahi-daemon
udp6 0 0 :::51309 :::* 313569/avahi-daemon
udp6 0 0 :::36658 :::* 1225182/(squid-1)
udp6 0 0 ::1:323 :::* 313603/chronyd
[root@localhost ~]#

定期检查异常开放端口以发现未授权访问:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
sudo netstat -tulnp | grep LISTEN
[root@localhost ~]# netstat -tulnp | grep LISTEN
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 3517/mariadbd
tcp 0 0 0.0.0.0:7502 0.0.0.0:* LISTEN 310063/sshd: /usr/s
tcp 0 0 0.0.0.0:139 0.0.0.0:* LISTEN 313627/smbd
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 3564/httpd
tcp 0 0 0.0.0.0:445 0.0.0.0:* LISTEN 313627/smbd
tcp 0 0 127.0.0.1:6010 0.0.0.0:* LISTEN 1234080/sshd: root@
tcp 0 0 127.0.0.1:199 0.0.0.0:* LISTEN 1262/snmpd
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 313639/cupsd
tcp6 0 0 ::1:631 :::* LISTEN 313639/cupsd
tcp6 0 0 :::3306 :::* LISTEN 3517/mariadbd
tcp6 0 0 :::3128 :::* LISTEN 1225182/(squid-1)
tcp6 0 0 ::1:6010 :::* LISTEN 1234080/sshd: root@
tcp6 0 0 :::7502 :::* LISTEN 310063/sshd: /usr/s
tcp6 0 0 :::139 :::* LISTEN 313627/smbd
tcp6 0 0 :::445 :::* LISTEN 313627/smbd
[root@localhost ~]#
1.2使用ss命令(推荐)

ss是比netstat更高效的现代替代工具:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
sudo ss -tulnp
[root@localhost ~]# ss -tulnp
Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
udp UNCONN 0 0 0.0.0.0:5353 0.0.0.0:* users:(("avahi-daemon",pid=313569,fd=12))
udp UNCONN 0 0 0.0.0.0:47221 0.0.0.0:* users:(("avahi-daemon",pid=313569,fd=14))
udp UNCONN 0 0 0.0.0.0:161 0.0.0.0:* users:(("snmpd",pid=1262,fd=6))
udp UNCONN 0 0 0.0.0.0:32947 0.0.0.0:* users:(("squid",pid=1225182,fd=8))
udp UNCONN 0 0 127.0.0.1:323 0.0.0.0:* users:(("chronyd",pid=313603,fd=5))
udp UNCONN 0 0 [::]:5353 [::]:* users:(("avahi-daemon",pid=313569,fd=13))
udp UNCONN 0 0 [::]:51309 [::]:* users:(("avahi-daemon",pid=313569,fd=15))
udp UNCONN 0 0 *:36658 *:* users:(("squid",pid=1225182,fd=7))
udp UNCONN 0 0 [::1]:323 [::]:* users:(("chronyd",pid=313603,fd=6))
tcp LISTEN 0 80 0.0.0.0:3306 0.0.0.0:* users:(("mariadbd",pid=3517,fd=18))
tcp LISTEN 0 128 0.0.0.0:7502 0.0.0.0:* users:(("sshd",pid=310063,fd=3))
tcp LISTEN 0 50 0.0.0.0:139 0.0.0.0:* users:(("smbd",pid=313627,fd=31))
tcp LISTEN 0 511 0.0.0.0:80 0.0.0.0:* users:(("httpd",pid=1230182,fd=3),("httpd",pid=1230126,fd=3),("httpd",pid=1230095,fd=3),("httpd",pid=3564,fd=3))
tcp LISTEN 0 50 0.0.0.0:445 0.0.0.0:* users:(("smbd",pid=313627,fd=30))
tcp LISTEN 0 128 127.0.0.1:6010 0.0.0.0:* users:(("sshd",pid=1234080,fd=9))
tcp LISTEN 0 4096 127.0.0.1:199 0.0.0.0:* users:(("snmpd",pid=1262,fd=7))
tcp LISTEN 0 4096 127.0.0.1:631 0.0.0.0:* users:(("cupsd",pid=313639,fd=8))
tcp LISTEN 0 4096 [::1]:631 [::]:* users:(("cupsd",pid=313639,fd=7))
tcp LISTEN 0 80 [::]:3306 [::]:* users:(("mariadbd",pid=3517,fd=19))
tcp LISTEN 0 4096 *:3128 *:* users:(("squid",pid=1225182,fd=11))
tcp LISTEN 0 128 [::1]:6010 [::]:* users:(("sshd",pid=1234080,fd=8))
tcp LISTEN 0 128 [::]:7502 [::]:* users:(("sshd",pid=310063,fd=4))
tcp LISTEN 0 50 [::]:139 [::]:* users:(("smbd",pid=313627,fd=29))
tcp LISTEN 0 50 [::]:445 [::]:* users:(("smbd",pid=313627,fd=28))
[root@localhost ~]#

图片

输出格式相似,但性能更优。

1.3.使用lsof命令

lsof可列出包括网络套接字在内的已打开文件:

1
lsof -i -P -n

参数说明:

  • -i:显示网络连接
  • -P:禁止将端口号解析为服务名(大写)
  • -n:禁止将IP地址解析为主机名

图片

1.4.使用nmap(远程扫描)

检查远程系统的开放端口:

1
nmap -p- <IP-ADDRESS>
  • -p-:扫描全部65535个端口

*2.保护Linux系统中的开放端口*

2.1关闭非必要端口

若服务无需运行,立即停用:

1
2
sudo systemctl stop <服务名>
sudo systemctl disable <服务名>
2.2配置防火墙

防火墙通过允许/阻断端口控制流量。

# 使用ufw(适用于Ubuntu/Debian)

允许特定端口:

1
sudo ufw allow 22/tcp

拒绝特定端口:

1
sudo ufw deny 23/tcp

启用防火墙:

1
sudo ufw enable

# 使用firewalld(适用于RHEL系列系统)

允许端口:

1
2
sudo firewall-cmd --add-port=22/tcp --permanent
sudo firewall-cmd --reload

阻断端口:

1
2
sudo firewall-cmd --remove-port=23/tcp --permanent
sudo firewall-cmd --reload

# 使用iptables

仅允许特定IP访问SSH:

1
sudo iptables -A INPUT -p tcp --dport 22 -s <允许的IP> -j ACCEPT

阻断所有非SSH入站流量:

1
2
sudo iptables -P INPUT DROP
sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT

保存规则:

1
sudo iptables-save > /etc/iptables.rules

3.实施端口敲门(Port Knocking)

端口敲门(Port Knocking) 是一种隐蔽的防火墙控制技术,通过预先设定的端口连接尝试序列,动态开放默认关闭的防火墙端口。

关于端口敲门的相关技术,下一节具体描述。

4.小结

检查并保护开放端检查并保护开放端口是 Linux 系统管理DevOps 中的基础安全实践。通过 定期监控端口状态合理配置防火墙规则,可以有效降低系统遭受网络攻击的风险。