日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > linux >内容正文

linux

Linux中的火墙策略优化(iptables,firewalld)

發(fā)布時間:2024/9/3 linux 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Linux中的火墙策略优化(iptables,firewalld) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

一、火墻介紹

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

四、火墻策略

默認策略中的5條鏈
input##輸入
output輸出
forward轉(zhuǎn)發(fā)
postrouting路由之后
prerouting路由之前
默認的3張表
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,)

iptables命令
-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ī)則
數(shù)據(jù)包狀態(tài)
RELATED???建立過連接的
ESTABLISHED正在連接的
NEW新的
iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT ## 所有建立過連接的或者正在連接的數(shù)據(jù)包都被接受 iptables -A INPUT -m state --state NEW -i lo -j ACCEPT ##屬于自身回環(huán)的新數(shù)據(jù)包都被接受 iptables -A INPUT -m state --state NEW -p tcp --dport 80 -j ACCEPT iptables -A INPUT -m state --state NEW -p tcp --dport 443 -j ACCEPT iptables -A INPUT -m state --state NEW -p tcp --dport 53 -j ACCEPT iptables -A INPUT -m state --state NEW ! -s 192.168.0.10 -p tcp --dport 22 -j ACCEPT ptables -A INPUT -m state --state NEW -j REJECT service iptables save nat表中的dnat snatsnat iptables -t nat -A POSTROUTING -o enp1s0 -j SNAT --to-source 172.25.254.101dnat iptables -t nat -A PREROUTING -i enp1s0 -j DNAT --to-dest 1.1.1.201

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

?

關于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
externalipv4網(wǎng)絡地址偽裝轉(zhuǎn)發(fā) sshd
關于firewalld的設定原理及數(shù)據(jù)存儲
/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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。