Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Linux 系統教學 >> Linux 教學

Linux hosts.allow 與 hosts.deny 訪問控制實戰:IP 白名單/黑名單配置

編輯:Linux 教學

TCP Wrappers 通過 /etc/hosts.allow(白名單)與 /etc/hosts.deny(黑名單)實現服務級訪問控制,匹配順序:先檢查 deny,再檢查 allow,allow 可覆蓋 deny。本文提供四類典型配置場景及 IP 寫法規範。

核心規則

  • 格式:服務進程名: 客戶端地址
  • 服務名對應 /etc/inetd.conf 或 xinetd 配置中的進程名(如 in.sshd、in.telnetd、in.ftpd)
  • 通配符 ALL 代表所有服務/所有地址

四大典型場景

場景hosts.denyhosts.allow
1. 僅允許特定網段 SSHin.sshd: ALLin.sshd: 218.64.87.0/255.255.255.128
2. 禁止特定網段 Telnetin.telnetd: 218.64.87.0/255.255.255.128(空或其他允許規則)
3. 全服務僅允許特定網段ALL: ALLALL: 218.64.87.0/255.255.255.128
4. 禁止特定網段所有服務ALL: 218.64.87.0/255.255.255.128(空)

IP 地址寫法三種規範

  1. 網絡地址/子網掩碼:218.64.87.0/255.255.255.0
  2. 前綴匹配:218.64.(匹配 218.64.x.x)
  3. CIDR 斜線記法:218.64.87.0/24(等同 255.255.255.0)

驗證與調試

# 測試 SSH 訪問控制
tcpdchk -v                    # 檢查配置語法
tcpdmatch in.sshd 192.168.1.100  # 模擬匹配測試

注意事項

  • 僅對支持 TCP Wrappers 的服務生效(需編譯時包含 libwrap)
  • 現代服務(sshd、vsftpd 等)通常內建支持,但需確認配置文件未禁用
  • 防火牆規則優先級高於 TCP Wrappers,建議雙層防護
  • 修改後無需重啟服務,下次連接即時生效
Copyright © Windows教程網 All Rights Reserved