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

相关推荐
之歆6 小时前
Linux文件系统与FHS详解
linux·文件系统
China_Yanhy8 小时前
转型AI运维工程师·Day 7:构建“数据飞轮” —— 每一句对话都是资产
运维·人工智能·状态模式
zl_dfq9 小时前
Linux 之 【多线程】(死锁、同步与竞态条件、条件变量、pthread_cond_xxx、POSIX信号量、sem_xxx)
linux
学Linux的语莫9 小时前
k8s常用命令
linux·容器·kubernetes
openKylin9 小时前
《2025年度OpenAtom openKylin社区全景案例集》正式发布
linux
CS_Zero9 小时前
Ubuntu安装Claude Code
linux·ubuntu·ai编程·claude
A星空1239 小时前
三、Kconfig介绍以及制作menuconfig界面
linux·运维·服务器
zylyehuo10 小时前
Windows & Linux 双系统资料整理
linux·夯实基础
安科士andxe10 小时前
实操指南|安科士1.25G CWDM SFP光模块选型、部署与运维全攻略
运维·数据库·5g
口袋物联11 小时前
模板方法模式在 C 语言中的应用(含 Linux 内核实例)
linux·c语言·模板方法模式