ssh服务
1.ssh服务的介绍
- 名称:ssh协议
- 安全外壳协议
- Secure shell 的缩写
- 建立在应用层和传输层基础之上的安全协议
- 作用
- sshd服务使用ssh协议可以用来进行远程控制和计算机之间的文件传输
- 比telnet安全,telnet传输是使用的明文而ssh传输时使用的是密文
2.OpenSSH安装需要四个软件包
- openSSH软件包,提供了服务端后台程序和客户端工具,用于加密远程控件和文件传输过程中的数据,并由此来代替原来的类似服务
- 安装包
- OpenSSH需要四个安装包
- openssh-5.3p1-114.el6_7.x86_64 包含服务器和客户端需要的核心文件
- openssh-clients-5.3p1-114.el6_7.x86_64 OpenSSH客户端软件包
- openssh-server-5.3p1-114.el6_7.x86_64 OpenSSH服务器软件包
- openssh-askpass-5.3p1-114.el6_7.x86_64 支持对话框窗口的显示,是一个基于x系统的密码
- 使用yum来查看openssh包
- OpenSSH需要四个安装包
[root@server /]# yum list all | grep openssh
openssh.x86_64 7.4p1-21.el7 @anaconda
openssh-clients.x86_64 7.4p1-21.el7 @anaconda
openssh-server.x86_64 7.4p1-21.el7 @anaconda
openssh-askpass.x86_64 7.4p1-21.el7 base
openssh-cavs.x86_64 7.4p1-21.el7 base
openssh-keycat.x86_64 7.4p1-21.el7 base
openssh-ldap.x86_64 7.4p1-21.el7 base
openssh-server-sysvinit.x86_64 7.4p1-21.el7 base
安装说明
1. 主要安装 openssh-clients 和openssh-server 其他依赖包会自动安装
2. 可根据需求,在服务端安装openssh-server,在客户端安装 openssh-clients
2.在镜像文件中查openssh包
因为sshd服务是存在镜像文件中的Packages目录下的,我们也可以使用命令来查看
[root@server /]# ls /centos/Packages/openssh*
/centos/Packages/openssh-7.4p1-21.el7.x86_64.rpm
/centos/Packages/openssh-keycat-7.4p1-21.el7.x86_64.rpm
/centos/Packages/openssh-askpass-7.4p1-21.el7.x86_64.rpm
/centos/Packages/openssh-server-7.4p1-21.el7.x86_64.rpm
/centos/Packages/openssh-clients-7.4p1-21.el7.x86_64.rpm
安装的两种方法
1.配置yum仓库,安装
[root@server /]# yum install -y openssh openssh-clients openssh-server
安装前提
-
以配置好yum源,(本地or网络源)推荐使用域yum安装
- 设置开机自动挂载系统镜像文件
echo "/dev/cdrom /centos iso660 defaults 0 0" >>/etc/fstab
光驱设备 挂载点 挂载格式 默认 默认
2,本地直接安装rpm包文件
[root@server /]# rpm -ivh /centos/Packages/openssh*.rpm
检查软件包已否是安装
[root@server /]# rpm -qa | grep openssh*
openssl-1.0.2k-19.el7.x86_64
openssh-server-7.4p1-21.el7.x86_64
openssh-clients-7.4p1-21.el7.x86_64
xmlsec1-openssl-1.2.20-7.el7_4.x86_64
openssh-7.4p1-21.el7.x86_64
openssl-libs-1.0.2k-19.el7.x86_64
查看安装路径
[root@server /]# rpm -ql openssh
/etc/ssh
/etc/ssh/moduli
/usr/bin/ssh-keygen
/usr/libexec/openssh
/usr/libexec/openssh/ctr-cavstest
/usr/libexec/openssh/ssh-keysign
/usr/share/doc/openssh-7.4p1
/usr/share/doc/openssh-7.4p1/CREDITS
/usr/share/doc/openssh-7.4p1/ChangeLog
/usr/share/doc/openssh-7.4p1/INSTALL
/usr/share/doc/openssh-7.4p1/OVERVIEW
/usr/share/doc/openssh-7.4p1/PROTOCOL
/usr/share/doc/openssh-7.4p1/PROTOCOL.agent
/usr/share/doc/openssh-7.4p1/PROTOCOL.certkeys
/usr/share/doc/openssh-7.4p1/PROTOCOL.chacha20poly1305
/usr/share/doc/openssh-7.4p1/PROTOCOL.key
/usr/share/doc/openssh-7.4p1/PROTOCOL.krl
/usr/share/doc/openssh-7.4p1/PROTOCOL.mux
/usr/share/doc/openssh-7.4p1/README
/usr/share/doc/openssh-7.4p1/README.dns
/usr/share/doc/openssh-7.4p1/README.platform
/usr/share/doc/openssh-7.4p1/README.privsep
/usr/share/doc/openssh-7.4p1/README.tun
/usr/share/doc/openssh-7.4p1/TODO
/usr/share/licenses/openssh-7.4p1
/usr/share/licenses/openssh-7.4p1/LICENCE
/usr/share/man/man1/ssh-keygen.1.gz
/usr/share/man/man8/ssh-keysign.8.gz
查看/etc/ssh的配置文件
[root@server /]# ll /etc/ssh
总用量 604
-rw-r--r--. 1 root root 581843 8月 9 2019 moduli
-rw-r--r-- 1 root root 2276 10月 30 19:27 ssh_config 客户端配置文件--安装了openssh-clients会产生
-rw------- 1 root root 3905 10月 30 19:28 sshd_config 服务端配置文件--安装了openssh-server会产生 以后只要修改的就是服务器的配置文件
-rw-r-----. 1 root ssh_keys 227 5月 17 2021 ssh_host_ecdsa_key
-rw-r--r--. 1 root root 162 5月 17 2021 ssh_host_ecdsa_key.pub
-rw-r-----. 1 root ssh_keys 387 5月 17 2021 ssh_host_ed25519_key
-rw-r--r--. 1 root root 82 5月 17 2021 ssh_host_ed25519_key.pub
-rw-r-----. 1 root ssh_keys 1675 5月 17 2021 ssh_host_rsa_key
-rw-r--r--. 1 root root 382 5月 17 2021 ssh_host_rsa_key.pub
使用
[root@server /]# systemctl start sshd 开启sshd服务
[root@server /]# systemctl enable sshd 设置为开机自启
[root@server /]# systemctl is-enabled sshd 查看是否为开机自启
enabled
软件包名为openssh
服务名为sshd
[root@server ~]# systemctl status sshd 查看sshd服务的状态
● sshd.service - OpenSSH server daemon
Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled)
Active: active (running) since 五 2021-11-26 08:58:06 CST; 1h 12min ago
查看是否开机自启
[root@server ~]# systemctl list-unit-files | grep sshd
anaconda-sshd.service static
sshd-keygen.service static
sshd.service enabled
sshd@.service static
sshd.socket disabled
优化
1.查看sshd_config 服务端的配置文件
[root@server pam.d]# vim /etc/ssh/sshd_config
15 # semanage port -a -t ssh_port_t -p tcp #PORTNUMBER "#" 与第一单词之间有空格代表注释
16 #
17 #Port 22 "#"与第一个单词之间没有空格代表默认值
一般就是修改端口号,防止黑客针对sshd服务端口号22进行暴力破解
查看sshd服务器的tcp和udp的状态信息
[root@server ssh]# netstat -tulnp | grep sshd
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1042/sshd
tcp 0 0 127.0.0.1:6010 0.0.0.0:* LISTEN 1545/sshd: root@pts
tcp 0 0 127.0.0.1:6011 0.0.0.0:* LISTEN 11730/sshd: root@pt
tcp6 0 0 :::22 :::* LISTEN 1042/sshd
tcp6 0 0 ::1:6010 :::* LISTEN 1545/sshd: root@pts
tcp6 0 0 ::1:6011 :::* LISTEN 11730/sshd: root@pt
修改端口号为3000
17 Port 3000
18 #AddressFamily any
重启服务并且查看sshd服务器的tcp和udp的状态信息
[root@server ssh]# systemctl restart sshd
[root@server ssh]# netstat -tulnp | grep sshd
tcp 0 0 0.0.0.0:3000 0.0.0.0:* LISTEN 13762/sshd
tcp 0 0 127.0.0.1:6010 0.0.0.0:* LISTEN 1545/sshd: root@pts
tcp 0 0 127.0.0.1:6011 0.0.0.0:* LISTEN 11730/sshd: root@pt
tcp6 0 0 :::3000 :::* LISTEN 13762/sshd
tcp6 0 0 ::1:6010 :::* LISTEN 1545/sshd: root@pts
tcp6 0 0 ::1:6011 :::* LISTEN 11730/sshd: root@pt
正常一个服务器一般都有两张网卡,一个连接外网,一个连接内网,需要将sshd服务监控某个网卡,就写某个网卡的ip地址
#Port 22
#AddressFamily any
#ListenAddress 0.0.0.0 0.0.0.0表示可以连接,如果只需内网连接就改为内网ip地址,外网亦是如此`
#ListenAddress :: ipv6地址的网卡,一般不用修改它
37 #LoginGraceTime 2m 登录等待时间默认为2分钟
38 #PermitRootLogin yes 是否允许root用户登录
39 #StrictModes yes
40 #MaxAuthTries 6
41 #MaxSessions 10
47 AuthorizedKeysFile .ssh/authorized_keys 认证的文件在.ssh/authorized_key
64 #PermitEmptyPasswords no 是否允许空密码登录
65 PasswordAuthentication yes 是否允许使用密码登录
105 #PrintMotd yes 打印一个登录提示信息 --- 登录时输出一个提示的登录语言 在/etc/motd文件中输入提示信息
106 #PrintLastLog yes 打印最近一次登录信息
[C:\~]$ ssh root@169.254.218.130
Connecting to 169.254.218.130:22...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.
Last login: Sat Nov 27 11:03:13 2021 from 169.254.218.2 打印最近一次的登录提示信息,注意 from 169.254.218.2 如果发现那天不是这个ip地址,或者ip地址不知道的,那么就要排查服务器是否遭到攻击,或者被黑
115 #UseDNS yes 是否使用DNS来反检,登录速度就会变快 可改可不改
2.配置安全的sshd服务
1.密码足够复杂
2.修改默认端口号
3.不用允许root用户直接登录,添加普通用户,授予root的权限
不能直接禁止root用户登录,因为因为有些程序需要root用户身份登录来运行,判断另一用户是不是超级管理员,主要看用户的ID是否为0
4.不允许密码登录,只能通过认证的密钥来登录系统
通过认证的密钥来登录系统
1.在客户端上面生成一个密钥
[root@client ~]# ssh-keygen 使用ssh-keygen生成一个密钥
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 输入密钥存放路径 默认在(/root/.ssh/id_rsa)文件中
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase): 输入解密密码,默认为空(直接按enter键就是空)
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:
SHA256:UEYLL7iIe8KXR+nKFMqZ6QuirsSeAr524L63aAd1GHo root@client
The key's randomart image is:
+---[RSA 2048]----+
| ..+ |
| . . = . |
| . + o o |
| o E o.o |
|. +.oo S |
|=+= = |
|OX++ o |
|OB*+o |
|X@B+. |
+----[SHA256]-----+
2. 查看/root/.ssh/文件 客户端
[root@client ~]# ll /root/.ssh/
total 8
-rw------- 1 root root 1675 Nov 27 00:13 id_rsa
-rw-r--r-- 1 root root 393 Nov 27 00:13 id_rsa.pub
3.将公钥传到服务器端
[root@client ~]# cd /root/.ssh/
[root@client .ssh]# ls
id_rsa id_rsa.pub
# 如果服务端不是22号端口就使用
# ssh-copy-id -i ~/.ssh/id_rsa.pub '-p 端口号 root@169.254.218.130'
[root@client .ssh]# ssh-copy-id -i root@169.254.218.130 将公钥传到server端
/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '169.254.218.130 (169.254.218.130)' can't be established.
ECDSA key fingerprint is SHA256:DubafNL95ruQktUi/BCsbu0r2joLQYAH+u9XQG1xK5A.
ECDSA key fingerprint is MD5:9e:30:cc:28:6b:cd:7d:0d:40:d9:c0:84:f9:71:3b:10.
Are you sure you want to continue connecting (yes/no)? ye ^H^H
Please type 'yes' or 'no': yes
/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/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@169.254.218.130's password:
/etc/bashrc
/root/bashrc
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@169.254.218.130'"
and check to make sure that only the key(s) you wanted were added.
4.将客户端的rsa.pub复制到了server服务端的 /root/.ssh/authorized_keys
[root@server .ssh]# cd /root/.ssh/
[root@server .ssh]# ls
authorized_keys known_hosts
[root@server .ssh]# cat authorized_keys
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCz6osiBBgRuZGld2lDh/Pv212SUNMAK/F4/glct2yv8Csa47hkLgu8yZYEQTAX6zQ2q/APpQ2c1mKR9QPrDhfBxjxeI8TmPcwwtGsmg+7gcbxhPZ1+9/QQ8n7KBgnce0Tch8hWzDWO5fas10txYXbrqbNbZs7ZW+BrfQ/SGvfeYwzOWAhX0rcaeNBLP8WA41HQIBcj7//2b4emOvW70lGUnR6UW5q/K/lutvv9ILoFsolsLs2LSQrDPos167zmW1dgbb8dU9ezewx44Vr+SRiCExe3ssjCtSlVD2FWbpbHyAqAUnZ94rgoBXRYfzs6sLSToxrbpVnF4mTO/uRdHa27 root@client
注意:从client端拷贝到服务端的公钥文件权限一定是600或者700,只能是这两种权限,其他任何的权限都是不行的(因为是为了安全性)
如果碰到不能使用ssh-copy-id 复制的,就直接复制,粘贴,记着一定要将权限设置为600或者700,就可以了
[root@client .ssh]# ssh root@169.254.218.130
Last login: Sat Nov 27 13:08:01 2021
hello fomalhuat
/etc/profile
[root@server1 ~]# 可以发现就直接登录进去了.
对于免密码的登录:在以后学习集群和写shell脚本,自动化运维的时候会使用到,免密码登录时前提
注意:这是client到server单向的,如果也想server到client也是免密登录,就得将server的公钥复制到client既可
防止暴力破解
1.通过开源的防护软件来防护安全
公司网站遭暴力破解,虽然没有成功,但是会增加系统负载,因为在暴力破解时,系统会不断地认证用户拒绝用户,从而增加了系统资源的额外开销,导致访问公司网站速度很慢.
fail2ban可以防止暴力破解,监控系统日志,然后匹配日志的错误信息,(正则式匹配)执行相应的屏蔽动作,(一般情况下是防火墙),发送e-mail通知系统管理员
#ban 禁令
工作原理:
通过分析一定时间内的相关服务日志,将满足动作的相关ip利用iptables加入到dorp列表一定时间
注意:
重启iptables服务,所有的drop将重置
1.下载fail2ban软件包
www.fail2ban.org
2.安装
[root@server ~]# ls
anaconda-ks.cfg fail2ban-0.8.14.tar.gz initial-setup-ks.cfg mac.html my.sh
[root@server ~]# tar -xf fail2ban-0.8.14.tar.gz -C /usr/local/ 将fail2ban安装在/usr/local目录下
3.查看fail2dan目录中的文件
[root@zabbix-server fail2ban-0.8.14]# ls
-rw-rw-r-- 1 root root 46255 8月 20 2014 ChangeLog
drwxrwxr-x 2 root root 250 8月 20 2014 client
drwxrwxr-x 2 root root 101 8月 20 2014 common
drwxrwxr-x 4 root root 76 8月 20 2014 config
-rw-rw-r-- 1 root root 19296 8月 20 2014 COPYING
-rw-rw-r-- 1 root root 13329 8月 20 2014 DEVELOP
drwxrwxr-x 2 root root 46 8月 20 2014 doc
-rwxrwxr-x 1 root root 12699 8月 20 2014 fail2ban-client
-rwxrwxr-x 1 root root 13570 8月 20 2014 fail2ban-regex
-rwxrwxr-x 1 root root 4502 8月 20 2014 fail2ban-server
-rwxrwxr-x 1 root root 8242 8月 20 2014 fail2ban-testcases
-rwxrwxr-x 1 root root 397 8月 20 2014 fail2ban-testcases-all
drwxrwxr-x 4 root root 4096 8月 20 2014 files
-rw-rw-r-- 1 root root 18972 8月 20 2014 FILTERS
-rwxrwxr-x 1 root root 69 8月 20 2014 kill-server
drwxrwxr-x 2 root root 217 8月 20 2014 man
-rw-rw-r-- 1 root root 8268 8月 20 2014 MANIFEST
-rw-rw-r-- 1 root root 3992 8月 20 2014 README.md
-rw-rw-r-- 1 root root 4189 8月 20 2014 README.Solaris
drwxrwxr-x 2 root root 4096 8月 20 2014 server
-rw-rw-r-- 1 root root 291 8月 20 2014 setup.cfg
-rwxrwxr-x 1 root root 3337 8月 20 2014 setup.py
drwxrwxr-x 4 root root 4096 8月 20 2014 testcases
-rw-rw-r-- 1 root root 1733 8月 20 2014 THANKS
-rw-rw-r-- 1 root root 1827 8月 20 2014 TODO
4.查看软件说明书
cat -n /usr/local/README.md
23 Required: 使用python语言写的,该软件依靠python环境.要求python版本大于2.4,
24 - [Python >= 2.4](http://www.python.org)
25
26 Optional:
27 - [pyinotify >= 0.8.3](https://github.com/seb-m/pyinotify)
28 - Linux >= 2.6.13
29 - [gamin >= 0.0.21](http://www.gnome.org/~veillard/gamin)
31 To install, just do: 安装步骤
32
33 tar xvfj fail2ban-0.8.12.tar.bz2 解压
34 cd fail2ban-0.8.12 进入到fail2ban-0.8.12
35 python setup.py install 安装python
5.安装
根据上面的33-35行来进行安装
...
[root@server fail2ban-0.8.14]python setup.py install
Writing /usr/share/fail2ban/fail2ban-0.8.14-py2.7.egg-info
Please do not forget to update your configuration files.
They are in /etc/fail2ban/. 安装后的主目录为/etc/fail2ban
[root@server /]# ll /etc/fail2ban/
总用量 32
drwxr-xr-x 2 root root 4096 11月 27 21:53 action.d 动作文件夹
-rw-rw-r-- 1 root root 1525 8月 20 2014 fail2ban.conf 日志级别的文件夹
drwxr-xr-x 2 root root 6 11月 27 21:53 fail2ban.d
drwxr-xr-x 2 root root 4096 11月 27 21:53 filter.d 条件文件夹
-rw-rw-r-- 1 root root 19316 8月 20 2014 jail.conf 主配置文件(所有的拦截都是该文件夹中配置)
drwxr-xr-x 2 root root 6 11月 27 21:53 jail.d
安装好之后使用python -v来查看python的版本是否大于2.4
[root@server fail2ban-0.8.14]# python -V
Python 2.7.5
该软件提供的启动脚本在源码文件夹中
[root@server ~]# cd /usr/local/fail2ban-0.8.14/
[root@server files]# ll /usr/local/fail2ban-0.8.14/files/
总用量 56
...
drwxrwxr-x 2 root root 42 8月 20 2014 nagios
-rwxrwxr-x 1 root root 2141 8月 20 2014 redhat-initd 启动脚本
...
将该启动脚本复制到系统的启动文件中
[root@server files]# cp redhat-initd /etc/rc.d/init.d/fail2ban.sh
注意:一定要将其设置为开机自启动
[root@server files]# chkconfig --add fail2ban.sh
[root@server fail2ban-0.8.14]# iptables -nvL 查看防火墙就会发现添加了一个防火墙`
Chain fail2ban-SSH (1 references)
pkts bytes target prot opt in out source destination
0 0 REJECT all -- * * 169.254.218.160 0.0.0.0/0 reject-with icmp-port-unreachable
811 48072 RETURN all -- * * 0.0.0.0/0 0.0.0.0/0
启动文件查找的方法
因为一个标准的系统启动脚本中都有chkconfig启动级别的字段(说明信息),使用grep就能迅速查找到该文件
在fail2ban的源码目录下查找
[root@server fail2ban-0.8.14]# grep chkconfig ./* -R --color
./files/redhat-initd:# chkconfig: - 92 08
[root@server fail2ban-0.8.14]# pwd
/usr/local/fail2ban-0.8.14
2.案例需求
需求
设置条件:ssh远程配置5分钟内3次密码登录失败,禁止该ip地址反问主机1小时,1小时后该限制自动解除,用户可重新登录
实现方法:
因为动作文件action.d/iptables.conf 以及日志匹配条件文件 fiter.d/sshd.conf 安装后是默认存在的.基本不会做任何修改,所有主要需要设置的在jail.conf文件,启用sshd服务的日志分析,指定动作阀值既可.
修改jail.conf主配置文件
[root@server fail2ban]# vim jail.conf
27 [DEFAULT] 全局设置
40 # "bantime" is the number of seconds that a host is banned. 正常时间是主机被禁止的秒数
41 bantime = 600
...
94 [ssh-iptables] 这是我们需要配置的
95
96 enabled = false 是否启用----修改为true
97 filter = sshd
98 action = iptables[name=SSH, port=ssh, protocol=tcp]
99 sendmail-whois[name=SSH, dest=you@example.com(you@example.com将这块修改为邮箱地址,就会向该邮箱发送报警信息,前提是系统已经安装了sendmail服务,且为启动状态), sender=fail2ban@example.com, sendername="Fail2Ban"]
100 logpath = /var/log/sshd.log 修改日志文件(因为是通过日志文件来判断用户登录错误的次数的)
sshd服务的日志文件是/var/log/secure
将其修改为 --- /var/log/secure,
101 maxretry = 5 最大尝试次数为: 修改为3次
添加 findtime = 300 也就是5分钟,
添加 bantime = 3600 禁止1小时
重启fail2ban服务
[root@server fail2ban]# systemctl restart fail2ban
查看状态
[root@server ~]# systemctl status fail2ban
验证
[root@client ~]# ssh root@169.254.218.130
root@169.254.218.130's password:
Permission denied, please try again.
root@169.254.218.130's password:
Permission denied, please try again.
root@169.254.218.130's password:
Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password). 输入三次就会发现被禁止登录了
[root@client ~]# ssh root@169.254.218.130
ssh: connect to host 169.254.218.130 port 22: Connection refused
查看结果
[root@server fail2ban-0.8.14]# fail2ban-client status
Status
|- Number of jail: 1
`- Jail list: ssh-iptables
[root@server fail2ban-0.8.14]# fail2ban-client status ssh-iptables
Status for the jail: ssh-iptables
|- filter
| |- File list: /var/log/secure
| |- Currently failed: 0
| `- Total failed: 3
`- action
|- Currently banned: 1
| `- IP list: 169.254.218.160
`- Total banned: 1
注意:1.如果将自己的登录客户端给禁止掉了,解决方法就是更换自己的ip地址登录后将日志文件secure清空或者等限制时间超过后在登录
2,fail2ban默认监控的是22端口,如果修改sshd服务的端口号,就需要将fail2ban默认监控的端口号改掉,如果不修改是访问不了的
修改jail.conf的主配置文件中的默认端口号
[root@server fail2ban-0.8.14]# vim /etc/fail2ban/jail.conf
...
98 action = iptables[name=SSH, port=ssh(ssh就为端口22,可修改为相应的端口号,例如:3000), protocol=tcp]
...
[root@server action.d]# vim /etc/fail2ban/action.d/iptables.conf
...
61 port = ssh (将ssh修改为相应的端口号)
...
保存文件既可