安装fail2ban保护Linux服务器

简介: CentOS 7.9.2009上安装配置fail2ban

最近总有人在尝试非法登录服务器,每天都有上千次的登录尝试,于是在服务器上安装了fail2ban,现将安装、配置过程记录下来,以备日后查阅。

Fail2ban扫描/var/log/auth.log等日志文件,并禁止进行过多失败登录尝试的IP地址。它通过在可配置的时间量内更新系统防火墙规则以拒绝来自这些IP地址的新连接来实现这一点。Fail2ban开箱即用,可以读取许多标准的日志文件,比如那些用于sshd和apache的文件,并且可以很容易地配置以读取您选择的任何日志文件,以查找您需要的任何错误(翻译自fail2ban官网的介绍)。

fail2ban是用python开发的,需要python版本不低于3.5,而CentOS7默认安装的是python2,因此首先需要安装python3,这里采用源码编译的方式并将python3安装到/usr/local/python3目录下,切记不可能用python3替换掉系统自带的python2,因为yum等程序不兼容python3。

一、源码编译安装python3

wget https://www.python.org/ftp/python/3.7.17/Python-3.7.17.tgz
tar zxvf Python-3.7.17.tgz
cd Python-3.7.17
./configure --prefix=/usr/local/python3 --enable-optimizationsmake-j4make install

二、安装fail2ban

git clone https://github.com/fail2ban/fail2ban.git
cd fail2ban
sudo python3 setup.py install
#检查fail2ban是否安装成功fail2ban-client -h

fail2ban的配置文件会安装到/etc/fail2ban目录下,将build目录下fail2ban.service复制到/lib/systemd/system/目录下,可通过systemctl启停fail2ban。

cat /lib/systemd/system/fail2ban.service
[Unit]
Description=Fail2Ban Service
Documentation=man:fail2ban(1)
After=network.target iptables.service firewalld.service ip6tables.service ipset.service nftables.service
PartOf=iptables.service firewalld.service ip6tables.service ipset.service nftables.service
[Service]
Type=simple
Environment="PYTHONNOUSERSITE=1"ExecStartPre=/bin/mkdir -p /run/fail2ban
ExecStart=/usr/local/python3/bin/fail2ban-server -xfstart# if should be logged in systemd journal, use following line or set logtarget to sysout in fail2ban.local# ExecStart=/usr/local/python3/bin/fail2ban-server -xf --logtarget=sysout startExecStop=/usr/local/python3/bin/fail2ban-client stopExecReload=/usr/local/python3/bin/fail2ban-client reload
PIDFile=/run/fail2ban/fail2ban.pid
Restart=on-failure
RestartPreventExitStatus=0255[Install]
WantedBy=multi-user.target

三、简单配置

这里仅简单设置了对sshd进行保护,主要修改/etc/fail2ban/jail.conf同时添加/etc/fail2ban/jail.d/sshd,下面仅列出修改部分,其余的取默认值即可(配置文件里对每个选项都有详细说明,可自行查阅)。

# /etc/fail2ban/jail.conf[sshd]
# To use more aggressive sshd modes set filter parameter "mode" in jail.local:# normal (default), ddos, extra or aggressive (combines all).# See "tests/files/logs/sshd" or "filter.d/sshd.conf" for usage example and details.#mode   = normalenabled =trueport    =ssh#根据自己的sshd配置修改logpath = %(sshd_log)s
backend = %(sshd_backend)s
cat /etc/fail2ban/jail.local
[DEFAULT]
ignoreip =192.168.2.105
bantime  =86400findtime  =300maxretry =3banaction = firewallcmd-rich-rules
backend = systemd
[sshd]
enabled =true
# /etc/fail2ban/jail.d/sshd.conf# sshd的保护规则[sshd]
enable =truemaxretry =5findtime = 10m
bantime = 24h

四、运行及检查

systemctl enable fail2ban.service
systemctl start fail2ban
systemctl status fail2ban
#如下提示说明已经正常启动● fail2ban.service - Fail2Ban Service
   Loaded: loaded (/usr/lib/systemd/system/fail2ban.service; enabled; vendor preset: disabled)
   Active: active (running) since Wed 2023-10-0418:37:48 CST; 5s ago
     Docs: man:fail2ban(1)
  Process: 30143ExecStartPre=/bin/mkdir -p /run/fail2ban (code=exited, status=0/SUCCESS)
 Main PID: 30146 (fail2ban-server)
   CGroup: /system.slice/fail2ban.service
           └─30146 /usr/local/python3/bin/python3 /usr/local/python3/bin/fail2ban-server -xfstartOct 0418:37:48 ydapp1 systemd[1]: Starting Fail2Ban Service...
Oct 0418:37:48 ydapp1 systemd[1]: Started Fail2Ban Service.
Oct 0418:37:49 ydapp1 fail2ban-server[30146]: Server ready
#可用fail2ban-client version查看版本号fail2ban-client version
1.1.0.dev1
#可通过fail2ban-client status查看状态fail2ban-client status sshd
Status for the jail: sshd
|- Filter
|  |- Currently failed: 0|  |- Total failed:     0|  `- File list:`- Actions
   |- Currently banned: 0   |- Total banned:     0`- Banned IP list:[root@ydapp1 fail2ban]# fail2ban-client status sshdStatus for the jail: sshd|- Filter|  |- Currently failed: 0|  |- Total failed:     0|  `- File list:
`- Actions   |- Currently banned: 0   |- Total banned:     0   `- Banned IP list:


目录
相关文章
|
22小时前
|
存储 关系型数据库 MySQL
linux安装MySQL8.0,密码修改权限配置等常规操作详解
linux安装MySQL8.0,密码修改权限配置等常规操作详解
|
1天前
|
Linux Python Windows
Python虚拟环境virtualenv安装保姆级教程(Windows和linux)
Python虚拟环境virtualenv安装保姆级教程(Windows和linux)
|
2天前
|
Ubuntu Linux 开发工具
【专栏】如何在 Linux 上安装和使用 exa?
【4月更文挑战第28天】在Linux上,exa是一个现代化的文件管理系统替代工具,提供直观的文件信息展示。要安装exa,可以在基于Debian的系统(如Ubuntu)上运行`sudo apt install exa`,基于RedHat(如CentOS)的系统运行`sudo yum install exa`,或从源代码编译安装。使用exa的基本命令是`exa`,它列出当前目录的文件和目录。通过选项如`-F`(显示文件类型)、`-h`(人类可读大小)、`-l`(详细信息)和`-s`(排序)可以定制输出。exa还能与其他命令(如grep)结合使用,提升效率。
|
2天前
|
存储 监控 Linux
【专栏】如何在 Linux 中列出已安装的驱动器?
【4月更文挑战第28天】在 Linux 中,了解已安装驱动器是系统管理的关键。本文介绍了三种方法:1) 使用 `lsblk` 命令显示设备名、大小和类型;2) `fdisk -l` 命令提供详细分区信息;3) `gnome-disks` 等系统管理工具展示驱动器信息。此外,还讨论了驱动器类型识别、挂载点概念及其应用。通过这些方法,用户能有效地监控和管理 Linux 系统中的驱动器。
|
2天前
|
存储 Linux 虚拟化
虚拟机(vmware)中安装linux系统
虚拟机(vmware)中安装linux系统
|
3天前
|
监控 关系型数据库 MySQL
|
4天前
|
监控 Linux 网络安全
Linux服务器如何查询连接服务器的IP
【4月更文挑战第17天】Linux服务器如何查询连接服务器的IP
8 1
|
1月前
|
Ubuntu JavaScript 关系型数据库
在阿里云Ubuntu 20.04服务器中搭建一个 Ghost 博客
在阿里云Ubuntu 20.04服务器上部署Ghost博客的步骤包括创建新用户、安装Nginx、MySQL和Node.js 18.x。首先,通过`adduser`命令创建非root用户,然后安装Nginx和MySQL。接着,设置Node.js环境,下载Nodesource GPG密钥并安装Node.js 18.x。之后,使用`npm`安装Ghost-CLI,创建Ghost安装目录并进行安装。配置过程中需提供博客URL、数据库连接信息等。最后,测试访问前台首页和后台管理页面。确保DNS设置正确,并根据提示完成Ghost博客的配置。
在阿里云Ubuntu 20.04服务器中搭建一个 Ghost 博客
|
1月前
|
存储 弹性计算 数据可视化
要将ECS中的文件直接传输到阿里云网盘与相册(
【2月更文挑战第31天】要将ECS中的文件直接传输到阿里云网盘与相册(
423 4
|
25天前
|
弹性计算
阿里云ECS使用体验
在申请高校学生免费体验阿里云ECS云服务器后的一些使用体验和感受。