
我希望阻止所有sshd
連接,但分配一個動態 IP,<subdomain>.ddns.net
因此我將其放入/etc/hosts.deny
:
sshd: ALL EXCEPT <subdomain>.ddns.net
這不允許我連接到 SSH。相反,如果我放置由該主機名稱
解析的 IP(確認),它就會起作用:dig <subdomain>.ddns.net
sshd: ALL EXCEPT <ipv4.resolved.by.hostname>
我也嘗試過 withUseDNS yes
或no
in sshd_config
,但它沒有改變任何東西。
防火牆(UFW)依規則開放ufw limit ssh
我的實際情況/etc/ssh/sshd_config
如下:
Protocol 2
HostKey /etc/ssh/ssh_host_rsa_key
HostKey /etc/ssh/ssh_host_ed25519_key
KexAlgorithms [email protected]
Ciphers [email protected],[email protected],[email protected],aes256-ctr,aes192-ctr,aes128-ctr
MACs [email protected],[email protected],[email protected],hmac-sha2-512,hmac-sha2-256,[email protected]
PermitRootLogin no
AllowUsers remotessh
IgnoreRhosts yes
PasswordAuthentication no
PermitEmptyPasswords no
ChallengeResponseAuthentication no
UsePAM yes
X11Forwarding no
PrintMotd no
PubkeyAuthentication yes
AllowTcpForwarding no
AllowStreamLocalForwarding no
GatewayPorts no
PermitTunnel no
UseDNS no
# Allow client to pass locale environment variables
AcceptEnv LANG LC_*
# override default of no subsystems
Subsystem sftp /usr/lib/openssh/sftp-server
答案1
該問題很可能是由於您連接的 IP 位址反向為 xxx.yourisp.com,而不是 subdomain.ddns.net。
當您嘗試從(動態)IP 位址連接到 sshd 時,tcpwrappers 會對您的 IP 位址執行反向 DNS 查找。如果解析為 xxx.yourisp.com,那麼它不會在hosts.allow 或(可能是hosts.deny)中找到匹配項,因此它不會允許從您的IP 連接到sshd。
作為解決方法,您可能需要考慮將subdomain.ddns.net 新增至/etc/hosts 檔案中,並建立每隔幾分鐘執行一次的cron 作業,並在動態IP 位址變更時使用動態IP 位址更新此條目。這不是一個非常優雅的解決方案,但這是我最近自己遇到這個問題時能想到的最好的解決方案。如果有人知道更乾淨的解決方案,請發表評論。
答案2
您將同時使用/etc/hosts.allow
和/etc/hosts.deny
來實現這一點。在 處/etc/hosts.allow
,輸入以下內容:
sshd: blablabla.ddns.net
在 /etc/hosts.deny 中插入以下內容:
sshd: ALL
它會起作用,因為/etc/hosts.allow
重疊/etc/hosts.deny
。但有一個問題:如果您的伺服器位於髮夾 NAT(有些也稱為 NAT 反射)後面,則某些連接將顯示為您的伺服器的網關的內部 IP 位址,因此可能很難阻止。
另一個選擇是使用 iptables,如下所示:
iptables -t filter -A INPUT -s blablabla.ddns.net -p tcp --dport 22 -j ACCEPT
iptables -t filter -A INPUT -p tcp --dport -j DROP
請注意 iptables 會考慮其規則的順序。
祝你好運。