Linux服务篇-sshd服务

一、SSHD服务

介绍:SSH 协议:安全外壳协议,为 Secure Shell 的缩写。SSH 为建立在应用层和传输层基础上的安全协议。

作用:sshd服务使用SSH协议可以用来进行远程控制, 或在计算机之间传送文件,相比较之前用telnet方式来传输文件要安全很多,因为telnet使用明文传输,ssh是加密传输。

1、必须的安装包:

OpenSSH软件包,提供了服务端后台程序和客户端工具,用来加密远程控件和文件传输过程中的数据,并由此来代替原来的类似服务。

安装包说明
openssh-6.4p1-8.el7.x86_64.rpm包含OpenSSH服务器及客户端需要的核心文件
openssh-clients-6.4p1-8.el7.x86_64.rpmOpenSSH客户端软件包
openssh-server-6.4p1-8.el7.x86_64.rpmOpenSSH服务器软件包

在我们的镜像文件里查找安装包,使用之前记得要先挂载

[root@qianshuiliyu ~]# ls /mnt/Packages/openssh* -l
-r--r--r--. 79 root root 348728 4月   3 2014 /mnt/Packages/openssh-6.4p1-8.el7.x86_64.rpm
-r--r--r--. 76 root root  68000 4月   3 2014 /mnt/Packages/openssh-askpass-6.4p1-8.el7.x86_64.rpm
# 图形界面下,支持对话框窗口的显示,是一个基于X 系统的密码诊断工具
-r--r--r--. 79 root root 480984 4月   3 2014 /mnt/Packages/openssh-clients-6.4p1-8.el7.x86_64.rpm
-r--r--r--. 79 root root  82192 4月   3 2014 /mnt/Packages/openssh-keycat-6.4p1-8.el7.x86_64.rpm
-r--r--r--. 79 root root 376088 4月   3 2014 /mnt/Packages/openssh-server-6.4p1-8.el7.x86_64.rpm

查看安装包是否安装支持对话框窗口的显示,是一个基于X 系统的密码诊断工具

[root@qianshuiliyu ~]# rpm -qa |grep opssh

2、安装

挂载镜像,通过yum源安装软件
yum安装,会自动解决依赖关系

[root@qianshuiliyu ~]# yum install -y openssh 
已加载插件:langpacks, product-id, subscription-manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
源 'local' 在配置文件中未指定名字,使用标识代替
local                                                                                       | 4.1 kB  00:00:00     
软件包 openssh-6.4p1-8.el7.x86_64 已安装并且是最新版本
无须任何处理

查看是否安装

[root@qianshuiliyu ~]# rpm -qa |grep openssh
openssh-6.4p1-8.el7.x86_64
openssh-clients-6.4p1-8.el7.x86_64
openssh-server-6.4p1-8.el7.x86_64

查看安装软件产生的文件,与etc有关的是关于配置文件的

[root@qianshuiliyu ~]# rpm -ql openssh      
/etc/ssh
/etc/ssh/moduli
/usr/bin/ssh-keygen
/usr/libexec/openssh
/usr/libexec/openssh/ctr-cavstest
......

openssh的配置文件

配置文件说明
ssh_config客户端配置文件
sshd_config服务器端配置文件
[root@qianshuiliyu ~]# ll /etc/ssh/*   
-rw-------. 1 root root     242153 3月  20 2014 /etc/ssh/moduli
-rw-r--r--. 1 root root       2123 3月  20 2014 /etc/ssh/ssh_config
-rw-------. 1 root root       4442 3月  20 2014 /etc/ssh/sshd_config
-rw-r-----. 1 root ssh_keys    227 6月  11 2018 /etc/ssh/ssh_host_ecdsa_key
-rw-r--r--. 1 root root        162 6月  11 2018 /etc/ssh/ssh_host_ecdsa_key.pub
-rw-r-----. 1 root ssh_keys   1679 6月  11 2018 /etc/ssh/ssh_host_rsa_key
-rw-r--r--. 1 root root        382 6月  11 2018 /etc/ssh/ssh_host_rsa_key.pub

3、设置开机启动

设置开启自启动,7版本,查看状态

[root@qianshuiliyu ~]# systemctl enable sshd
[root@qianshuiliyu ~]# systemctl list-dependencies |grep sshd   
  ├─sshd.service
[root@qianshuiliyu ~]# systemctl status sshd
sshd.service - OpenSSH server daemon
   Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled)
   Active: active (running) since 五 2020-09-18 13:05:36 CST; 3h 18min ago
  Process: 1902 ExecStartPre=/usr/sbin/sshd-keygen (code=exited, status=0/SUCCESS)
 Main PID: 1912 (sshd)
   CGroup: /system.slice/sshd.service
           └─1912 /usr/sbin/sshd -D
......
[root@qianshuiliyu ~]# systemctl list-unit-files |grep sshd
anaconda-sshd.service                       static  
sshd-keygen.service                         static  
sshd.service                                enabled 
sshd@.service                               static  
sshd.socket                                 disabled

设置开启自启动,6版本

[root@qianshuiliyu ~]# chkconfig sshd on
[root@qianshuiliyu ~]# systemctl list-unit-files | grep sshd

4、使用口令连接

ssh [远程主机用户名]@[远程服务器主机名或IP地址] [-p指定端口]
或者
ssh -l [远程主机用户名] [远程服务器主机名或IP 地址]

1、不指定用户,当前登录的是哪个就连接哪个用户
第一次登录服务器时系统没有保存远程主机的信息,为了确认该主机身份会提示用户是否继续连接,输入yes 后登录,这时系统会将远程服务器信息写入用户主目录下的$HOME/.ssh/known_hosts 文件中,下次再进行登录时因为保存有该主机信息就不会再提示了

[root@qianshuiliyu ~]$ ssh 192.172.168.100
The authenticity of host '192.172.168.100 (192.172.168.100)' can't be established.
ECDSA key fingerprint is 34:e9:9f:05:ec:c7:18:da:6f:5e:9e:ca:83:32:1f:44.
Are you sure you want to continue connecting (yes/no)? yes
# 无法确认host主机的真实性,只知道它的密钥指纹,问你还想继续连接吗
Warning: Permanently added '192.172.168.100' (ECDSA) to the list of known hosts.
root@192.172.168.100's password: 
Last login: Fri Sep 18 17:29:36 2020
[root@qianshuiliyu ~]# cat /root/.ssh/known_hosts 
192.172.168.100 ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIblzdHAyNTY
AAABBBPiXTbK2LYsd/8PE+uRO91D/418Mb7qm0rGDT+fPxwcInq36D0TfsIFtvkPfh5H4qOJoAB+I/3UFj9
EI9fPdXOE=

RSA算法基于一个十分简单的数论事实:将两个大素数相乘十分容易,但是想要对其乘积进行因式分解却极其困难,因此可以将乘积公开作为加密密钥。

2、连接指定用户登陆,用户不存在需要创建用户以及密码

[root@qianshuiliyu ~]# ssh student@192.172.168.100
student@192.172.168.100's password: 
Last login: Fri Sep 18 17:30:28 2020 from 192.172.168.100

3、-l方式

[student@qianshuiliyu ~]$ ssh -l root 192.172.168.100
root@192.172.168.100's password: 
Last login: Fri Sep 18 17:31:13 2020 from 192.172.168.100

5、免密码登陆-密钥方式

通过密钥对的方式实现免密码方式连接
密钥连接是单向的,如果需要双向都连接,就双向都要进行认证操作
公钥是加密的,私钥是解密的,公钥可以再网络中传输,私钥不能再网络中传输
私钥就相当于你账号的密码,不能忘记,公钥相当于账号
1、在客户机上连接服务器,在客户机上生成密钥对

[root@qianshuiliyu ~]# ssh-keygen 
# -t参数可以指定密钥类型 ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
# 提示输入密匙文件的保存路径
Enter passphrase (empty for no passphrase): 
# 保护密钥的密码,直接回车为空
Enter same passphrase again: 
# 再次输入保护密钥的密码
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
97:e0:f3:c0:fe:5f:5a:a4:c5:5f:b8:d3:10:fe:ab:43 root@qianshuiliyu
The key's randomart image is:
+--[ RSA 2048]----+
|                 |
|                 |
|        .     .  |
|       o . . o o |
|        S o   B .|
|       . =   E *.|
|        . . o = +|
|         .   = ..|
|          ..o.o. |
+-----------------+

查看密钥对:

[root@qianshuiliyu ~]# ll /root/.ssh/*
-rw-------. 1 root root 1679 9月  19 10:59 /root/.ssh/id_rsa
-rw-r--r--. 1 root root  399 9月  19 10:59 /root/.ssh/id_rsa.pub

2、将公钥发布到服务器(复制文件发过去也行,把内容放到对应的文件里)

[root@qianshuiliyu ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub 192.172.168.100
# -i,指定本地公钥的存放位置
The authenticity of host '192.172.168.100 (192.172.168.100)' can't be established.
ECDSA key fingerprint is 34:e9:9f:05:ec:c7:18:da:6f:5e:9e:ca:83:32:1f:44.
Are you sure you want to continue connecting (yes/no)? yes
# 无法确定对方的真实性,是否继续连接
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.172.168.100's password: 
# 首次分发密钥需要输入对端服务器的用户密码
Number of key(s) added: 1

Now try logging into the machine, with:   "ssh '192.172.168.100'"
and check to make sure that only the key(s) you wanted were added.

说明:
如果服务器不是监听22端口,则需要这样传输密钥:

 ssh-copy-id -i /root/.ssh/id_rsa.pub "-p 212 root@192.172.168.100"

其他参数
ssh-copy-id [-n] [-i [identity_file]] [-p port] [-o ssh_option] [user@]hostname

选项说明
-l指定身份文件路径
-p指定端口
-o其他的参数,时间等
user@]hostname用户名@IP或者主机名

把本地主机的公钥发送(或复制)到远程主机,在authorized_keys文件上
查看服务器端的文件
这个密钥的权限只能是600或者700,安全方面

[root@qianshuiliyu ~]# cat /root/.ssh/authorized_keys 
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC/Hp8mhY3te+wtnOyL9qBb/k/BHrubMthrPF1t3pYOxZcPluG
j/LZy3c22CHviWefbKjMcHSLfx7Dvj7M4U77He9cwuy+hUKHavkc4Tw0hYvEdL9mwT7iOhrvtdjQj2PKXAk
Acot+WMGxEVrAN3tZIFef8jjmt2hf8AOn4bl9am7+pmZUpIye1q/I6GVu8kb2LASMMw7w7Bv3+gURtfXty
vGYphrwSt0cj+3zrIHFbDIRuASU5cR45QP+k38RDBLweM960joukVvpSNLgAf/Y8GbAGzK3Yol7amwYn
faQ3g1C3rDb4V3XJCwZmRmd2kwXKh5A9NTls3uC51adb5np root@qianshuiliyu

# 查看权限
[root@qianshuiliyu ~]# ll /root/.ssh/authorized_keys
-rw-------. 1 root root 399 9月  19 11:12 /root/.ssh/authorized_keys

免密码登陆服务

[root@qianshuiliyu ~]# ssh root@192.172.168.100
Last login: Sat Sep 19 12:05:13 2020 from 192.172.168.200

三、配置文件说明

1、查看配置文件

[root@qianshuiliyu ~]# ll /etc/ssh/ 
总用量 268
-rw-------. 1 root root     242153 3月  20 2014 moduli
-rw-r--r--. 1 root root       2123 3月  20 2014 ssh_config
# 客户端配置文件
-rw-------. 1 root root       4442 3月  20 2014 sshd_config
# 服务端配置文件
-rw-r-----. 1 root ssh_keys    227 6月  11 2018 ssh_host_ecdsa_key
-rw-r--r--. 1 root root        162 6月  11 2018 ssh_host_ecdsa_key.pub
-rw-r-----. 1 root ssh_keys   1679 6月  11 2018 ssh_host_rsa_key
-rw-r--r--. 1 root root        382 6月  11 2018 ssh_host_rsa_key.pub

编辑配置文件

[root@qianshuiliyu ~]# vim /etc/ssh/sshd_config 

2、端口设置

#Port 22
# 默认监听端口22,可以设置多个端口
===========================================================
1、修改服务器端口,客户机的22端口无法连接

[root@qianshuiliyu ~]# vim /etc/ssh/sshd_config 
...
#Port 22
Port 289
port 296
...
===========================================================
2、重启服务
[root@qianshuiliyu ~]# systemctl restart sshd
===========================================================
3、查看修改的端口
[root@qianshuiliyu ~]# netstat -anutp |grep sshd
tcp        0      0 0.0.0.0:289           0.0.0.0:*        LISTEN      14340/sshd
tcp        0      0 0.0.0.0:296           0.0.0.0:*        LISTEN      14340/sshd 
==========================================================
4、此时客户机连接服务器默认的22端口是连接不了的
[root@qianshuiliyu ~]# ssh root@192.172.168.100
ssh: connect to host 192.172.168.100 port 22: Connection refused
==========================================================
5、指定端口进行连接
[root@qianshuiliyu ~]# ssh root@192.172.168.100 -p 289
root@192.172.168.100's password: 
Last failed login: Sat Sep 19 19:15:43 CST 2020 from 192.172.168.200 on ssh:notty
There was 1 failed login attempt since the last successful login.
Last login: Sat Sep 19 19:04:38 2020 from 192.172.168.1

3、监听地址设置

#AddressFamily any
#ListenAddress 0.0.0.0
# 服务器绑定的IP 地址,0.0.0.0 表示监听所有地址
#ListenAddress ::
# IPV6版本地址
==========================================================
1、添加一个地址进行监听
[root@qianshuiliyu ~]# ifconfig ifcfg-wang:0 192.172.168.99
==========================================================
2、修改监听地址
#ListenAddress 0.0.0.0
ListenAddress 192.172.168.99
==========================================================
3、重启服务
[root@qianshuiliyu ~]# systemctl restart sshd
==========================================================
4、查看
[root@qianshuiliyu ~]# netstat -anutp |grep sshd|grep 22
tcp        0      0 192.172.168.99:22       0.0.0.0:*          LISTEN      14780/sshd
==========================================================
5、此时其他的地址是无法登陆的
[root@qianshuiliyu ~]# ssh root@192.172.168.100
ssh: connect to host 192.172.168.100 port 22: Connection refused
6、使用监听地址进行登录
[root@qianshuiliyu ~]# ssh root@192.172.168.99
The authenticity of host '192.172.168.99 (192.172.168.99)' can't be established.
ECDSA key fingerprint is 34:e9:9f:05:ec:c7:18:da:6f:5e:9e:ca:83:32:1f:44.
Are you sure you want to continue connecting (yes/no)?

# The default requires explicit activation of protocol 1
#Protocol 2
# SSH协议版本2,支持1,要把Protocol 2改为Protocol 2,1 
# HostKey for protocol version 1
#HostKey /etc/ssh/ssh_host_key
# 设置包含计算机私人密匙的文件
# HostKeys for protocol version 2
HostKey /etc/ssh/ssh_host_rsa_key
#  ssh  version 2使用REA的私钥
#HostKey /etc/ssh/ssh_host_dsa_key
# ssh  version 2使用DSA的私钥
HostKey /etc/ssh/ssh_host_ecdsa_key

密钥说明:
RSA 是一种非对称加解密算法。
RSA 与 DSA 都是非对称加密算法。其中RSA的安全性是基于极其困难的大整数的分解(两个素数的乘积);
DSA 的安全性是基于整数有限域离散对数难题。基本上可以认为相同密钥长度的 RSA 算法与 DSA 算法安全性相当。
公钥用于加密,它是向所有人公开的;私钥用于解密,只有密文的接收者持有。

# Lifetime and size of ephemeral version 1 server key
# 临时版本1服务器密钥的生命周期和大小
#KeyRegenerationInterval 1h
# 密钥更新间隔时长
#ServerKeyBits 1024
# 服务器密钥长度

#SyslogFacility AUTH
SyslogFacility AUTHPRIV
# 当有人使用 SSH 登入系统的时候,SSH 会记录信息,这个信息要记录的类型为AUTHPRIV。
#LogLevel INFO
# 登录记录的等级!INFO级别以上。

默认日志存放在哪?
sshd服务日志存放在: /var/log/secure
查看日志

[root@qianshuiliyu ~]# ll /var/log/secure 
-rw-------. 1 root root 12815 9月  19 14:19 /var/log/secure

/etc/rsyslog.conf指明这个类型的存放路径

[root@qianshuiliyu ~]# vim /etc/rsyslog.conf 
......
#The authpriv file has restricted access.
authpriv.*                                              /var/log/secure
......

4、设置优雅的等待时间

#LoginGraceTime 2m
#  优雅的登录等待时间,默认2分钟
==========================================================
1、修改时间
[root@qianshuiliyu ~]#  vim /etc/ssh/sshd_config 
LoginGraceTime 20s
==========================================================
2、重启:
[root@qianshuiliyu ~]# systemctl restart sshd   
==========================================================
3、超过时间,连接断开
[root@qianshuiliyu ~]# ssh root@192.172.168.100
root@192.172.168.100's password: 
Connection closed by 192.172.168.100

5、禁止root用户登录

#PermitRootLogin yes
# 是否允许root登录,yes or no,建议设置成no
==========================================================
1、修改配置
#PermitRootLogin yes
PermitRootLogin no
==========================================================
2、重启服务
[root@qianshuiliyu ~]# systemctl restart sshd 
==========================================================
3、登录
[root@qianshuiliyu ~]# ssh root@192.172.168.100
root@192.172.168.100's password: 
Permission denied, please try again.
root@192.172.168.100's password: 
Permission denied, please try again.
root@192.172.168.100's password: 
Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).
# 权限被拒绝

6、禁止使用密码登录,使用密钥登录

# To disable tunneled clear text passwords, change to no here!
#PasswordAuthentication yes
#PermitEmptyPasswords no
# 是否允许空密码登录,不要管
PasswordAuthentication yes
#  是否用密码认证,如果no掉一定要做好密钥,否则凉凉
==========================================================
1、修改配置文件
PasswordAuthentication no
==========================================================
2、重启服务
[root@qianshuiliyu ~]# systemctl restart sshd
==========================================================
3、使用密钥登录
[root@qianshuiliyu ~]# ssh root@192.172.168.100
Last login: Sun Sep 20 09:21:43 2020 from 192.172.168.200
==========================================================
4、取消密钥无法登陆(删除密钥文件)
[root@qianshuiliyu ~]# ssh 192.172.168.100
Permission denied (publickey,gssapi-keyex,gssapi-with-mic).

7、设置登录提示

#PrintMotd yes
#  打印登陆提示信息,内容是 /etc/motd 这个文档的内容
#PrintLastLog yes
#  打印最近一次登录的日志
==========================================================
1、编辑提示
[root@qianshuiliyu ~]# echo "你已经来到Linux的世界,你的一切操作都将被记录!" > /etc/motd
==========================================================
2、重启服务
[root@qianshuiliyu ~]# systemctl restart sshd
==========================================================
3、连接查看
[root@qianshuiliyu ~]# ssh root@192.172.168.100
root@192.172.168.100's password: 
Last login: Sun Sep 20 09:53:51 2020 from 192.172.168.200
你已经来到Linux的世界,你的一切操作都将被记录!
122 #TCPKeepAlive yes
123 #UseLogin no
124 UsePrivilegeSeparation sandbox          # Default for new installations.
# UsePrivilegeSeparation yes 
# 是否权限较低的程序来提供用户操作。我们知道 sshd 启动在 port 22 , 
# 因此启动的程序是属于 root 的身份。那么当 student 登入后,这个设定值
# 会让 sshd 产生一个属于 sutdent 的 sshd 程序来使用,对系统较安全 
==========================================================
[root@qianshuiliyu ~]# ssh student@192.172.168.100
student@192.172.168.100's password: 
Last login: Fri Sep 18 18:38:01 2020 from 192.172.168.100
[student@qianshuiliyu ~]$ ps -axu |grep sshd 
root       1929  0.0  0.1 135680  4928 ?        Ss   09:13   0:00 sshd: root@pts/0
root       5383  0.0  0.0  82956  3580 ?        Ss   10:50   0:00 /usr/sbin/sshd -D
root       5844  0.1  0.1 135680  4884 ?        Ss   11:31   0:00 sshd: student [priv]
student    5849  0.0  0.0 135680  2096 ?        S    11:31   0:00 sshd: student@pts/1
student    5901  0.0  0.0 112656   992 pts/1    R+   11:31   0:00 grep --color=auto sshd
# 这样,当这个进程sshd被缓存区溢出等手段,得到登录的权限后,还是student普通用户。不会得到root身份。

#UseDNS yes
# 为了要判断客户端来源是正常合法的,因此会使用 DNS 去反查客户端的主机名 
# 连接内网主机慢的时候可以no
# GSSAPIAuthentication yes 
# 两个都改才生效,都no才生效

#PidFile /var/run/sshd.pid
#  可以放置 SSHD 这个 PID 的文档

四、防止暴力破解

方法一:密码足够复杂
密码足够的复杂,密码的长度要大于8位最好大于20位(当然越长越好)。密码的复杂度是密码要尽可能有数字、大小写字母和特殊符号混合组成
方法二:修改默认端口
方法三:通过密钥进行认证
方法四:不允许root用户直接登陆,添加普通账号,授予root的权限
是否可以禁止root身份登录? 不行,因为有些程序需要使用root身份登录并运行。另外判断一个用户是不是超级管理员,看的是用户的ID是否为0。
方法五:直接禁止网络

设置普通用户的权限

不让root登录修改passwd的bash修改位/sbin/nologin,或者sshd的配置文件no掉

root:x:0:0:root:/root:/sbin/nologin
==========================================================
给普通账号权限
student:x:0:0:student:/home/student:/bin/bash
查看登录
[root@qianshuiliyu ~]# ssh student@192.172.168.100
student@192.172.168.100's password: 
Last login: Sun Sep 20 10:48:27 2020 from 192.172.168.200
[root@qianshuiliyu ~]# whoami 
root
# 此时,如果配置文件no掉root,student无法登陆

五、发送数据SCP

scp [源] [目标]

-r复制目录

要把当前一个文件copy到远程另外一台主机上

[root@qianshuiliyu ~]# scp /etc/passwd root@192.172.168.200:/root/
root@192.172.168.200's password: 
passwd                                             100% 1968     1.9KB/s   00:00    
[root@qianshuiliyu ~]#

把文件从远程主机copy到当前系统

[root@qianshuiliyu ~]# scp root@192.172.168.200:/etc/passwd /root/
root@192.172.168.200's password: 
passwd                                               100% 1917     1.9KB/s   00:00    
[root@qianshuiliyu ~]# ls
a.txt  core.3492  fail2ban-0.8.14  fail2ban-0.8.14.tar.gz  passwd

六、fail2ban

fail2ban可以监视你的系统日志,然后匹配日志的错误信息(正则式匹配)执行相应的屏蔽动作(一般情况下是防火墙),而且可以发送e-mail通知系统管理员,很好、很实用、很强大!
#ban (bæn)禁令

简单来说其功能就是防止暴力破解。工作的原理是通过分析一定时间内的相关服务日志,将满足动作的相关IP利用iptables加入到dorp列表一定时间。
重启iptables服务的话,所有DORP将重置。

下载地址:fail2ban官网
将下载的安装包上传到服务器,或者直接在服务器上下载

[root@qianshuiliyu ~]# ls
fail2ban-0.8.14.tar.gz

将下载的压缩包进行解压

[root@qianshuiliyu ~]# tar -zxvf fail2ban-0.8.14.tar.gz 
fail2ban-0.8.14/
fail2ban-0.8.14/.coveragerc
......
==========================================================
[root@qianshuiliyu ~]# ls
fail2ban-0.8.14  fail2ban-0.8.14.tar.gz

查看文档说明
[root@qianshuiliyu ~]# vim fail2ban-0.8.14/README.md
在这里插入图片描述
因为软件是由python编写的,所以必须安装python解释器,且版本大于等于2.4

查看当前系统python版本

[root@qianshuiliyu ~]# python -V
Python 2.7.5

安装:

[root@qianshuiliyu ~]# cd fail2ban-0.8.14/
[root@qianshuiliyu fail2ban-0.8.14]# python setup.py install
running install
running build
running build_py
creating build
creating build/lib

设置启动脚本

查看当前路径
[root@qianshuiliyu fail2ban-0.8.14]# pwd
/root/fail2ban-0.8.14
==========================================================
将启动脚本添加到启动项里面,并重命名为fail2ban
[root@qianshuiliyu fail2ban-0.8.14]# cp files/redhat-initd /etc/init.d/fail2ban
[root@qianshuiliyu fail2ban-0.8.14]# ls /etc/init.d/
fail2ban functions  iprdump  iprinit  iprupdate  netconsole  network  README   rhnsd
启动脚本
[root@qianshuiliyu fail2ban-0.8.14]# systemctl start fail2ban 
[root@qianshuiliyu fail2ban-0.8.14]# chkconfig  --add  fail2ban 

[root@qianshuiliyu ~]# ll /etc/init.d
lrwxrwxrwx. 1 root root 11 6月  11 2018 /etc/init.d -> rc.d/init.d

如何寻找启动脚本

启动脚本查找, 一个新的软件包,怎么知道哪个文件是启动脚本文件?
这就要找服务器启动脚本文件中有什么特点,然后过滤出来对应的文件名。

[root@qianshuiliyu fail2ban-0.8.14]# grep chkconfig ./* -R             
./files/redhat-initd:# chkconfig: - 92 08
# chkconfig

相关主要文件说明:

/etc/fail2ban/action.d  
# 动作文件夹,内含默认文件。iptables以及mail等动作配置
/etc/fail2ban/fail2ban.conf   
# 定义了fai2ban日志级别、日志位置及sock文件位置
/etc/fail2ban/filter.d                   
# 条件文件夹,内含默认文件。过滤日志关键内容设置
/etc/fail2ban/jail.conf     
# 主要配置文件,模块化。主要设置启用ban动作的服务及动作阀值  , jail,监狱 
/etc/rc.d/init.d/fail2ban          
# 启动脚本文件

配置文件

因为动作文件(action.d/iptables.conf)以及日志匹配条件文件(filter.d/sshd.conf )安装后是默认存在的。基本不用做任何修改。所有主要需要设置的就只有jail.conf文件。
启用sshd服务的日志分析,指定动作阀值即可。实例文件/etc/fail2ban/jail.conf及说明如下:

[DEFAULT]
# 全局设置
ignoreip = 127.0.0.1/8
# 忽略的IP列表,不受设置限制
bantime  = 600
# 屏蔽时间,单位:秒
findtime  = 600
# 这个时间段内超过规定次数会被ban掉
maxretry = 3
# 最大尝试次数
backend = auto
# 日志修改检测机制(gamin、polling和auto这三种)
==========================================================
[ssh-iptables]
# 单个服务检查设置,如设置bantime、findtime、maxretry和全局冲突,服务优先级大于全局设置

enabled  = true
# 是否激活此项(true/false)修改成 true,这个不激活,一切操作都无法使用
filter   = sshd
# 过滤规则filter的名字,对应filter.d目录下的sshd.conf
action   = iptables[name=SSH, port=ssh, protocol=tcp]
# 动作的相关参数,对应action.d/iptables.conf文件
           sendmail-whois[name=SSH, dest=you@example.com, sender=fail2ban@example.com, sendername="Fail2Ban"]
           # dest,触发报警的收件人
logpath  = /var/log/sshd.log
检测的系统的登陆日志文件。这里要写sshd服务日志文件。 默认为logpath  = /var/log/sshd.log
maxretry = 3
# 输入密码次数
bantime  = 1800
# 禁止用户IP访问主机半小时
findtime  = 300    
# 在5分钟内超过规定次数会被ban掉

实战:ssh远程登录三次密码错误,禁止半小时不能登录

修改局部的配置文件

logpath  = /var/log/secure
# 将日志改为自己的日志,要不可能不显示效果
maxretry = 3
bantime  = 1800
findtime  = 300
==========================================================
清空日志,便于查看
[root@qianshuiliyu ~]# > /var/log/secure
==========================================================
重启服务
[root@qianshuiliyu ~]# systemctl restart fail2ban.service 
==========================================================
以防火墙的形式显示
[root@qianshuiliyu fail2ban-0.8.14]# iptables -nvL 
...
Chain fail2ban-SSH (1 references)
 pkts bytes target     prot opt in     out     source               destination         
   77  5056 RETURN     all  --  *      *       0.0.0.0/0            0.0.0.0/0     

远程主机进行连接,三次密码输错

[root@qianshuiliyu ~]# ssh 192.172.168.100
root@192.172.168.100's password: 
Permission denied, please try again.
root@192.172.168.100's password: 
Permission denied, please try again.
root@192.172.168.100's password: 
Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).
# 第四次登录就直接拒绝的
[root@qianshuiliyu ~]# ssh 192.172.168.100
ssh: connect to host 192.168.1.63 port 22: Connection refused

查看fail2ban是否工作

[root@qianshuiliyu ~]# fail2ban-client status 
Status
|- Number of jail:      1
`- Jail list:           ssh-iptables

具体看某一项的状态也可以看,如果显示被ban的ip和数目就表示成功了,如果都是0,说明没有成功

[root@qianshuiliyu ~]# fail2ban-client status ssh-iptables
Status for the jail: ssh-iptables
|- filter
|  |- File list:        /var/log/secure 
|  |- Currently failed: 1
|  `- Total failed:     12
`- action
   |- Currently banned: 1
   |  `- IP list:       192.172.168.200 
   `- Total banned:     1

查看日志显示

[root@qianshuiliyu ~]# tail -10 /var/log/fail2ban.log 
2020-09-21 08:42:21,123 fail2ban.jail   [3797]: INFO    Jail 'ssh-iptables' uses pyinotify
2020-09-21 08:42:21,146 fail2ban.jail   [3797]: INFO    Initiated 'pyinotify' backend
2020-09-21 08:42:21,149 fail2ban.filter [3797]: INFO    Added logfile = /var/log/secure
2020-09-21 08:42:21,150 fail2ban.filter [3797]: INFO    Set maxRetry = 3
2020-09-21 08:42:21,151 fail2ban.filter [3797]: INFO    Set findtime = 300
2020-09-21 08:42:21,152 fail2ban.actions[3797]: INFO    Set banTime = 1800
2020-09-21 08:42:21,192 fail2ban.jail   [3797]: INFO    Jail 'ssh-iptables' started
2020-09-21 08:42:26,267 fail2ban.actions[3797]: WARNING [ssh-iptables] Ban 192.172.168.200
2020-09-21 08:45:45,938 fail2ban.comm   [3797]: WARNING Command ['stats', 'ssh-iptable'] has failed. Received Exception('Invalid command',)

注意事项

1.让黑名单的用户登录,清空相关日志就可以了

[root@qianshuiliyu ~]# >  /var/log/secure 

2.防火墙的变动也需要重启fail2ban
3.修改端口,配置文件的端口也要相对应

/etc/fail2ban/jail.conf 
/etc/fail2ban/action.d/iptables.conf

七、通过脚本进行防护

在这里插入图片描述

八、Linux自带的PAM模块

查看模块

[root@qianshuiliyu ~]# ls  /etc/pam.d/
atd                  gdm-fingerprint         other             remote             sshd                      systemd-user
chfn                 gdm-launch-environment  passwd            rhn_register       su                        vlock
chsh                 gdm-password            password-auth     runuser            subscription-manager      vmtoolsd
config-util          gdm-pin                 password-auth-ac  runuser-l          subscription-manager-gui  xserver
crond                gdm-smartcard           pluto             setup              sudo
cups                 kcheckpass              polkit-1          smartcard-auth     sudo-i
fingerprint-auth     kscreensaver            postlogin         smartcard-auth-ac  su-l
fingerprint-auth-ac  liveinst                postlogin-ac      smtp               system-auth
gdm-autologin        login                   ppp               smtp.postfix       system-auth-ac

说明:
在这里插入图片描述
在这里插入图片描述
pam的常用命令

查看某一用户错误登陆次数:,root代表用户名
pam_tally –-user
例如,查看root用户的错误登陆次数:
pam_tally –-user root
清空某一用户错误登陆次数:
pam_tally –-user –-reset
例如,清空 root 用户的错误登陆次数,
pam_tally –-user root –-reset 
  • 2
    点赞
  • 38
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

浅水鲤鱼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值