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--------------------

相关推荐
zxnbmk22 分钟前
磁盘挂载与迁移【自用复制】
linux
WW、forever23 分钟前
【服务器】上传服务器中数据至 FigShare(Python)
运维·服务器·python
YQ_011 小时前
Ubuntu下安装WPS
linux·ubuntu·wps
小义_1 小时前
【Docker】知识四
linux·运维·docker·容器
小志biubiu1 小时前
Linux_进程概念(A)-进程部分【Ubuntu】
linux·运维·服务器·ubuntu·操作系统·进程
普通网友1 小时前
Ubuntu 入门及安装全指南
linux·运维·ubuntu
wei_shuo1 小时前
OpenHarmony Linux 命令行工具适配实战:基于 Cursor × WSL 的 tree 2.2.1 交叉编译与 HNP 打包全流程指南
linux·openharmony·tree
冷雨夜中漫步1 小时前
DockerDesktop打包docker镜像时报错
运维·docker·容器
xlp666hub1 小时前
你对 argc 和 argv 的理解有多深?
linux
xdpcxq10292 小时前
嵌入式Linux手动交叉编译
linux·运维·服务器