linux - 允许特定 IP 地址使用 ssh 并使用/etc/hosts.allow 和/etc/hosts.deny 文件阻止所有其他服务器

标签 linux

<分区>

允许一组十个 IP 地址的 ssh 连接,并使用 hosts.allow 和 hosts.deny 文件阻止所有其他 IP。这个怎么做?

最佳答案

SSHD 将首先检查 /etc/hosts.allow 中的条目。如果 /etc/hosts.allow 中没有适合 SSHD 的规则,将继续检查 /etc/hosts.deny 规则。

所以你需要添加:

/etc/hosts.deny

sshd: ALL EXCEPT LOCAL

阻止每个 SSH 连接,除了 localhost,它不在 /etc/hosts.allow 中。

/etc/hosts.allow

sshd: 192.168.178.10
sshd: 192.168.178.11
sshd: 192.168.178.10/255.255.255.0

允许特定 IP。最后一条规则是 IP 范围的示例。

然而,使用 /etc/hosts.allow/etc/hosts.deny 不是推荐的方法来允许 SSH 仅用于几个IP。您应该考虑使用 iptables 来完成这项工作。

您可以使用如下规则为特定 IP 允许 SSH:

iptables -A INPUT -m state --state NEW,ESTABLISHED,RELATED --source x.x.x.x -p tcp --dport 22 -j ACCEPT
iptables -A INPUT -m state --state NEW,ESTABLISHED,RELATED -p tcp --dport 22 -j DROP

如果你想使用 iptables,你应该看看 this stackoverflow question .

关于linux - 允许特定 IP 地址使用 ssh 并使用/etc/hosts.allow 和/etc/hosts.deny 文件阻止所有其他服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32221168/

相关文章:

linux - EC2 实例的 SSL 证书

linux - 如何在文件中压缩-1

linux - if 语句中的未知错误

linux - Arch Linux 上的 Intellij Idea 14 打开灰屏

linux - 在 Linux 中删除选定的 60 天旧文件的脚本

linux - 如何在具有 Windows (PE) 目标的 Linux 机器上交叉编译 haskell 程序?

linux - 如何列出具有非挂起链接的目录中的所有符号链接(symbolic link)?

linux - Bash:进程监控和操作

linux - 在 ssh 断开连接后保持 perl 脚本运行

c++ - 二进制文件格式与基于文本的格式文件大小