如何关闭 linux 中的指定端口
在 Linux 系统中,我们可以通过防火墙规则来关闭指定端口。常用的防火墙有 UFW、iptables 和 firewalld。
使用 UFW
-
安装 UFW:sudo apt install ufw
拒绝端口:sudo ufw deny PORT(替换 PORT 为要关闭的端口号)
使用 iptables
-
查看当前规则:sudo iptables -L
添加拒绝规则:
sudo iptables -I INPUT -p tcp –dport PORT -j DROP(TCP 端口)
sudo iptables -I INPUT -p udp –dport PORT -j DROP(UDP 端口)
使用 firewalld
-
查看当前规则:sudo firewall-cmd –list-all
添加拒绝规则:sudo firewall-cmd –add-port=PORT/protocol –permanent(替换 PORT 和 protocol 为要关闭的端口号和协议)
示例
以下命令关闭端口 80:
sudo ufw deny 80 sudo iptables -I INPUT -p tcp --dport 80 -j DROP sudo firewall-cmd --add-port=80/tcp --permanent
登录后复制
注意:
根据您的系统和防火墙软件,实际命令可能略有不同。
始终记得在更改防火墙规则后重新加载规则(例如,sudo ufw reload、sudo iptables-restore 或 sudo firewall-cmd –reload)。
如果使用其他防火墙软件,请查阅其文档以了解具体关闭端口的步骤。






