本篇文章將介紹一下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
<?xml version="1.0" encoding="utf-8"?>
<zone>
<short>Work</short>
<description>For use in work areas. You mostly trust the other computers on networks to not harm your computer. Only selected incoming connections are accepted.</description>
</zone>
[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
<?xml version="1.0" encoding="utf-8"?>
<zone>
<short>Work</short>
<description>For use in work areas. You mostly trust the other computers on networks to not harm your computer. Only selected incoming connections are accepted.</description>
<service name="ssh"/>
</zone>
[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)