CentOS 7 配置 iptables、firewalld
2022-11-19
iptables、firewalld
iptables默认每个服务都是开启的,需要拒绝才能限制;firewalld默认每个服务都是拒绝的,需要设置才能开放
iptables 服务在 /etc/sysconfig/iptables 中储存配置;
firewalld 将配置储存在 /usr/lib/firewalld/ 和 /etc/firewalld/ 中的各种 XML 文件里
使用 iptables 的时候每一个单独更改意味着清除所有旧有的规则和从 /etc/sysconfig/iptables 里读取所有新的规则;使用 firewalld 却不会再创建任何新的规则;仅仅运行规则中的不同。因此 firewalld 可以在运行时改变设置而不丢失现行配置。
关闭 firewall
systemctl stop firewalld.service # 停止firewall
systemctl disable firewalld.service # 禁止firewall开机启动
配置 iptables
yum install iptables-services -y #安装
1.清除原有filter规则.
iptables -F #清除预设表filter中的所有规则链的规则
iptables -X #清除预设表filter中使用者自定链中的规则
iptables -Z #所有计数器归0
2. 添加自己的规则
iptables -P INPUT ACCEPT #先允许所有,不然有可能会杯具
iptables -A INPUT -i lo -j ACCEPT #允许来自于lo接口的数据包(本地访问)
iptables -A INPUT -p tcp --dport 22 -j ACCEPT #开放22端口
iptables -A INPUT -p tcp --dport 21 -j ACCEPT #开放21端口(FTP)
iptables -A INPUT -p tcp --dport 80 -j ACCEPT #开放80端口(HTTP)
iptables -A INPUT -p tcp --dport 443 -j ACCEPT #开放443端口(HTTPS)
iptables -A INPUT -p icmp --icmp-type 8 -j ACCEPT #允许ping
iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT #允许接受本机请求之后的返回数据 RELATED
iptables -P INPUT DROP #其他入站一律丢弃
iptables -P OUTPUT ACCEPT #所有出站一律绿灯
iptables -P FORWARD DROP #所有转发一律丢弃
3. 其他规则设定
iptables -A INPUT -p tcp -s 45.96.174.68 -j ACCEPT #添加内网ip信任(接受其所有TCP请求)
iptables -P INPUT DROP #过滤所有非以上规则的请求
iptables -I INPUT -s ***.***.***.*** -j DROP #要封停一个IP,使用下面这条命令:
iptables -D INPUT -s ***.***.***.*** -j DROP #要解封一个IP,使用下面这条命令:
4. 保存规则
service iptables save
5. 开启iptables服务
#注册iptables服务
#相当于以前的chkconfig iptables on
systemctl enable iptables.service
#开启服务
systemctl start iptables.service
#查看状态
systemctl status iptables.service