Linux禁用端口

Linux防火墙Iptable如何设置只允许某个ip访问80端口,只允许特定ip访问某端口?参考下面命令,只允许46.166.150.22访问本机的80端口。如果要设置其他ip或端口,改改即可。

iptables -I INPUT -p TCP --dport 80 -j DROP
iptables -I INPUT -s 46.166.150.22 -p TCP --dport 80 -j ACCEPT

在root用户下执行上面2行命令后,重启iptables, service iptables restart

查看iptables是否生效:

[root@www.ctohome.com]# iptables -L
Chain INPUT (policy ACCEPT)
target           prot opt source               destination        
ACCEPT     tcp  --  46.166.150.22    anywhere            tcp dpt:http
DROP         tcp  --  anywhere             anywhere            tcp dpt:http

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination        

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination        

 上面命令是针对整个服务器(全部ip)禁止80端口,如果只是需要禁止服务器上某个ip地址的80端口,怎么办?

下面的命令是只允许来自174.140.3.190的ip访问服务器上216.99.1.216的80端口

iptables -A FORWARD -s 174.140.3.190 -d 216.99.1.216 -p tcp -m tcp --dport 80 -j ACCEPT
iptables -A FORWARD -d 216.99.1.216 -p tcp -m tcp --dport 80 -j DROP

 

 

 

开放端口

永久开放2个端口
firewall-cmd --permanent --zone=public --add-port=80/tcp

firewall-cmd --permanent --zone=public --add-port=443/tcp

firewall-cmd --reload

查看开放的端口号

firewall-cmd --list-ports

 

 

 

 

systemctl status firewalld

posted on 2026-10-30 16:28  泽一年  阅读(4)  评论(0)    收藏  举报

导航