跳转到主内容
新派编程网 - 新潮编程理念,全新技术课堂!

linux怎么关闭指定端口

如何关闭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 Docker Desktop(linux) 当前 Docker 最新稳定版本之一,主要针对稳定性和兼容性进行了修复优化,适合生产环境与日常开发使用。该版本继续强化 AI 开发支持、容器日志管理以及 Docker Engine 的安全能力,对 Windows/macOS/Linux 平台兼容性进行了进一步优化。 下载 查看当前规则:
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
)。 如果使用其他防火墙软件,请查阅其文档以了解具体关闭端口的步骤。

相关文章