/etc/hosts.allow 與 /etc/hosts.deny 是 TCP Wrappers 機制的配置文件,適用於所有链接 libwrap 的網絡服務(sshd、vsftpd、telnetd、portmap 等)。匹配邏輯:
hosts.allow,符合任一規則 → 允許。hosts.deny,符合任一規則 → 拒絕。格式:daemon_list : client_list [: option]
daemon_list:服務進程名(如 sshd、vsftpd、in.telnetd),多個用空格或逗號分隔,ALL 代表所有服務。client_list:IP、網段、主機名、域名(以 . 開頭)、ALL、LOCAL(無點號主機名)等。option:可選,如 spawn /path/script、severity auth.info 等。# /etc/hosts.allow
sshd : 192.168.1.0/255.255.255.0
sshd : 10.0.0.0/255.0.0.0
sshd : .trusted-domain.com # 允許該域名下所有主機
# /etc/hosts.deny
sshd : ALL # 拒絕其他所有 IP
# /etc/hosts.deny
sshd : 62.75.214.93
sshd : 203.215.252.189
sshd : 219.143.200.169
sshd : 60.12.193.134
sshd : 201.83.220.44
sshd : c953dc2c.virtua.com.br
sshd : gera125.server4you.de
# /etc/hosts.allow
vsftpd : 192.168.10.0/24
vsftpd : .mycompany.com
# /etc/hosts.deny
vsftpd : ALL
# /etc/hosts.allow
ALL : 10.10.10.0/255.255.255.0 # 管理網段
ALL : 192.168.100.5 # 運維堡垒機
# /etc/hosts.deny
ALL : ALL
hosts.allow > hosts.deny,切勿在 allow 中寫 ALL: ALL 否則 deny 失效。tcpdchk -v 檢查語法錯誤。spawn /usr/bin/logger -t tcpwrap "Denied %c for %d" 記錄拒絕事件。# 測試某 IP 是否被允許訪問 sshd
tcpdmatch sshd 192.168.1.50
# 查看當前生效規則
cat /etc/hosts.allow /etc/hosts.deny
善用 hosts.allow/deny 可快速實現服務級 IP 白/黑名單,是輕量級主機防護的重要一環。