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
- 查看当前规则: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
发表评论