本篇文章将介绍一下Arch下安装配置firewall防火墙(Centos7.X系列,FirewallD默认直接开启的)。
[root@Centos6 ~]# chkconfig --list | grep iptables #centos6.x系列 iptables默认是开启的
iptables 0:off 1:off 2:on 3:on 4:on 5:on 6:off
################################################################################
[root@Centos7 ~]# systemctl list-unit-files | grep firewalld #可以看到centos下firewall是默认开启的
firewalld.service enabled
[root@Centos7 ~]# firewall-cmd -V #Centos7.X系统FirewallD版本是0.3.9
0.3.9
################################################################################
[root@Arch ~]# systemctl list-unit-files | grep firewalld #Arch没有安装,可以pacman -S firewalld
安装
firewalld.service disabled
[root@Arch ~]# firewall-cmd -V #FirewallD防火墙版本0.3.13
0.3.13
[root@Arch ~]# systemctl start firewalld #开启firewalld防火墙
[root@Arch ~]# firewall-cmd --state #查看FirewallD防火墙状态
running
[root@Arch ~]# systemctl enable firewalld #设置firewald开机启动
Created symlink from /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service to /usr/lib/systemd/system/firewalld.service.
Created symlink from /etc/systemd/system/basic.target.wants/firewalld.service to /usr/lib/systemd/system/firewalld.service.
[root@Arch ~]# systemctl list-unit-files | grep firewalld
firewalld.service enabled
[root@Arch zones]# pwd
/usr/lib/firewalld/zones #firewalld默认配置文件
[root@Arch zones]# pwd
/etc/firewalld/zones #firewalld系统配置文件
[root@Arch ~]# firewall-cmd --get-zones #firewall支持的区域
block dmz drop external home internal public trusted work
[root@Arch ~]# firewall-cmd --get-default-zone #获取默认区域
[root@Arch ~]# firewall-cmd --set-default-zone=public #设置默认区域
[root@Arch ~]# firewall-cmd --zone=public --list-all #区域public支持启用的特性
public (default)
interfaces:
sources:
services: dhcpv6-client ssh
ports: 80/tcp
masquerade: no
forward-ports:
icmp-blocks:
rich rules:
[root@Arch ~]# firewall-cmd --zone=public --add-interface=eno16777736 #将eno16777736 加入public区域
[root@Arch ~]# firewall-cmd --get-zone-of-interface=eno16777736 #查看eno16777736所在区域
[root@Arch ~]# firewall-cmd --zone=public --remove-interface=eno16777736 #将eno16777736接口从public接口中删除
[root@Arch ~]# firewall-cmd --zone=public --list-services #查看public启用那些服务,默认是dhcpv6-client ssh
[root@Arch ~]# firewall-cmd --zone=public --add-port=80/tcp #从public区域增加tcp 80 端口
[root@Arch ~]# firewall-cmd --zone=public --remove-port=80/tcp #从public区域删除tcp 80 端口
[root@Arch ~]# firewall-cmd --zone=dmz --remove-service=ssh #dmz区域删除ssh服务
[root@Arch ~]# firewall-cmd --zone=external --remove-service=ssh #dmz区域删除ssh服务
[root@Arch ~]# firewall-cmd --zone=home --remove-service=dhcpv6-client #home区域删除dhcpv6-client服务
[root@Arch ~]# firewall-cmd --zone=work --remove-service=ssh #work区域删除ssh服务
临时生效和永久生效:
[root@Arch zones]# firewall-cmd --zone=work --add-service=ssh
success
[root@Arch zones]# cat /etc/firewalld/zones/work.xml
Work
For use in work areas. You mostly trust the other computers on networks to not harm your computer. Only selected incoming connections are accepted.
[root@Arch zones]# firewall-cmd --zone=work --remove-service=ssh
success
[root@Arch zones]# firewall-cmd --permanent --zone=work --add-service=ssh
success
[root@Arch zones]# cat /etc/firewalld/zones/work.xml
Work
For use in work areas. You mostly trust the other computers on networks to not harm your computer. Only selected incoming connections are accepted.
[root@Arch zones]#
备注:没有--permanent参数,配置将在firewalld --reload后被恢复,因此建议所有配置均增加 --permanent 参数。
配置演示:
[root@Arch /]# ls /sys/class/net/ #我的虚拟机对外接口是eno16777736,真实服务器可能是enp3s1 eth1类似的
eno16777736 lo
[root@Arch /]# firewall-cmd --permanent --new-zone=personal #增加一个名字叫做personal的区域
[root@Arch ~]# firewall-cmd --permanent --zone=personal --add-service=ssh #在personal区域增加ssh服务
[root@Arch ~]# firewall-cmd --permanent --zone=public --remove-interface=eno16777736 #区域public中将网卡删除
[root@Arch ~]# firewall-cmd --permanent --zone=personal --add-interface=eno16777736 #将网卡添加到personal区中
[root@Arch ~]# firewall-cmd --permanent --zone=personal --add-port=443/tcp #personal增加tcp 443端口
[root@Arch ~]# firewall-cmd --permanent --zone=personal --add-service=http #personal增加http服务支持
[root@Arch ~]# firewall-cmd --set-default=personal
[root@Arch /]# firewall-cmd --reload
Iptables vs Firewalld:
[root@Centos6 ~]# iptables -t filter -A INPUT -p tcp --dport 22 -j ACCEPT #开启22端口允许通过
[root@Centos6 ~]# iptables -t filter -A INPUT -p tcp --dport 22 ! -s 1.1.1.1/32 -j DROP #源IP不是1.1.1.1的,链接到22端口,数据丢弃
####################################################################
[root@Arch ~]# firewall-cmd --permanent --zone=personal --add-port=22/tcp #[root@Arch ~]# firewall-cmd --permanent --zone=personal --add-service=ssh
[root@Arch ~]# firewall-cmd --permanent --zone=personal --add-rich-rule='rule family="ipv4" source address="1.1.1.1/32" service name="ssh" accept' #firewall-cmd --reload生效 (参考:man firewalld.richlanguage)