关于CentOS7的firewalld

CentOS6 → 7 升级后的一个重大变化之一是默认的访问控制方式从iptables变为了firewalld。当然,并不意味着不能使用iptables。虽然会有一些麻烦,但一旦安装好,还是可以使用iptables的。此外,在使用iptables的情况下,应该将firewalld禁用。(因为同时使用两者没有意义)

据说在CentOS7中,默认情况下firewalld是启用的,但在我使用“Opscode centos-7.0”的vagrant Box时,起初却是禁用的。
所以,我进行了一些关于firewalld启动、停止、添加等操作的调查。

确认是否已经设置为自动启动。

# systemctl is-enabled firewalld

设为自动启动

# systemctl enable firewalld.service

启动

# systemctl start firewalld.service

确认清单

# firewall-cmd --list-all
public (default, active)
  interfaces: enp0s3
  sources:
  services: dhcpv6-client ssh
  ports:
  masquerade: no
  forward-ports:
  icmp-blocks:
  rich rules:

添加规则

# firewall-cmd --zone=public --add-port=8080/tcp --permanent
# firewall-cmd --zone=public --add-service=http --permanent

— permanent 选项是用来在操作系统重新启动时保持生效的。但是,更改不会立即生效,您需要使用以下步骤进行重新加载。如果不使用 permanent 选项,则即使不重新加载也会立即生效,但是在操作系统重新启动后会失效。

上述示例是在服务名称上添加常见的端口号 80 的“http”服务。与使用 firewall-cmd –zone=public –add-port=80/tcp –permanent 指定端口进行添加是相同的。

刷新

# firewall-cmd --reload
# firewall-cmd --list-all
public (default, active)
  interfaces: enp0s3
  sources:
  services: dhcpv6-client http ssh
  ports: 8080/tcp
  masquerade: no
  forward-ports:
  icmp-blocks:
  rich rules:

删除规则

# firewall-cmd --zone=public --remove-port=8080/tcp --permanent
# firewall-cmd --zone=public --remove-service=http --permanent

为了在操作系统重新启动时也能生效,不要忘记加上“–permanent”选项。