目錄
iptables與firewalld服務
iptables的三表五鏈
iptables的安裝和啟用
iptables命令的格式及常用參數
命令格式
常用參數
編寫規則示例
firewalld的域
firewalld的啟用
firewalld-cmd命令的常用參數
firewalld的高級規則
firewalld的地址偽裝與端口轉發
iptables與firewalld服務
kernel space通過netfilter來管理火墻的策略
而iptables域firewalld是netfilter的管理工具
netfilter這個內核網絡棧過濾框架的使用需要通過iptables或nftables來進行
與netfilter進行交互工具常用種類
iptables服務使用iptables交互,管理手段豐富,配置比較復雜。
firewalld服務使用nftables交互 ,配置類似windows火墻,功能模塊度高,使用簡單。
iptables的三表五鏈
iptables使用基于表的規則集
常用的有三個表
五條鏈chain
每個表控制的鏈表?
iptables的安裝和啟用
dnf install iptables-nft-services.noarch -y
systemctl disable --now firewalld.service
systemctl mask firewalld.service
systemctl enable --now iptables.service
systemctl status iptables.service
iptables命令的格式及常用參數
命令格式
iptables [-t 表名] 管理選項 [鏈名] [匹配條件] [-j 控制類型]
tips:
不指定表名時,默認指filter表
不指定鏈名時,默認指表內的所有鏈
除非設置鏈的默認策略,否則必須指定匹配條件
控制類型使用大寫字母,其余均為小寫?
常用參數
指定與匹配條件
常用的管理選項
常用的控制類型
編寫規則示例
查看iptables策略
iptables -nL
iptables -t nat -nL
清空指定表中的策略?
iptables -F -t filter
#-F清空的策略是臨時的,iptables的規則默認保存在內存中,如果需要永久保存需要把策略以字符的形式寫入到配置文件/etc/sysconfig/iptables中
systemctl restart iptables.service
iptables -nL
cat /etc/sysconfig/iptables #查看配置文件中以文字形式保存的策略
iptables -F
service iptables save #將當前表中的策略以文字形式保存在配置文件中
cat /etc/sysconfig/iptables
iptables -F
?添加規則,禁止主機訪問,未禁止的能訪問
iptables -t filter -A INPUT -p tcp --dport 80 -s 192.168.44.30 -j REJECT
iptables -nL
測試
?在編寫規則“ ! ”為取反
iptables -t filter -A INPUT -p tcp --dport 80 ! -s 192.168.81.10 -j REJECT
測試
允許本機回環接口訪問
iptables -t filter -A INPUT -i lo -j ACCEPT
?插入規則到鏈中
iptables -t filter -I INPUT 1 -p tcp --dport 80 -s 192.168.81.10 -j REJECT
刪除表中指定鏈的規則
iptables -t filter -D INPUT 2
?修改表中指定鏈的規則
iptables -t filter -R INPUT 1 -p tcp --dport 80 ! -s 192.168.44.30 -j REJECT
在指定表中創建一個新的鏈
iptables -N fjw
重命名用戶自定義鏈中的名稱
iptables -E fjw FJW
刪除用戶自定義鏈
iptables -X FJW
snat地址轉換
第一步,先給與不同網段的服務器的測試機添加路由
第二步,打開使用iptables火墻的服務器的內核路由功能
第三步,在nat表中編寫規則
測試
snat地址轉換
iptables -t nat -A PREROUTING -i ens160 -j DNAT --to-dest 192.168.44.30
測試
firewalld的域
firewalld采用基于域的規則集
firewalld的啟用
firewalld默認是開啟
由于做完iptables的實驗要,重新開啟
systemctl disable --now iptables.service
systemctl enable --now firewalld.service
firewalld-cmd命令的常用參數
firewall-cmd --reload ? ?#添加端口轉發后+permanent參數要reload后才能生效不然不生效
firewall-cmd --get-default-zone
firewall-cmd --set-default-zone=trusted
firewall-cmd --change-interface=ens192
firewall-cmd --get-zones
firewall-cmd --get-active-zones
firewall-cmd --get-services
cd /lib/firewalld/services/
firewall-cmd --list-all
firewall-cmd --list-all-zones
firewall-cmd --add-source=192.168.44.30/24 --zone=trusted
firewall-cmd --get-active-zones
firewall-cmd --remove-source=192.168.44.30/24 --zone=trusted
firewall-cmd --add-service=http
firewall-cmd --add-port=80/tcp
firewalld的高級規則
vim /etc/firewalld/firewalld.conf
systemctl restart firewalld.service
firewall-cmd --direct --add-rule ipv4 filter INPUT 1 -p tcp --dport 80 ! -s 192.168.81.10 -j ACCEPT
firewall-cmd --direct --get-all-rules
firewalld的地址偽裝與端口轉發
要想開啟火墻功能的服務器充當路由器就要開啟地址偽裝
firewall-cmd --permanent --add-masquerade
firewall-cmd --reload
端口轉發
firewall-cmd --permanent --add-forward-port=port=22:proto=tcp:toport=22:toaddr=192.168.44.30
firewall-cmd --reload #添加端口轉發后+permanent參數要reload后才能生效不然不生效
測試?