Linux中的火墙策略优化(iptables,firewalld)
一、火墻介紹
1.netfilter
2.iptables
3.iptables | firewalld
二、火墻管理工具切換
在rhel8中默認使用的是firewalldfirewalld----->iptablesdnf install iptables-services -y systemctl stop firewalld systemctl disable firewalld systemctl mask firewalld systemctl enable --now iptablesiptales -------> fiewalld dnf install firewalld -y systemctl stop iptables systemctl disable iptables systemctl mask iptables systemctl enable --now firewalld三、iptables 的使用
#火墻策略的永久保存# /etc/sysconfig/iptables ##iptables 策略記錄文件永久保存策略 iptales-save > /etc/sysconfig/iptables service iptables save四、火墻策略
| input | ##輸入 |
| output | 輸出 |
| forward | 轉發 |
| postrouting | 路由之后 |
| prerouting | 路由之前 |
| filter | 經過本機內核的數據(input output forward) |
| nat | 不經過內核的數據(postrouting,prerouting,input,output) |
| mangle | 當filter和nat表不夠用時使用(input output forward ,postrouting,prerouting,) |
| -t | 指定表名稱 |
| -n | 不做解析 |
| -L | 查看 |
| -A | 添加策略 |
| -p | 協議 |
| --dport | 目的地端口 |
| -s | 來源 |
| -j | 動作 |
| ACCEPT | 允許 |
| DROP | 丟棄 |
| REJECT | 拒絕 |
| SNAT | 源地址轉換 |
| DNAT | 目的地地址轉換 |
| -N | 新建鏈 |
| -E | 更改鏈地址 |
| -X | 刪除鏈 |
| -D | 刪除規則 |
| -I | 插入規則 |
| -R | 更改規則 |
| -P | 更改默認規則 |
| RELATED??? | 建立過連接的 |
| ESTABLISHED | 正在連接的 |
| NEW | 新的 |
SNAT:局域網共享一個公網IP接入lnternel,好處如下
1、保護內網用戶安全,因為公網地址總有一些人惡意掃描,而內網地址在公網沒有路由所以無法被掃描,能被掃描的只有防火墻這一臺,這樣就減少了被攻擊的可能。
2、Ipv4地址匱乏,很多公司只有一個ipv4地址,但是卻有幾百個用戶需要上網,這個時候就需要使用SNAT。
3、省錢,公網地址付費,使用SNAT只需要一個公網ip就可以滿足幾百人同時上網。
DNAT:向internel發布內網服務器
在內網中有服務器,如果想讓公網用戶訪問有有兩種方法。
1.配置雙網卡,一網卡對內,一網卡對外;一般是高訪問量的web服務器,為了避免占用網關的流量才這樣做,使用不是很廣泛
2.內網web服務器,或是ftp服務器,為了用戶在公網也可以訪問,有不想買公網ip地址,采用DNAT方案。
?
SNAT
實驗環境:
server:雙網卡? 172 網段 和1 網段
?client:單網卡1.1.1段的
測試:
?
DNAT:
實驗環境:網卡配置和上步實驗相同
iptables -t nat -A PREROUTING -i enp1s0 -j DNAT --to-dest 1.1.1.201測試:
使用真機ping 1網段
?
問題: 如果實現不了,試試重啟一下網卡。
firewalld
## 1 firewalld的開啟 ## systemctl stop iptables systemctl disable iptables systemctl mask iptables systemctl unmask firewalld systemctl enable --now firewalld?
| trusted??? | 接受所有的網絡連接 |
| home | 用于家庭網絡,允許接受ssh mdns ipp-client samba-client dhcp-client |
| work | 工作網絡 ssh ipp-client dhcp-client |
| public | 公共網絡 ssh dhcp-client |
| dmz | 軍級網絡 ssh |
| block??? | 拒絕所有 |
| drop | 丟棄?? ?所有數據全部丟棄無任何回復 |
| internal | 內部網絡 ssh mdns ipp-client samba-client dhcp-client |
| external | ipv4網絡地址偽裝轉發 sshd |
| /etc/firewalld | 火墻配置目錄 |
| /lib/firewalld | 火墻模塊目錄 |
firewalld的管理命令
firewall-cmd --state ##查看火墻狀態 firewall-cmd --get-active-zones ##查看當前火墻中生效的域 firewall-cmd --get-default-zone ##查看默認域 firewall-cmd --list-all ##查看默認域中的火墻策略 firewall-cmd --list-all --zone=work ##查看指定域的火墻策略 firewall-cmd --set-default-zone=trusted ##設定默認域firewall-cmd --get-services ##查看所有可以設定的服務 firewall-cmd --permanent --remove-service=cockpit ##移除服務 firewall-cmd --reload firewall-cmd --permanent --add-source=172.25.254.0/24 --zone=block ##指定數據來源訪問指定域 firewall-cmd --reload firewall-cmd --permanent --remove-source=172.25.254.0/24 --zone=block ##刪除自定域中的數據來源firewall-cmd --permanent --remove-interface=ens224 --zone=public ##刪除指定域的網絡接口 firewall-cmd --permanent --add-interface=ens224 --zone=block ##添加指定域的網絡接口 firewall-cmd --permanent --change-interface=ens224 --zone=public ##更改網絡接口到指定域firewalld的高級規則
實驗時要把http加到火墻服務里面 firewall-cmd --direct --get-all-rules ##查看高級規則 firewall-cmd --direct --add-rule ipv4 filter INPUT 1 ! -s 172.25.254.1 -p tcp --dport 80 -j REJECT #不能直接寫允許,所以拒絕所有人的形式允許172.25.254.1一個 !除了 curl http://172.25.254.101?測試:只允許172.25.254.1訪問,其他都拒絕
?
?
?
firewalld中的NAT
SNAT firewall-cmd --permanent --add-masquerade firewall-cmd --reloadDNAT firewall-cmd --permanent --add-forward-port=port=22:proto=tcp:toaddr=1.1.1.201 firewall-cmd --reload測試方式:和iptables 一樣
總結
以上是生活随笔為你收集整理的Linux中的火墙策略优化(iptables,firewalld)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 网络文件系统(samba、nfs、isc
- 下一篇: shell 中常用到的基础命令