文章目录

  • ​​1.iptables​​
  • ​​2.firewalld​​

1.iptables

  • 步骤:
    (1)iptables有一个全局策略:要么全局DROP,要么全局ACCEPT,所以如果你设定了你需要的防火墙规则,首先先要将所有iptables先accept,接着安装你设定的防火墙规则,最后全局DROP掉即可
    (2)若服务器本身就有防火墙策略
iptables -P INPUT ACCEPT
iptables -F
iptables -A INPUT -s XXXX -j ACCEPT ##目的是为了能够登录到该服务器,后面会做全局DROP
安装你的防火墙规则
iptables -P INPUT DROP
  • iptables命令解释
    将一个源IP限制加入到防火墙策略中
iptables -I INPUT -s 源IP -j ACCEPT

iptables -A INPUT -s 源IP -j ACCEPT

iptables -A INPUT -d 源IP -j ACCEPT


-I和-A的区别是:-I是插入,-A是追加,本质没啥区别

如何限制子网网管或者限制特定IP呢?
iptables -I INPUT -s 源IP -j ACCEPT 限制的是源IP
iptables -I INPUT -s 源IP/16 -j ACCEPT 限制的是网段
  • 将一个目的端口限制加入到防火墙策略中
iptables -I INPUT -p tcp --dport 目的PORT -j ACCEPT
  • 删除一条防火墙规则
首先获取要删除规则的序号
iptables -L -n --line-number

删除行号
iptables -D INPUT 行号

2.firewalld

  • rich rules相关
查询方法:
firewall-cmd --list-all

添加方法:
firewall-cmd --permanent --zone=public --add-rich-rule="rule family="ipv4" source address="192.116.116.116/8" accept"

移除方法:
firewall-cmd --permanent --remove-rich-rule="rule family="ipv4" destination address="192.116.116.116/8" accept"

所有带有--permanent记住要firewall-cmd --reload进行持久化加载,不带该参数的为运行时加载
持久化的文件在/etc/firewalld/zones/public.xml
  • firewalld可以使用iptables的INPUT和OUTPUT方法,方法如下:
查询方法:
firewall-cmd --direct --get-all-rules

添加方法:
firewall-cmd --permanent --direct --sdd-rule ipv4 filter INPUT 1 -s 196.116.116.116/8 -j ACCEPT
添加后:iptables -L -n可以看得到

删除方法:
firewall-cmd --permanent --direct --remove-rule ipv4 filter INPUT 1 -s 196.116.116.116/8 -j ACCEPT

所有带有--permanent记住要firewall-cmd --reload进行持久化加载,不带该参数的为运行时加载
持久化的文件在:/etc/firewalld/direct.xml
  • firewalld还可以单独将限制的端口配置在/etc/firewalld/service目录下,eg:XXX.xml(本身就是持久化的文件)
查询方法:在services可以看到
firewall-cmd --list-all

增加方法
firewall-cmd --permanent --zone=zone --add-service=XXX

删除方法
firewall-cmd --permanent --zone=zone --remove-service=XXX

firewall-cmd --reload
  • 其他说明
    (1)如果软件启动异常,则将masquerade设置为no:firewall-cmd --permanent --zone=public --remove-masquerade
    (2)通过public模式的interfaces是不需要配置的,默认配置的就是有ip的网口
    (3)其他常用命令见:Linux防火墙firewalld安全设置
  • 参考:基于linux下firewalld防火墙的配置,Linux防火墙配置(iptables, firewalld),Linux防火墙firewalld安全设置,​​FirewallD入门手册​​,linux系统中firewalld防火墙管理工具firewall-config(GUI图形用户界面),FirewallD 详解