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

相关推荐
csbDD1 小时前
2025年网络安全(黑客技术)三个月自学手册
linux·网络·python·安全·web安全
李狗蛋儿啊1 小时前
zero自动化框架搭建---Git安装详解
运维·git·自动化
小金的学习笔记2 小时前
如何在本地和服务器新建mysql用户和密码
运维·服务器·mysql
s_fox_2 小时前
nginx ngx_http_module(7) 指令详解
运维·nginx·http
EasyNVR2 小时前
EasyRTC智能硬件:实时畅联、沉浸互动、消音护航
运维·服务器·网络·安全·音视频·webrtc·p2p
CarryBest2 小时前
Jenkins 环境搭建---基于 Docker
运维·jenkins
若云止水3 小时前
Ubuntu 下 nginx-1.24.0 源码分析 - ngx_process_options
运维·nginx
s_fox_3 小时前
nginx ngx_http_module(9) 指令详解
运维·nginx·http
Natsuagin3 小时前
轻松美化双系统启动界面与同步时间设置(Windows + Ubuntu)
linux·windows·ubuntu·grub
我们的五年3 小时前
【Linux网络编程】应用层协议HTTP(请求方法,状态码,重定向,cookie,session)
linux·网络·http