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