溫馨提示×

Linux防火墻如何配置IP白名單

小樊
57
2025-08-23 16:37:18
欄目: 智能運維

在Linux系統中,配置IP白名單通常涉及使用iptables或firewalld等工具。以下是使用這兩種工具配置IP白名單的基本步驟:

使用iptables配置IP白名單

  1. 清空現有規則(可選,但推薦):

    sudo iptables -F
    
  2. 設置默認策略(可選):

    sudo iptables -P INPUT DROP
    sudo iptables -P FORWARD DROP
    sudo iptables -P OUTPUT ACCEPT
    
  3. 允許本地回環接口的流量

    sudo iptables -A INPUT -i lo -j ACCEPT
    
  4. 允許已建立的連接

    sudo iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
    
  5. 添加IP白名單規則: 假設你想允許IP地址為192.168.1.100的設備訪問:

    sudo iptables -A INPUT -s 192.168.1.100 -j ACCEPT
    
  6. 保存規則(根據發行版不同,命令可能有所不同):

    • Debian/Ubuntu:
      sudo sh -c "iptables-save > /etc/iptables/rules.v4"
      
    • CentOS/RHEL:
      sudo service iptables save
      
  7. 重啟iptables服務(如果需要):

    sudo systemctl restart iptables
    

使用firewalld配置IP白名單

  1. 啟用firewalld服務(如果尚未啟用):

    sudo systemctl start firewalld
    sudo systemctl enable firewalld
    
  2. 添加IP白名單規則: 假設你想允許IP地址為192.168.1.100的設備訪問:

    sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="192.168.1.100" accept'
    
  3. 重新加載firewalld配置

    sudo firewall-cmd --reload
    

驗證配置

無論使用哪種工具,都可以通過以下命令查看當前的防火墻規則:

  • iptables:

    sudo iptables -L -v -n
    
  • firewalld:

    sudo firewall-cmd --list-all
    

通過這些步驟,你可以成功配置Linux系統的IP白名單,確保只有指定的IP地址可以訪問你的系統。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女