一、openssh
当主机中开启openssh服务,那么就对外开放了远程连接的接口
openssh服务的服务端sshd
openssh服务的客户端ssh
二、在客户端连接sshd的方式
ssh 服务端用户@服务端ip地址
例如ssh [email protected] 在客户端用ssh命令连接172.25.254.229主机的root用户
三、给ssh服务添加新的认证方式 KEY认证
1.生成锁和钥匙
[root@localhost ~]# ssh-keygen 生成密钥的命令
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 保存加密字符的文件用默认
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase): 可以为空,如果想为空必须大于4位
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:
00:4f:b0:72:40:9f:a6:fb:91:86:d2:69:bc:7c:75:5f root@localhost
The key's randomart image is:
+--[ RSA 2048]----+
| .o o.. |
| o * |
| . * o |
| = . |
| . S |
| o + .. . E |
|. B +. . . . |
| + +.. . |
| o.. |
+-----------------+
输入ssh-keygen后一直回车即可,选取默认方式:
2.加密ssh用户的认证
在服务端
ssh-copy-id -i /root/.ssh/id_rsa.pub [email protected]
ssh-copy-id 加密命令
-i 指定密钥
/root/.ssh/id_rsa.pub 密钥
root 加密用户
3.验证
解密文件传输到客户端
scp /root/.ssh/id_rsa [email protected]:/root/.ssh/
在客户端
ssh [email protected] 连接不需要密码
在服务端
rm -fr /root/.ssh/authorized_keys 当此文件被删除,客户端解密文件失效
在客户端删除rm -fr /root/.ssh/id_rsa之后也没办法免密执行。
在服务端
scp /root/.ssh/id_rsa.pub /root/.ssh/authorized_keys 从新生成锁文件,解密文件功能恢复
scp /root/.ssh/id_rsa [email protected]:/root/.ssh/ 重新发送解密文件
截图中我所用的server是服务端,desktop是客户端。如下图所示,解锁钥匙传过去之后,客户端连接就不需要密码。
四、sshd的安全配置
禁止原始认证方式
78(代表行,我们可以使用:set nu打开序号)
PasswordAuthentication no|yes 开启或关闭ssh的默认认证方式
关闭了ssh默认认证时超级用户也无法连接。
做以下三个命令时必须保证 78行 为yes,即打开ssh的认证。
48 PermitRootLogin no|yes 开启或关闭root用户的登陆权限,结束完成注释掉
49(行数,这个可以任意在任意位置书写) AllowUsers Jane
用户白名单,当前设定是只允许Jane登陆。下图呈现结果除了westos可以登录其他用户连同超级用户也不可以。
50(行数,这个可以在任意位置书写) DenyUsers student
用户黑名单,当前设定是只不允许student登陆。
黑白名单不可以同时存在,做完实验之后都注释掉即可。黑白名单可以在任意地方书写。
rsync [参数] file username@ip:/dir
rsync -r 同步目录
-l 不忽略链接
-p 不忽略文件权限
-t 不忽文件时间戳
-g 不忽文件所有组
-o 不忽文件所有人
-D 不忽略设备文件
rsync远程数据同步,比scp命令同步命令快的多。下图可以看出来跳过了很多文件,进而加快了速度。
rsync -r /mnt [email protected]:/mnt/ 仅仅复制内容
rsync -r /mnt/ [email protected]:/mnt/ 连同复制本身
同步链接: