免密登录详细讲解

首先了解一下ssh的工作过程,在整个通讯过程中,为实现SSH的安全连接,服务端与客户端要经历如下五个阶段:

1、版本号协商阶段:SSH目前包括SSH1和SSH2两个版本,双方通过版本协商确定使用的版本

2、密钥和算法协商阶段:SSH支持多种加密算法,双方根据本端和对端支持的算法,协商出最终使用的算法

3、认证阶段:SSH客户端向服务器端发起认证请求,服务器端对客户端进行认证

4、会话请求阶段:认证通过后,客户端向服务器端发送会话请求

5、交互会话阶段:会话请求通过后,服务器端和客户端进行信息的交互

其中认证阶段有两种认证方法:

1、基于口令的认证(password认证):客户端向服务器发出password认证请求,将用户名和密码加密后发送给服务器,服务器将该信息解密后得到用户名和密码的明文,与设备上保存的用户名和密码进行比较,并返回认证成功或失败消息。

2、基于密钥的认证(publickey认证):客户端产生一对公共密钥,将公钥保存到将要登录的服务器上的那个账号的家目录的.ssh/authorizedkeys文件中。认证阶段:客户端首先将公钥传给服务器端。服务器端收到公钥后会与本地该账号家目录下的authorizedkeys中的公钥进行对比,如果不相同,则认证失败;否则服务端生成一段随机字符串,并先后用客户端公钥和会话密钥对其加密,发送给客户端。客户端收到后将解密后的随机字符串用会话密钥发送给服务器。如果发回的字符串与服务器端之前生成的一样,则认证通过,否则,认证失败。

我们使用第二种认证方式

1.两台机器:第一台机器作为客户端,第二台机器作为服务器,在第一台使用rhce用户免密登录第二台机器

1>先在客户端生成一对公共密钥

[bh@baiheng ~]$ ssh-keygen -t rsa -b 2048 --生成一对公共密钥
Generating public/private rsa key pair.
Enter file in which to save the key (/home/bh/.ssh/id_rsa): key保存的目录默认为家目录下的.ssh/,如果不改变,直接回车
Created directory '/home/bh/.ssh'.
Enter passphrase (empty for no passphrase): --密码,如果不需要,直接回车
Enter same passphrase again: --再次输入密码,直接回车
Your identification has been saved in /home/bh/.ssh/id_rsa.
Your public key has been saved in /home/bh/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:5HAOcT5s+cIIaHsI+SGF34UTYV6y3hqSUWGZlD/qK2A bh@baiheng
The key's randomart image is:
+---[RSA 2048]----+
| ...XOo .        |
|.o **+.= .       |
|+.=.++o O        |
| =.B.oo@ o       |
|  * +.o.S .      |
|.E o.o   .       |
|.. ..            |
|  . .            |
|   ...           |
+----[SHA256]-----+


此时一对密钥生成成功,进入家目录下的.ssh下查看是否生成成功
[bh@baiheng ~]$ cd .ssh/
[bh@baiheng .ssh]$ ll
total 8
-rw------- 1 bh bh 1823 Jan  8 16:22 id_rsa       ---私钥
-rw-r--r-- 1 bh bh  392 Jan  8 16:22 id_rsa.pub   ---公钥

2>将公钥保存到将要登录的服务器上的那个账号的家目录的.ssh/authorizedkeys文件中

ssh-copy-id -i [email protected] ---将公钥保存到将要登录的服务器上的那个账号的家目录的.ssh/authorizedkeys文件中

ssh-copy-id -i [email protected]

bh--所要复制到的用户
192.168.245.129--服务器的的ip

至此免密登录还没设置成功,所以此次登录还要输入密码

[bh@baiheng .ssh]$ ssh-copy-id -i [email protected]
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/bh/.ssh/id_rsa.pub"
The authenticity of host '192.168.245.129 (192.168.245.129)' can't be established.
ECDSA key fingerprint is SHA256:QgFDtok3CICC4Nwf7A5QtUBBlb0+Ksfvyiu6p1K8VsA.
Are you sure you want to continue connecting (yes/no/[fingerprint])? 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
[email protected]'s password: --输入密码

Number of key(s) added: 1  ---密钥增加,表面添加成功

Now try logging into the machine, with:   "ssh '[email protected]'"
and check to make sure that only the key(s) you wanted were added.


此时免密登录配置完成

3、测试

[bh@baiheng ~]$ ssh [email protected] --登录服务器。免密登录
Red Hat Enterprise Linux release 8.5 (Ootpa)
4.18.0-348.el8.x86_64
localhost.localdomain
/bin/bash
Activate the web console with: systemctl enable --now cockpit.socket

This system is not registered to Red Hat Insights. See https://cloud.redhat.com/
To register this system, run: insights-client --register

Last login: Sun Jan  8 20:34:23 2023 from 192.168.245.128
[bh@bh ~]$ 

2.禁止root用户远程登录和设置三个用户sshuser1, sshuser2, sshuser3, 只允许sshuser3登录,不允许sshuser1, sshuser2登录

1>创建三个用户sshuser1, sshuser2, sshuser3,并设置密码

1、创建用户sshuser1, sshuser2, sshuser3
[root@baiheng ~]# useradd sshuser1
[root@baiheng ~]# useradd sshuser2
[root@baiheng ~]# useradd sshuser3

2、给用户设置密码
[root@baiheng ~]# echo '123' | passwd --stdin sshuser1
Changing password for user sshuser1.
passwd: all authentication tokens updated successfully.
[root@baiheng ~]# echo '123' | passwd --stdin sshuser2
Changing password for user sshuser2.
passwd: all authentication tokens updated successfully.
[root@baiheng ~]# echo '123' | passwd --stdin sshuser3
Changing password for user sshuser3.
passwd: all authentication tokens updated successfully.

2、 使用vim打开/etc/ssh/sshd_config文件,设置登录白名单(默认没有这个配置,需要自己手动添加),允许远程登录的用户。如果名单中没有的用户,则提示拒绝登录。按G进入文件底部添加AllowUsers sshuser3,仅让sshuser3登录,不添加sshuser1, sshuser2,即拒绝登录。

[root@baiheng ~]# vim /etc/ssh/sshd_config--进入配置文件

AllowUsers sshuser3 --进入文件底部添加

3、测试

C:\Users\37419>ssh [email protected] ---sshuser1拒绝登录
[email protected]'s password:
Permission denied, please try again.
[email protected]'s password:
Permission denied, please try again.
[email protected]'s password:

C:\Users\37419>ssh [email protected] ---sshuser2拒绝登录
[email protected]'s password:
Permission denied, please try again.
[email protected]'s password:
Permission denied, please try again.
[email protected]'s password:

C:\Users\37419>ssh [email protected] -----sshuser3成功登录
[email protected]'s password:
Red Hat Enterprise Linux release 8.5 (Ootpa)
4.18.0-348.el8.x86_64
localhost.localdomain
/bin/bash
Activate the web console with: systemctl enable --now cockpit.socket

This system is not registered to Red Hat Insights. See https://cloud.redhat.com/
To register this system, run: insights-client --register

Last login: Sun Jan  8 16:53:28 2023 from 192.168.245.1
[sshuser3@baiheng ~]$

你可能感兴趣的:(服务器,运维)