目录
一、SSH远程管理
- SSH(Secure Shell)是一种安全协议,主要用来实现字符界面的远程登录、远程复制等功能。
- SSH协议对通信双方的数据传输进行了加密处理,其中包括用户登录时输入的用户口令
1.1 配置OpenSSH服务端
- OpenSSH
服务名称 | sshd |
---|---|
服务端主程序 | /usr/sbin/sshd |
服务端配置文件 | /etc/ssh/sshd_config |
- 服务监听选项
[root@localhost ~]# vim /etc/ssh/sshd_config
...
#Port 22 ## 端口号 可以修改
#ListenAddress 0.0.0.0 ## 监听地址 可修改
#ListenAddress :: ## 监听IPV6的地址
# UseDNS no ## 禁用反向解析
- 用户登录控制
[root@localhost ~]# vim /etc/ssh/sshd_config
LoginGraceTime 2m ## 会话时间 一定要设置会话超时时间,防止进程占用
PermitRootLogin no ## 是否允许root管理员登录
MaxAuthTries 6 ## 最大尝试登录次数(输错) 默认尝试次数为3次 需要增加尝试次数(连接次数),默认输入次数才有用
PermitEmptyPasswords no ##是否需要密码登录
AllowUsers jerry admin@61.23.24.25 ## 允许用户登录 @后加终端即允许从固定的终端登录 DenyUsers 即为禁止xxx
MaxSession 10 ## 最大允许10个终端
- 登录验证方式
[root@localhost ~]# vim /etc/ssh/sshd_config
PasswordAuthentication yes ## 启用密码验证
PubkeyAuthentication yes ##密钥对验证
AuthorizedKeysFile .ssh/authorized_keys ## 指定公钥库地址
1.2使用SSH客户端程序
1.2.1 SSH 远程安全登录的控制
命令基本格式
ssh user@host
[root@text02 ~]# ssh root@192.168.233.200
root@192.168.233.200's password:
Last login: Thu Jul 9 17:33:06 2020 from 192.168.233.1
- 限制roort登录
[root@localhost ~]# vim /etc/ssh/sshd_config
#LoginGraceTime 2m
PermitRootLogin no ## 禁止使用root登录
#StrictModes yes
#MaxAuthTries 6
#MaxSessions 10
#PubkeyAuthentication yes
[root@test01 ~]# systemctl restart sshd ##重启ssh服务
[root@text02 ~]# ssh root@192.168.233.200
root@192.168.233.200's password:
Permission denied, please try again. ## 权限拒绝 只能尝试三次
root@192.168.233.200's password:
Permission denied, please try again.
root@192.168.233.200's password:
Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).
- 限制以root远程登录 会有BUG
[root@text02 ~]# ssh tom@192.168.233.200 ## 以用户tom的身份远程登录
tom@192.168.233.200's password:
Last login: Thu Jul 9 12:29:08 2020 from 192.168.233.180
[tom@test01 ~]$ su root ## 以tom切换到root身份
密码:
[root@test01 tom]#
- 需要PAM认证开启,开启su命令wheel组
[root@test01 tom]# vi /etc/pam.d/su
#%PAM-1.0
auth sufficient pam_rootok.so
# Uncomment the following line to implicitly trust users in the "wheel" group.
#auth sufficient pam_wheel.so trust use_uid
# Uncomment the following line to require a user to be in the "wheel" group.
auth required pam_wheel.so use_uid
- 将允许切换的用户加入wheel组
[root@localhost pam.d]# gpasswd -a tom1 wheel
- tom 无法切换 tom1 可以照常切换
[root@text02 ~]# ssh tom@192.168.233.200
tom@192.168.233.200's password:
Last login: Thu Jul 9 18:49:04 2020 from 192.168.233.180
[tom@test01 ~]$ su root
密码:
su: 拒绝权限
[root@text02 ~]# ssh tom1@192.168.233.200
tom1@192.168.233.200's password:
Last login: Thu Jul 9 19:14:08 2020 from 192.168.233.180
[tom1@test01 ~]$ su root
密码:
[root@test01 tom1]#
1.2.2AllowUsers:(白名单)DenyUsers:(黑名单)
- 白名单 黑名单 只能设置一个
- 白名单:仅允许某些用户,拒绝所有人(用在安全性场合较高的地方)
- 黑名单:仅拒绝某些用户,允许所有人 (用在安全性场合较低的地方)
AllowUsers tom wangwu@192.168.195.164 ##只允许tom 还有wangwu从192.168.195.164终端登录 其他都不允许
DenyUsers tom wangwu@192.168.195.164 ## 不允许tom登录,还有wangwu从192.168.195.164终端登录,其他都允许。
设置密码输入错误的次数
- MaxAuthTries 6 ## 最大尝试登录次数(输错) 默认尝试次数为3次
- 需要增加尝试次数(连接次数),默认输入错误次数才有用。
## 在连接端输入
[root@text02 ~]# ssh -o NumberOfPasswordPrompts=8 tom@192.168.233.180 ## 允许输入错误最大为8次,输次数默认为6次
[root@text02 ~]# ssh -o NumberOfPasswordPrompts=8 tom@192.168.233.200
tom@192.168.233.200's password:
Permission denied, please try again.
tom@192.168.233.200's password:
Permission denied, please try again.
tom@192.168.233.200's password:
Permission denied, please try again.
tom@192.168.233.200's password:
Permission denied, please try again.
tom@192.168.233.200's password:
Permission denied, please try again.
tom@192.168.233.200's password:
Received disconnect from 192.168.233.200 port 22:2: Too many authentication failures
Authentication failed.
二、sshd密钥对登录
- 1.创建密钥对(由客户端的用户wang在本地创建密钥对)
公钥文件:id_rsa
公钥文件:id_rsa.pub
[wang@localhost /]$ ssh-keygen -t ecdsa ## 以ecdsa 的方式生成公钥密钥对
Generating public/private ecdsa key pair.
Enter file in which to save the key (/home/wang/.ssh/id_ecdsa): ## 保存密钥的文件位置
Created directory '/home/wang/.ssh'.
Enter passphrase (empty for no passphrase): ## 输入密码 123456
Enter same passphrase again: ## 再次输入
Your identification has been saved in /home/wang/.ssh/id_ecdsa.
Your public key has been saved in /home/wang/.ssh/id_ecdsa.pub.
The key fingerprint is:
SHA256:ltKzaPcb6gL7zcNicKwCmlE2ZCbuq4M47cwzhF9v1kU wang@localhost.localdomain
The key's randomart image is:
+---[ECDSA 256]---+
| |
|. + |
|.= |
| .+ . .E |
|.+ . .. S. |
|+.. + o+ o. |
|+B.. Booo.. |
|B+* o.B++o . |
|o+++ =.+=.o. |
+----[SHA256]-----+
[wang@localhost /]$ ls /home/wang/.ssh/ ## 查看生成的密钥
id_ecdsa id_ecdsa.pub
- 2.上传公钥文件 id_rsa.pub
任何方式均可(共享,FTP,Email,SCP,。。。)
[wang@localhost .ssh]$ ssh-copy-id -i id_ecdsa.pub tom@192.168.233.200 ## 将公钥传输到192.168.233.200tom用户下
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "id_ecdsa.pub"
The authenticity of host '192.168.233.200 (192.168.233.200)' can't be established.
ECDSA key fingerprint is SHA256:YAbzobc07zu/b4duIJhP1ieAiG0KNtiHYPEgMFNu4EI.
ECDSA key fingerprint is MD5:74:3f:cc:f2:2f:b9:d2:a5:cb:b6:1c:e4:f6:da:b0:34.
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
tom@192.168.233.200's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'tom@192.168.233.200'"
and check to make sure that only the key(s) you wanted were added.
- 3 进入另一台主机 查看公钥文件
[root@localhost ~]# cd /home/tom
[root@localhost tom]# ls -a
. .. .bash_history .bash_logout .bash_profile .bashrc .cache .config .mozilla .ssh
[root@localhost tom] vim /.ssh/authorized_keys # 查看文件
- 4、验证
[wang@localhost .ssh]$ ssh tom@192.168.233.200
Enter passphrase for key '/home/wang/.ssh/id_ecdsa': ## 输入创建密钥时设置的密码 123456
Last login: Mon Jun 22 11:51:28 2020 from 192.168.145.1
- 5、面交互下次登录不用输入密码
[wang@localhost .ssh]$ ssh-agent bash ## ssh代理bash(shell环境)功能
[wang@localhost .ssh]$ ssh-add ## 添加交互的密钥
Enter passphrase for /home/wang/.ssh/id_ecdsa: ## 输入密钥
Identity added: /home/wang/.ssh/id_ecdsa (/home/wang/.ssh/id_ecdsa)
[wang@localhost .ssh]$ ssh tom@192.168.233.200
Last login: Sun Jul 12 00:14:00 2020 from 192.168.233.180 ## 远程登录 不需要密码
[tom@localhost ~]$
三、TCP Wrappers控制
- TCP Wrappers 将其他的TCP服务包裹起来,增加了一个安全检测的过程,外来的连接请求必须先通过这层安全检测,获得许可后才能访问真正的服务程序,是一个简易的防火墙。
TCP Wrappers机制 | 调用相应的网络程序 |
---|---|
代为监听窗口21 | vsftpd |
代为监听窗口23 | telnet |
代为监听窗口110 | ipop3 |
代为监听窗口143 | imp |
- TCP Wrappers保护机制的实现方式
- 通过tcpd主程序对其他服务程序进行包装
- 由其他服务程序调用libwrap.so.*链接库
3.1 TCP Wrappers访问策略
- 机制的保护对象为各种网络服务程序,针对访问服务的客户机地址进行访问控制。
- 对应的两个策略文件为 /etc/hosts.allow /etc/hosts.deny
- 先执行/etc/hosts.allow 再执行/etc/hosts.deny
3.2 策略的配置方式
<服务程序列表>:<客户机地址列表>
-
服务列表
多个服务以逗号分隔,ALL表示所有服务 -
客户机地址列表
多个地址以逗号分隔,ALL表示所有服务,允许使用通配符*和?,网段地址,如192.168.1 或者 192.168.1.0/255.255.255.0,区域地址,如.benet.com。
3.3 策略应用实例
- 禁止登录
[root@localhost etc]# vim /etc/hosts.deny ## 所有人无法登录
sshd:all
[wang@localhost .ssh]$ ssh tom@192.168.233.200 ## 以tom身份无法登录
ssh_exchange_identification: read: Connection reset by peer
- 白名单设置
[root@text01 ~]# vim /etc/hosts.allow
sshd:192.168.233.200
[root@text01 ~]# vim /etc/hosts.deny
sshd:all
- 192.168.233.200 客户机
[root@localhost ~]# ssh root@192.168.233.200
root@192.168.233.200's password:
Last login: Sun Jul 12 00:57:45 2020 from 192.168.233.100
[root@text01 ~]# ## 登录成功
- 192.168.233.100 客户机
[root@localhost tom]# ssh root@192.168.233.200
ssh_exchange_identification: read: Connection reset by peer ## 无法登录
[root@localhost tom]#