linux使用/etc/hosts.deny拒绝恶意ssh到本机

一、目标

在某些特殊情况下,服务器有很多恶意暴力ssh破解的攻击。解决的办法有很多,这里用/etc/hosts.deny增加黑名单的方式来简单阻止一下。

二、前言

/etc/hosts.allow 优先于 /etc/hosts.deny。

如果在allow和deny文件中都有某个ip,那么这个ip是被允许ssh。

如果在allow中有,deny没有,那么这个ip是被允许ssh。

如果allow中没有,deny有,那么这个ip是被禁止ssh。

还可以在 /etc/hosts.deny文件里追加一个额外配置,让其读取另外一个ip列表。这样可以使你以后维护需要禁止的ip更方便

三、 /etc/hosts.deny的使用方法

1、方法1:

直接在 /etc/hosts.deny文件里写拒绝规则

bash 复制代码
echo "sshd: 10.0.1.29" >> /etc/hosts.deny
2、方法2:

在 /etc/hosts.deny文件写一个额外读取ip列表的规则

bash 复制代码
echo "sshd: /etc/sshd.deny.hostguard"   >> /etc/hosts.deny'
bash 复制代码
echo "10.0.1.29" >> /etc/sshd.deny.hostguard

注:以后就往sshd.deny.hostguard里面写恶意ip的地址就行了。保持一行一个ip。

四、(附)看哪些ip在恶意访问

bash 复制代码
tail -f /var/log/secure
bash 复制代码
tail -n 100 /var/log/secure |grep 'Failed password' |egrep -o '([0-9]{1,3}\.){3}[0-9]{1,3}'
bash 复制代码
lastb -n 500 |awk '{print $3}' |sort |uniq |egrep -o '([0-9]{1,3}\.){3}[0-9]{1,3}'

注意检查别有自己ip被写进去了

----------------------2024年5月11日09:21:49--------------------

相关推荐
蝎子莱莱爱打怪13 小时前
Centos7中一键安装K8s集群以及Rancher安装记录
运维·后端·kubernetes
埃博拉酱13 小时前
VS Code Remote SSH 连接 Windows 服务器卡在"下载 VS Code 服务器":prcdn DNS 解析失败的诊断与 BITS 断点续传
windows·ssh·visual studio code
崔小汤呀14 小时前
最全的docker安装笔记,包含CentOS和Ubuntu
linux·后端
何中应15 小时前
vi编辑器使用
linux·后端·操作系统
何中应15 小时前
Linux进程无法被kill
linux·后端·操作系统
何中应15 小时前
rm-rf /命令操作介绍
linux·后端·操作系统
何中应15 小时前
Linux常用命令
linux·操作系统
葛立国15 小时前
从 / 和 /dev 说起:Linux 文件系统与挂载点一文理清
linux
DianSan_ERP1 天前
电商API接口全链路监控:构建坚不可摧的线上运维防线
大数据·运维·网络·人工智能·git·servlet
哇哈哈20211 天前
信号量和信号
linux·c++