sshd

openssh

当主机中开启openssh服务,那么就对外开放了远程连接的接口
#openssh服务的服务端

sshd

#openssh服务的客户端


ssh



#### 2.在客户端连接sshd的方式 ####
ssh   服务端用户@服务端ip地址

例如
ssh   [email protected]    ##在客户端用ssh命令连接172.25.254.155主机的root用户

[root@localhost ~]# ssh [email protected]
The authenticity of host '172.25.254.200 (172.25.254.200)' can't be established.
ECDSA key fingerprint is eb:24:0e:07:96:26:b1:04:c2:37:0c:78:2d:bc:b0:08.
Are you sure you want to continue connecting (yes/no)?     ##当当前主机第一次连接陌生主机时
                            ##会自动建立.ssh/know_hosts
                            ##这个文中记录的是连接过的主机信息
                            #
[email protected]'s password:             #输入密码连接成功
Last login: Fri Mar 30 02:05:52 2018 from 172.25.254.100
[root@localhost ~]# exit                #表示退出当前连接
logout
Connection to 172.25.254.200 closed.


"注意:以上连接方式是不能打开远程主机的图形功能的如果需要打开远程主机图形功能需要输入 -X"
ssh -X [email protected]

cheese




#### 3.给ssh服务添加新的认证方式 KEY认证####

(本机ip为172.25.254.113)scp file1 [email protected]:/root/Desktop
                        把自己的文件file1以超级用户的身份传给ip为172.25.254.213的用户在其/root/Desktop下存放
scp [email protected]:/root/Desktop/file2  .   把ip为172.25.254.213用户的/root/Desktop/file2文件复制到当前
(服务机)生成密钥ssh-keygen ------(空格)默认存放的位置-------(空格)密码可空格若要设置则需4位及以上----生成密钥

ls /root/.ssh    查看密钥是否以生成     id_rsa    id_rsa.pub
给要保护的机子上锁(保护id为172.25.254.213的root用户)  ssh-copy-id -i /root/.ssh/id_rsa.pub [email protected]
想要什么机子(172.25.254.113)可以免密码连接到(172.25.254.213)将文件传输给其
scp /root/.ssh/id_rsa  [email protected]:/root/.ssh     (只能将此钥匙放到此位置才能被读)
用ip为(172.25.254.113)的机子测试连接(172.25.254.213)
ssh [email protected]   不用输入密码就可以连接
若无此密钥侧要输入密码才能进入。
传密钥时必须要保证钥传给的用户先前连接过此时才会生成/root/.ssh/目录


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..            |
+-----------------+

2.加密ssh用户的认证
#在服务端
ssh-copy-id -i /root/.ssh/id_rsa.pub [email protected]
ssh-copy-id        ##加密命令
-i            ##指定密钥
/root/.ssh/id_rsa.pub    ##密钥
root            ##加密用户
172.25.254.200        ##主机ip

3.验证
#解密文件传输到客户端
scp /root/.ssh/id_rsa  [email protected]:/root/.ssh/

#在客户端
ssh [email protected]        #连接不需要密码

#在服务端
rm -fr /root/.ssh/authorized_keys    ##当此文件被删除,客户端解密文件失效

#在服务端

cp /root/.ssh/id_rsa.pub /root/.ssh/authorized_keys    ##从新生成锁文件,解密文件功能恢复


设置两台虚拟机ip并相互连接并实行 rm -fr /root/.ssh/


此时若修改文件/etc/ssh/sshd_config连接变为密码连接不可此时就算你知道连接的用户密码若你没有密钥也不能连接

此时删掉服务端的锁文件,此时就算客户端有钥匙也无法开启




#### 4.sshd的安全配置 ####
1.禁止原始认证方式
78 PasswordAuthentication no|yes    ##开启或关闭ssh的默认认证方式
48 PermitRootLogin no|yes        ##开启或关闭root用户的登陆权限
79 AllowUsers westos            ##用户白名单,当前设定是只允许westos登陆
80 DenyUsers linux            ##用户黑名单,当前设定是只不允许linux登陆

此都是在文件/etc/ssh/sshd_config  文件中设置第78行为是否可以使用密码连接第48行为是否可以连接此id的超级用户设置

黑白名单的设置需在文件空白处填写设置黑白名单一般黑白名单只能出现一个,不能同时出现。


此时还可以用密码连接



猜你喜欢

转载自blog.csdn.net/xdmaidou/article/details/79915723
今日推荐