Linux 系統層面限制 IP 訪問,主要有兩套機制:TCP Wrappers(/etc/hosts.allow/deny,應用層過濾)和Netfilter(iptables/nftables,內核層包過濾)。本文系統講解雙重防線配置、優先級邏輯、常見坑點與生產環境模板。
libwrap(ldd /usr/sbin/sshd | grep libwrap 有輸出)tcpd 或直接調用 hosts_access()/etc/hosts.allow,**匹配即允許**,停止檢查/etc/hosts.deny,**匹配即拒絕**,停止檢查
# 允許管理網段 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
# 拒絕所有 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
#!/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
# /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
# 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
# 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 save | CentOS/RHEL 7 |
| iptables-save | iptables-save > /etc/sysconfig/iptables | CentOS/RHEL 7 |
| iptables-persistent | apt-get install iptables-persistent; netfilter-persistent save | Debian/Ubuntu |
| nftables | nft 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),構建纵深防禦體系。