萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 作業系統 >  >> Linux 教學

Linux 禁止特定 IP 訪問實戰:TCP Wrappers 與 iptables/nftables 雙重防線

Linux 系統層面限制 IP 訪問,主要有兩套機制:TCP Wrappers(/etc/hosts.allow/deny,應用層過濾)和Netfilter(iptables/nftables,內核層包過濾)。本文系統講解雙重防線配置、優先級邏輯、常見坑點與生產環境模板。

方案一:TCP Wrappers(hosts.allow/deny)

適用條件

  • 服務程序編譯時鏈接了 libwrap(ldd /usr/sbin/sshd | grep libwrap 有輸出)
  • 服務通過 tcpd 或直接調用 hosts_access()
  • 常見支持服務:sshd、vsftpd、portmap、xinetd 管理的服務
  • 不支持:大多數獨立守護進程(Nginx、Apache、MySQL 等)

配置邏輯(順序優先)

  1. 先檢查 /etc/hosts.allow,**匹配即允許**,停止檢查
  2. 再檢查 /etc/hosts.deny,**匹配即拒絕**,停止檢查
  3. 均不匹配 → **默認允許**

/etc/hosts.allow 示例(白名單)


# 允許管理網段 SSH
sshd: 10.0.0.0/255.255.255.0 192.168.1.0/24

# 允許特定 IP 所有服務
ALL: 192.168.1.100

# 允許內網訪問 Samba
smbd: 192.168.0.0/255.255.255.0

# 允許所有訪問 HTTP/HTTPS(Web 服務通常不走 tcp_wrappers,此處僅示例)
httpd: ALL
vsftpd: ALL

/etc/hosts.deny 示例(黑名單/默認拒絕)


# 拒絕所有 SSH 連接(配合 allow 白名單實現「默認拒絕,白名單放行」)
sshd: ALL

# 拒絕所有 Telnet
in.telnetd: ALL

# 默認拒絕所有服務(極其嚴格,需在 allow 中逐項放行)
# ALL: ALL EXCEPT 192.168.1.0/24 10.0.0.0/8

生效驗證


# 重啟 xinetd(若服務由其管理)
systemctl restart xinetd

# 測試:從非白名單 IP 連接 SSH
# 應提示:ssh_exchange_identification: Connection closed by remote host

方案二:iptables/nftables(內核級、全協議、高性能)

iptables 經典配置模板


#!/bin/bash
# /usr/local/bin/setup_firewall.sh

IPT=/sbin/iptables

# 清空現有規則
$IPT -F
$IPT -X
$IPT -Z

# 默認策略
$IPT -P INPUT DROP
$IPT -P FORWARD DROP
$IPT -P OUTPUT ACCEPT

# 允許本地回環
$IPT -A INPUT -i lo -j ACCEPT
$IPT -A OUTPUT -o lo -j ACCEPT

# 允許已建立/相關連接
$IPT -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

# 允許 ICMP(ping、MTU 發現)
$IPT -A INPUT -p icmp --icmp-type echo-request -m limit --limit 6/s -j ACCEPT
$IPT -A INPUT -p icmp --icmp-type destination-unreachable -j ACCEPT
$IPT -A INPUT -p icmp --icmp-type time-exceeded -j ACCEPT
$IPT -A INPUT -p icmp --icmp-type parameter-problem -j ACCEPT

# 允許 SSH(僅管理網段)
$IPT -A INPUT -p tcp -s 10.0.0.0/8 --dport 22 -j ACCEPT
$IPT -A INPUT -p tcp -s 192.168.0.0/16 --dport 22 -j ACCEPT

# 允許 HTTP/HTTPS(全網)
$IPT -A INPUT -p tcp --dport 80 -j ACCEPT
$IPT -A INPUT -p tcp --dport 443 -j ACCEPT

# 允許特定應用端口(如監控、API)
$IPT -A INPUT -p tcp -s 10.0.0.0/8 --dport 9090 -j ACCEPT  # Prometheus
$IPT -A INPUT -p tcp -s 10.0.0.0/8 --dport 3306 -j ACCEPT  # MySQL 僅內網

# 記錄被拒絕的包(限速避免日誌洪水)
$IPT -A INPUT -m limit --limit 5/min -j LOG --log-prefix "IPT_DROP: " --log-level 4

# 保存規則
# CentOS/RHEL 7
service iptables save
# 或
iptables-save > /etc/sysconfig/iptables

# Debian/Ubuntu (iptables-persistent)
iptables-save > /etc/iptables/rules.v4

nftables 現代配置(Debian 10+/CentOS 8+ 推薦)


# /etc/nftables.conf
#!/usr/sbin/nft -f

flush ruleset

table inet filter {
    chain input {
        type filter hook input priority 0; policy drop;

        # 回環
        iifname "lo" accept

        # 已建立/相關
        ct state established,related accept

        # ICMP
        ip protocol icmp icmp type { echo-request, destination-unreachable, time-exceeded, parameter-problem } limit rate 6/second accept
        ip6 protocol ipv6-icmp icmpv6 type { echo-request, destination-unreachable, time-exceeded, parameter-problem } limit rate 6/second accept

        # SSH 僅管理網段
        ip saddr { 10.0.0.0/8, 192.168.0.0/16 } tcp dport 22 accept

        # Web 服務
        tcp dport { 80, 443 } accept

        # 內網服務
        ip saddr 10.0.0.0/8 tcp dport { 3306, 9090, 6379 } accept

        # 記錄拒絕
        limit rate 5/minute log prefix "NFT_DROP: " level info
    }

    chain forward {
        type filter hook forward priority 0; policy drop;
    }

    chain output {
        type filter hook output priority 0; policy accept;
    }
}

# 應用配置
nft -f /etc/nftables.conf

# 開機自動載入
systemctl enable --now nftables

封禁單 IP / IP 段速查


# iptables 封禁單 IP
iptables -I INPUT -s 203.0.113.10 -j DROP

# 封禁網段
iptables -I INPUT -s 203.0.113.0/24 -j DROP

# 查看規則帶行號
iptables -L INPUT --line-numbers

# 刪除規則(按行號)
iptables -D INPUT 3

# 保存
iptables-save > /etc/sysconfig/iptables

端口級精確控制(如僅允許特定 IP 訪問 9889)


# 1. 默認拒絕該端口
iptables -I INPUT -p tcp --dport 9889 -j DROP

# 2. 允許白名單 IP(插在 DROP 之前,-I 默認插在鏈頭)
iptables -I INPUT -s 192.168.1.201 -p tcp --dport 9889 -j ACCEPT
iptables -I INPUT -s 192.168.1.202 -p tcp --dport 9889 -j ACCEPT
iptables -I INPUT -s 192.168.1.203 -p tcp --dport 9889 -j ACCEPT

# 規則順序:ACCEPT (201) → ACCEPT (202) → ACCEPT (203) → DROP (all)
# 匹配即停止,白名單優先生效

持久化與自動化

方法命令適用系統
service 保存service iptables saveCentOS/RHEL 7
iptables-saveiptables-save > /etc/sysconfig/iptablesCentOS/RHEL 7
iptables-persistentapt-get install iptables-persistent; netfilter-persistent saveDebian/Ubuntu
nftablesnft list ruleset > /etc/nftables.conf現代系統
rc.local在 /etc/rc.local 調用腳本通用(不推薦)

雙重防線協同策略

層級工具保護對象優勢
應用層TCP Wrappers特定服務(sshd、vsftpd 等)配置簡單、支持主機名/域名匹配、日誌友好
內核層iptables/nftables所有協議/端口/接口性能高、狀態檢測、模塊豐富、支持 NAT/標記

建議:關鍵服務(SSH、資料庫管理端口)雙層防護;Web 服務走 iptables/nftables 統一管理。


TCP Wrappers 適合「服務級、簡單白名單」,iptables/nftables 適合「系統級、全流量、高性能」防護。生產環境請**默認拒絕、顯式放行**,配合日誌審計與自動化封禁(Fail2Ban/CrowdSec),構建纵深防禦體系。

copyright © 萬盛學電腦網 all rights reserved