Centos7 | 防火墻(firewalld)使用ipset管理ip地址的集合

文章目錄

  • 一、firewalld中ipset的用途
    • 1.1 用途
    • 1.2 注意與iptables所用的ipset命令的不同,
    • 1.3 配置詳解
  • 二、firewalld中ipset的操作例子
    • 2.1 新建一個set
    • 2.2 在set中添加ip
    • 2.3 從set中刪除ip
    • 2.4 刪除一個set
    • 2.5 打印一個set的文件路徑
    • 2.6 打印一個set的內容
    • 2.8 判斷一個ip是否存在于set中?
    • 2.9 列出所有的ipsets
    • 2.10 得到所有的默認ipset類型
  • 三、firewalld中使用ipset
    • 3.1 把一個ipset加入到禁止的規則
    • 3.2 把ip地址中ipset中刪除
  • 四、添加到ipset中的ip地址數據是否會重復
  • 五、使用腳本抓取有問題的ip加入到拒絕訪問的ipset
  • 六、如何防止自己被誤關在防火墻外?使用ip白名單
  • 七、查看firewalld的版本
  • 八、查看linux的版本

一、firewalld中ipset的用途

1.1 用途

ipset是ip地址的集合,firewalld使用ipset可以在一條規則中處理多個ip地址,執行效果更高對ip地址集合的管理也更方便

1.2 注意與iptables所用的ipset命令的不同,

不要混合使用firewall-cmd的ipset參數與linux平臺上的ipset命令,避免引起沖突,firewalld的ipset會記錄到/etc/firewalld/ipsets/目錄下

1.3 配置詳解

IPSet Options--get-ipset-types    打印支持的ipset類型--new-ipset=<ipset>  --類型= < ipset類型>[——選項= <關鍵>[= < >價值]]. .添加一個新的ipset--new-ipset-from-file=<filename> [--name=<ipset>]  從文件中添加一個新的ipset[可選名稱]--delete-ipset=<ipset>  刪除已存在的ipset [可選名稱]--load-ipset-defaults=<ipset>  加載ipset默認設置[可選名稱]--info-ipset=<ipset>打印關于ipset的信息[可選名稱]--path-ipset=<ipset>打印ipset的文件路徑[可選名稱]--get-ipsets         打印預定義ipsets--ipset=<ipset> --set-description=<description>設置新的描述為ipset[可選名稱]--ipset=<ipset> --get-description打印ipset的描述[可選名稱]--ipset=<ipset> --set-short=<description>設置新的短描述為ipset[可選名稱]--ipset=<ipset> --get-short打印ipset的簡短描述[可選名稱]--ipset=<ipset> --add-entry=<entry>向ipset中添加一個新條目[可選名稱]--ipset=<ipset> --remove-entry=<entry>從ipset中刪除一個條目[可選名稱]--ipset=<ipset> --query-entry=<entry>返回ipset是否有條目--ipset=<ipset> --get-entries列出ipset的表項[可選名稱]--ipset=<ipset> --add-entries-from-file=<entry>向ipset中添加新條目[可選名稱]--ipset=<ipset> --remove-entries-from-file=<entry>從ipset中刪除條目[可選名稱]

二、firewalld中ipset的操作例子

2.1 新建一個set

–new-ipset=sshblock: 指定新ipset的名字為:sshblock

–type=hash:ip 指定類型為 hash:ip,這種形式不允許重復而且只有一個ip

[root@blog ipsets]# firewall-cmd --permanent --new-ipset=sshblock --type=hash:ip
success查看ipset文件是否已生成?
說明:默認的目錄是:/etc/firewalld/ipsets
[root@blog ipsets]# more /etc/firewalld/ipsets/sshblock.xml
<?xml version="1.0" encoding="utf-8"?>
<ipset type="hash:ip">
</ipset>

2.2 在set中添加ip

[root@blog ipsets]# firewall-cmd --permanent --ipset=sshblock --add-entry=111.111.111.111
success查看添加ip的效果
[root@blog ipsets]# more /etc/firewalld/ipsets/sshblock.xml
<?xml version="1.0" encoding="utf-8"?>
<ipset type="hash:ip"><entry>111.111.111.111</entry>
</ipset>

2.3 從set中刪除ip

[root@blog ipsets]# firewall-cmd --permanent --ipset=sshblock --remove-entry=111.111.111.111
success查看刪除ip的效果
[root@blog ipsets]# more /etc/firewalld/ipsets/sshblock.xml
<?xml version="1.0" encoding="utf-8"?>
<ipset type="hash:ip">
</ipset>

2.4 刪除一個set

[root@blog ipsets]# firewall-cmd --permanent --delete-ipset=sshblock
success查看sshblock這個set的配置文件是否還存在?
[root@blog ipsets]# more /etc/firewalld/ipsets/sshblock.xml
more: stat of /etc/firewalld/ipsets/sshblock.xml failed: No such file or directory

2.5 打印一個set的文件路徑

[root@blog ipsets]# firewall-cmd --permanent --path-ipset=sshblock
/etc/firewalld/ipsets/sshblock.xml

2.6 打印一個set的內容

[root@blog ipsets]# firewall-cmd --permanent --info-ipset=sshblock
sshblocktype: hash:ipoptions:entries: 111.111.111.111

2.8 判斷一個ip是否存在于set中?

[root@blog ipsets]# firewall-cmd --permanent --ipset=sshblock --query-entry=1.1.1.1
no
[root@blog ipsets]# firewall-cmd --permanent --ipset=sshblock --query-entry=111.111.111.111
yes

2.9 列出所有的ipsets

[root@blog ipsets]# firewall-cmd --permanent --get-ipsets
sshblock

2.10 得到所有的默認ipset類型

[root@blog ipsets]# firewall-cmd --get-ipset-types
hash:ip hash:ip,mark hash:ip,port hash:ip,port,ip hash:ip,port,net hash:mac
hash:net hash:net,iface hash:net,net hash:net,port hash:net,port,net

三、firewalld中使用ipset

3.1 把一個ipset加入到禁止的規則

[root@blog ipsets]# firewall-cmd --permanent --add-rich-rule 'rule family="ipv4" source ipset="sshblock" drop'
success查看xml中的記錄:
[root@blog ipsets]# more /etc/firewalld/zones/public.xml
...<rule family="ipv4"><source ipset="sshblock"/><drop/></rule>
...使生效
[root@blog ipsets]# firewall-cmd --reload
success把禁止的規則刪除
[root@blog ipsets]# firewall-cmd --permanent --remove-rich-rule 'rule family="ipv4" source ipset="sshblock" drop'
success查看xml中的記錄(剛才的drop信息被去除了):
[root@blog ipsets]# more /etc/firewalld/zones/public.xml
......使生效
[root@blog ipsets]# firewall-cmd --reload
success

3.2 把ip地址中ipset中刪除

注意:沒寫入到磁盤

[root@blog ipsets]# firewall-cmd --ipset=sshblock --remove-entry=111.111.111.111
success
[root@blog ipsets]# firewall-cmd --ipset=sshblock --query-entry=111.111.111.111
no
[root@blog ipsets]# firewall-cmd --ipset=sshblock --get-entries可見已刪除成功,如果想永久性的記錄下來:寫入到磁盤后 reload一次
[root@blog ipsets]# firewall-cmd --permanent --ipset=sshblock --remove-entry=111.111.111.111
success
[root@blog ipsets]# more /etc/firewalld/ipsets/sshblock.xml
<?xml version="1.0" encoding="utf-8"?>
<ipset type="hash:ip">
</ipset>
[root@blog ipsets]# firewall-cmd --reload
success

四、添加到ipset中的ip地址數據是否會重復

因為使用了hash類型,當ip重復時firewall-cmd會報錯:

新建ipset
[root@blog ipsets]# firewall-cmd --permanent --new-ipset=sshblock --type=hash:ip
success
添加ip
[root@blog ipsets]# firewall-cmd --permanent --ipset=sshblock --add-entry=111.111.111.111
success查看文件
[root@blog ipsets]# more /etc/firewalld/ipsets/sshblock.xml
<?xml version="1.0" encoding="utf-8"?>
<ipset type="hash:ip"><entry>111.111.111.111</entry>
</ipset>再次添加ip
[root@blog ipsets]# firewall-cmd --permanent --ipset=sshblock --add-entry=111.111.111.111
Warning: ALREADY_ENABLED: 111.111.111.111
success查看文件:
[root@blog ipsets]# more /etc/firewalld/ipsets/sshblock.xml
<?xml version="1.0" encoding="utf-8"?>
<ipset type="hash:ip"><entry>111.111.111.111</entry>
</ipset>

沒有出現重復的情況

五、使用腳本抓取有問題的ip加入到拒絕訪問的ipset

常用的幾類ip:

  1. 被firewalld防火墻reject的ip

  2. nginx日志中訪問過于頻率的ip

  3. secure日志中登錄失敗的ip

我們以secure日志中登錄失敗的ip為例:

先用命令抓取到登錄失敗的ip:

[root@blog log]# grep -i 'Failed password' /var/log/secure | awk '{print $11}' | sort -n | uniq -c | sort -k1nr | awk '{if ($1>5) print $2}'
...寫一段腳本,放到crond中定時執行即可:
[root@blog ~]# vi ./addlogifailip2firewall.sh內容:
#!/bin/bash
for LINE in `grep -i 'Failed password' /var/log/secure | awk '{print $11}' | sort -n | uniq -c | sort -k1nr | awk '{if ($1>3) print $2}'`; doecho "${LINE}";firewall-cmd --permanent --ipset=sshblock --add-entry="${LINE}";
done;
firewall-cmd --reload;

六、如何防止自己被誤關在防火墻外?使用ip白名單

把允許訪問的ip加入到trusted區域:

[root@blog zones]# firewall-cmd --permanent --zone=trusted --add-source=111.111.111.111使生效:
[root@blog zones]# firewall-cmd --reload注意此處不要使用ipset,
使用ipset后,如果同一個ip也被加入了被拒絕的set,
則此ip還是會關到外面。
原因在于firewalld把規則轉到nftables的處理機制,
它把set的處理合并到默認的public zone中去處理了.
大家可以用nft的命令驗證 :
[root@blog log]# nft list ruleset

七、查看firewalld的版本

[root@blog ~]# firewall-cmd --version
0.6.3

八、查看linux的版本

[root@blog ~]# cat /etc/redhat-release
CentOS Linux release 8.0.1905 (Core)

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/pingmian/91111.shtml
繁體地址,請注明出處:http://hk.pswp.cn/pingmian/91111.shtml
英文地址,請注明出處:http://en.pswp.cn/pingmian/91111.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Day06_C++編程

01.思維導圖02.將鳥籠放飛所有鳥類的題&#xff0c;改成觀察者模式#include <iostream> #include <cstring> #include <cstdlib> #include <unistd.h> #include <sstream> #include <vector> #include <memory>//寫一個鳥類:有一個多…

【面試場景題】隨機立減金額計算

文章目錄背景設計思路方案結論高斯分布&#xff08;正態分布&#xff09;背景 某電商公司跟某銀行有合作&#xff0c;推進銀行信用卡辦卡&流水&#xff0c;使用此銀行信用卡用戶&#xff0c;支付可以隨機立減10&#xff5e;30元。其實公司每一筆都可獲得30元支付立減金&…

2025年湖北中級注冊安全工程師報考那些事

2025年湖北中級注冊安全工程師報考那些事各位從事建筑安全的人員看過來&#xff0c;注冊安全工程師是你們行業認可度較為高的證書。關于報考無論是安全相關專業跟不相關的專業都是可以報考的。只是年份要求不同。 本科&#xff1a;相關專業3年&#xff0c;不相關專業4年。 專科…

Prometheus + Grafana + Micrometer 監控方案詳解

這套組合是當前Java生態中最流行的監控解決方案之一&#xff0c;特別適合云原生環境下的微服務應用監控。下面我將從技術實現到最佳實踐進行全面解析。 一、技術棧組成與協作 1. 組件分工組件角色關鍵能力Micrometer應用指標門面(Facade)統一指標采集API&#xff0c;對接多種監…

實習小記(個人中心的編輯模塊)

實習小記&#xff08;個人中心的編輯模塊&#xff09; 項目需要加一個個人中心的編輯模塊&#xff0c;也是差不多搞了一天下來&#xff0c;其中遇到了很多問題&#xff0c;也是來記錄、分享一下。 技術棧&#xff1a;React、antd、TypeScript 需求 點擊編輯&#xff0c;彈出編…

【7】串口編程三種模式(查詢/中斷/DMA)韋東山老師學習筆記(課程聽不懂的話試著來看看我的學習筆記吧)

<1>前置概念補充在深入拆解三種模式前&#xff0c;先通過提供的 “函數對比表” 建立整體認知&#xff1a;這張表是串口收發的「武器庫索引」&#xff0c;清晰標注了查詢、中斷、DMA 三種模式下&#xff0c;收發 / 回調函數的對應關系。后續會結合實際代碼&#xff0c;講…

【Kubernetes 指南】基礎入門——Kubernetes 201(二)

二、滾動升級- 滾動升級&#xff08;Rolling Update&#xff09;通過逐個容器替代升級的方式來實現無中斷的服務升級&#xff1a;- 在滾動升級的過程中&#xff0c;如果發現了失敗或者配置錯誤&#xff0c;還可以隨時回滾&#xff1a;- 需要注意的是&#xff0c; kubectl rolli…

網絡資源模板--基于Android Studio 實現的圖書商城App

目錄 一、測試環境說明 二、項目簡介 三、項目演示 四、部設計詳情&#xff08;部分) 登錄注冊頁 首頁 五、項目源碼 一、測試環境說明 電腦環境 Windows 11 編寫語言 JAVA 開發軟件 Android Studio (2020) 開發軟件只要大于等于測試版本即可(近幾年官網直接下載…

JavaWeb 進階:Vue.js 與 Spring Boot 全棧開發實戰(Java 開發者視角)

作為一名 Java 開發工程師&#xff0c;當你掌握了 HTML、CSS 和 JavaScript 的基礎后&#xff0c;是時候接觸現代前端框架了。Vue.js 以其簡潔的 API、漸進式的設計和優秀的中文文檔&#xff0c;成為眾多 Java 開發者入門前端框架的首選。Vue.js 讓你能快速構建響應式、組件化的…

智能體產品化的關鍵突破:企業智能化轉型的“最后一公里”如何邁過?

智能體產品化的關鍵突破&#xff1a;企業智能化轉型的“最后一公里”如何邁過&#xff1f; 在人工智能迅猛發展的當下&#xff0c;智能體&#xff08;Agent&#xff09;成為企業數字化轉型的新引擎。無論是市場分析、客戶服務&#xff0c;還是自動化辦公&#xff0c;智能體都被…

Rust × Elasticsearch官方 `elasticsearch` crate 上手指南

1 為什么選擇官方 Rust 客戶端&#xff1f; 語義化兼容&#xff1a;客戶端 主版本 與 ES 主版本 嚴格對應&#xff0c;8.x 客戶端可對接任何 8.x 服務器&#xff1b;不存在跨主版本兼容承諾 (docs.rs)100% API 覆蓋&#xff1a;穩定 API 全量映射&#xff0c;Beta/實驗特性可按…

怎樣畫流程圖?符號與流程解構教程

在數字化辦公和項目管理日益復雜的當下&#xff0c;流程圖早已不是工程師、項目經理的專屬工具&#xff0c;它正快速成為每一位職場人提升表達效率、理清工作邏輯的利器。無論是軟件開發中的流程規范、產品設計階段的用戶路徑&#xff0c;還是企業內部的審批流程、團隊協作機制…

vue3 + vite || Vue3 + Webpack創建項目

1.vue3 vite搭建項目方法 &#xff08;需要提前裝node,js&#xff09; 1. 使用官方 create-vite 工具&#xff08;推薦&#xff09; 1.使用npm----------------------------- npm create vuelatest2.使用pnpm----------------------------- pnpm create vuelatest3.使用yarn--…

Vue2-封裝一個含所有表單控件且支持動態增減行列的表格組件

效果1. 無編輯權限&#xff1a;顯示普通表格2. 有編輯權限&#xff1a;根據配置顯示編輯控件3. 可以動態新增行&#xff0c;也可以動態新增列 核心代碼無權限情況的核心代碼<!-- 無編輯權限時顯示普通表格 --><el-tablev-if"!hasEditPermission"ref"ta…

網絡原理 - TCP/IP(一)

目錄 1. 應用層&#xff1a;用戶與網絡的 “交互窗口” 1.1 應用層協議&#xff1a;規范交互的 “通用語言” 1.2 自定義協議&#xff1a;適配特殊需求的 “專屬規則” 1.3 應用層數據格式&#xff1a;讓數據 “說得明白” 1.3.1 XML&#xff1a;結構化但繁瑣的 “老…

Orange的運維學習日記--16.Linux時間管理

Orange的運維學習日記–16. Linux時間管理 文章目錄Orange的運維學習日記--16. Linux時間管理系統與硬件時鐘時鐘類型對比查看內核支持的時鐘源本地時間調整使用 date 查看與設置一次性同步&#xff1a;ntpdate同步到硬件時鐘&#xff1a;hwclock基于 systemd 的 timedatectl交…

Git 與 GitHub 的對比與使用指南

Git 與 GitHub 的對比與使用指南 在軟件開發中&#xff0c;Git 和 GitHub 是兩個密切相關但本質不同的工具。下面我將逐步解釋它們的定義、區別、核心概念以及如何協同使用&#xff0c;確保內容真實可靠&#xff0c;基于廣泛的技術實踐。 1. 什么是 Git&#xff1f; Git 是一個…

20250726-4-Kubernetes 網絡-Service DNS名稱解析_筆記

一、Service DNS名稱 ?1. 例題:通信需求 通信場景:項目A中的Pod需要與項目B中的Pod進行通信,直接使用Pod IP不可行,因為Pod IP會隨著Pod生命周期變化。 解決方案:通過Service提供的穩定IP地址進行通信,不受Pod重建、擴容/縮容等操作影響。 2. CoreDNS介紹 ?? 基本功能…

vscode 登錄ssh記住密碼直接登錄設置

第一種情況在系統已經生成密鑰對的情況下&#xff1a;點擊這里的設置第二步&#xff1a;第三步&#xff1a;沒有填寫的給填寫一下第四步驟&#xff1a;保存后進入選擇這個點開第五步&#xff1a;去Linux終端下輸入這個命令就OK了echo "ssh-rsa內容" >> ~/.ssh/…

Nginx 動靜分離配置(詳細版)

本文介紹了Nginx 動靜分離相關配置&#xff0c;主要包括了配置文件創建、配置示例、配置原理解析以及重新啟用配置文件等等 本文目錄1. 創建 Nginx 配置文件2. 配置示例3. 配置原理解析4. 啟用配置文件并重新加載 Nginx1. 創建 Nginx 配置文件 在 /etc/nginx/sites-available …