## 一、SSH服务
sshd服务安装-ssh命令使用方法
### 1、SSHD服务
介绍:[SSH](http://baike.baidu.com/subview/16184/5909253.htm) 协议:安全外壳协议。为 [Secure Shell](http://baike.baidu.com/view/2118359.htm) 的缩写。SSH 为建立在应用层和传输层基础上的安全协议。
作用:sshd服务使用SSH协议可以用来进行远程控制, 或在计算机之间传送文件
相比较之前用telnet方式来传输文件要安全很多,因为telnet使用明文传输,是加密传输。
### 2、服务安装:
需要安装OpenSSH 四个安装包:
OpenSSH软件包,提供了[服务端](http://baike.baidu.com/view/1087294.htm)后台程序和[客户端](http://baike.baidu.com/view/930.htm)工具,用来加密远程控件和[文件传输](http://baike.baidu.com/view/543341.htm)过程中的数据,并由此来代替原来的类似服务。
```
[root@centos7 ~]#yum install openssh openssh-clients openssh-server -y
```
**启动ssh服务**
```
[root@localhost init.d]# systemctl start sshd
[root@localhost init.d]# systemctl enable sshd
```
1、ssh [远程主机用户名] @[远程服务器主机名或IP地址]
如果用root进程登录远程主机可以这样:
```sh
[root@hopu ssh]# ssh 192.168.3.27
```
普通用户:
```sh
[root@client~]#useradd test && echo 123456 | passwd --stdin test
更改用户 test 的密码 。
passwd:所有的身份验证令牌已经成功更新。
[root@server~]#ssh test@192.168.3.27
[root@client~]#cat .ssh/known_hosts
192.168.3.27 ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBOFpFGHHueBhYmGMdbS8EtQsrF4vCns2CzNKRAN9XkwL7NoZbGRuDionbLyiDallwUOA7f52Rpvq8vSJLL8ftDM=
```
第一次登录服务器时系统没有保存远程主机的信息,为了确认该主机身份会提示用户是否继续连
接,输入yes 后登录,这时系统会将远程服务器信息写入用户主目录下的$HOME/.ssh/known_hosts文件中,下次再进行登录时因为保存有该主机信息就不会再提示了
例:修改sshd服务默认监听的端口为222
```
[root@centos7 ~]#vim/etc/ssh/sshd_config
改:
Port 22
为:
Port 222
[root@centos7 ~]# systemctl restart sshd
```
修改完端口默认端口后,登录方法:
```sh
[root@centos7 ~]#ssh -p 222 192.168.3.27
[root@centos7 ~]#ssh -p 222 -l test 192.168.3.27
```
设置sshd服务器绑定的IP 地址,0.0.0.0 表示侦听所有地址
## 二、配置双机互信
```
[root@centos7 ~]#ssh-keygen #一直回车
[root@centos7 ~]#ssh-copy-id -i 10.0.0.20
```
## 三、通过 fail2ban 来防止暴力破解 ssh
```
[root@centos7 ~]#tar xf fail2ban-0.8.14.tar.gz
[root@centos7 ~]#cd fail2ban-0.8.14/
[root@centos7 ~/fail2ban-0.8.14]#python setup.py install
#生成服务启动脚本
[root@centos7 ~/fail2ban-0.8.14]#cp files/redhat-initd /etc/init.d/fail2ban
[root@centos7 ~/fail2ban-0.8.14]#chkconfig fail2ban on
[root@centos7 ~/fail2ban-0.8.14]#chkconfig --list
#修改配置文件
[root@centos7 ~/fail2ban-0.8.14]#cat /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]
enabled = true
filter = sshd
action = iptables[name=SSH, port=ssh, protocol=tcp]
sendmail-whois[name=SSH, dest=you@example.com, sender=fail2ban@example.com, sendername="Fail2Ban"]
logpath = /var/log/secure
bantime = 3600 #禁止用户IP访问主机1小时
findtime = 300 #在5分钟内内出现规定次数就开始工作
maxretry = 3 #3次密码验证失败
#启动服务
[root@centos7 ~/fail2ban-0.8.14]#/etc/init.d/fail2ban restart
Restarting fail2ban (via systemctl): [ 确定 ]
#清空日志开启收集信息
[root@centos7 ~/fail2ban-0.8.14]#>/var/log/secure
[root@centos7 ~/fail2ban-0.8.14]#/etc/init.d/fail2ban restart
Restarting fail2ban (via systemctl): [ 确定 ]
#查看防火墙
[root@centos7 ~/fail2ban-0.8.14]#iptables -L
tips:如果做错了,想清空一下记录,还原 只需要把 > /var/log/secure 清空就可以了。
[root@centos7 ~]# > /var/log/secure
[root@centos7 ~]# systemctl restart sshd
```
**注意事项:如果要更改默认监听端口号需要修改以下四个配置文件才可生效**
```
[root@client ~]#vim /etc/ssh/ssh_config
[root@client ~]#vim /etc/fail2ban/action.d/iptables.conf
[root@client ~]#vim /etc/fail2ban/jail.conf
[root@client ~]#vim /etc/ssh/sshd_config
#重启服务
[root@client ~]#systemctl restart fail2ban
[root@client ~]#systemctl restart sshd
#查看端口号
[root@client ~]#ss -ntl
```
## 四、通过 pam 模块来防止暴力破解 ssh
[root@centos7 ~]# vim /etc/pam.d/sshd
#在第一行下面添加一行:
auth required pam_tally2.so deny=3 unlock_time=600 even_deny_root root_unlock_time=1200
说明:尝试登陆失败超过3次,普通用户600秒解锁,root用户1200秒解锁
手动解除锁定:
查看某一用户错误登陆次数:
```sh
[root@centos7 ~]#pam_tally2 --user test
Login Failures Latest failure From
test 7 10/11/22 23:10:32 192.168.2.2
```
清空 test 用户错误登陆次数:
```
[root@centos7 ~]#pam_tally2 --user test --reset
Login Failures Latest failure From
test 0
```