ssh 密码、证书登录

密码认证

开放、关闭root 登录

需要修改sshd_config 配置;

PermitRootLogin prohibit-password 或 PermitRootLogin yes

设置为yes 即允许root 用户进行登录;

开启密码进行认证登录

PasswordAuthentication yes

ChallengeResponseAuthentication : Change to yes to enable challenge-response passwords (beware issues with some PAM modules and threads)

如果关闭密码登录将PasswordAuthentication 与ChallengeResponseAuthentication  值均设置为no 即可;

 

修改了sshd_config 文件后,需要对ssh 服务进行重启,如:systemctl restart sshd

证书认证

为了安全,需要使用到ssh 协议,可以通过证书进行认证通讯,如:A 机器为服务器,B为客户端,B需要连接到A机器进行相关配置操作,那么首先需要登录到A机器,那么这里可以直接用证书进行;

1、客户端生成公钥:

如ubuntu:

root@master:~# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
/root/.ssh/id_rsa already exists.
Overwrite (y/n)? y
Enter passphrase (empty for no passphrase):
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:
SHA256:IClHSxcxTrljPy8oWLbXlJt2HbOdaXt/SGdC+iMavmk root@master
The key's randomart image is:
+---[RSA 3072]----+
|    o *+         |
|   o *..         |
|  . = o.         |
|   o .+.      .  |
|     . oS.   o   |
|    o   =   + o o|
|   + . + =.. B B |
|  . o o *.Eo+ O o|
|     o . +=o o.++|
+----[SHA256]-----+

如果是windows 系统,那么可能需要借助第三方工具,如mobaxterm,命令与ubuntu 相同,mobaxterm 工具生成的米要对存在工具用户目录,如:C:\Users\test\AppData\Roaming\MobaXterm\home\.ssh;

如果是本级已安装了openssh工具,那么可以直接在windows 命令行生成,如:

C:\Users\wenlong>ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (C:\Users\test/.ssh/id_rsa):

存放的秘钥对在用户的跟目录下的.ssh 下;

2、拷贝公钥到服务器的鉴权文件;

服务器上的用户目录下有.ssh 的目录,再目录下的authorized_keys 文件中添加前面生成的公钥,即id_rsa.pub;

注意点:

1、authorized_keys 文件可能不存在,不存在直接创建即可;

2、这里的用户目录也需要注意,客户端使用哪个用户登录到服务端,那么就再系统的哪个目录下进行添加,如:客户端使用test 用户登录服务器A,那么需要添加在/home/test/.ssh/authorized_keys 文件中;

3、authorized_keys 文件的权限:所有者用户设置为可读写,其余不允许,即:600

4、Selinux在未关闭的情况下,然仍无法通过验证,出现:Server refused our key,那么需要进行设置:# restorecon -R -v /home,

该命令的作用了恢复/home 目录下所有文件的默认selinux安全上下文属性。

再次查看~/.ssh/authorized_keys文件的selinux安全上下文属性,发现已经改变:

[test@localhost ~]$ ll -Z ~/.ssh/authorized_keys
-rwxr--r--. test tesst unconfined_u:object_r:user_home_t:s0 /home/test/.ssh/authorized_keys

 

 

posted @ 2023-03-10 11:08  橘子飞飞  阅读(728)  评论(0编辑  收藏  举报