欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

iptables添加防火墙限制,允许某ip或网段访问此端口

程序员文章站 2022-06-04 19:36:33
...
1、在tcp协议中,禁止所有的ip访问本机的80端口
[[email protected] ~ 17:11:28]# iptables -I INPUT -p tcp --dport 80 -j DROP
[[email protected] ~ 17:13:54]#  service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[  OK  ]
[[email protected] ~ 17:13:58]# service iptables restart
iptables: Setting chains to policy ACCEPT: filter          [  OK  ]
iptables: Flushing firewall rules:                         [  OK  ]
iptables: Unloading modules:                               [  OK  ]
iptables: Applying firewall rules:                         [  OK  ]
[[email protected] ~ 17:14:01]# 
2、只允许192.168.200.199访问本机的80端口
[[email protected] ~ 17:15:35]# iptables -I INPUT -s 192.168.200.199 -p tcp --dport 80 -j ACCEPT
[[email protected] ~ 17:16:05]#  service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[  OK  ]
[[email protected] ~ 17:16:07]# service iptables restart
iptables: Setting chains to policy ACCEPT: filter          [  OK  ]
iptables: Flushing firewall rules:                         [  OK  ]
iptables: Unloading modules:                               [  OK  ]
iptables: Applying firewall rules:                         [  OK  ]
[[email protected] ~ 17:16:10]# 
如要配置允许某个网段的ip进行访问:
iptables -I INPUT -s 192.168.200.0/24 -p tcp --dport 80 -j ACCEPT