linux实现免密登陆的方法是通过将客户端的公钥存储到服务器的密钥文件中,客户端登陆服务器时,服务器通过验证客户端的密钥,认证成功即可登陆成功,具体方法如下:

  • 在客户端使用ssh-keygen命令生成密钥文件,windows客户端需要下载openssh客户端

[root@centos8 ~]# ssh-keygen

Generating public/private rsa key pair.

Enter file in which to save the key (/root/.ssh/id_rsa): #密钥存储路径,默认为用户目录下的.ssh文件夹

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.

………………….

  • 将客户端公钥文件上传到服务器

ssh-copy-id -i /root/.ssh/id_rsa userid@server_ip #上传密钥文件
# /root/.ssh/id_rsa替换为你的密钥文件路径
# userid@server_ip改为服务器的登陆用户名和IP地址
# 输入正确的密码即可将密钥文件上传到服务器

也可以将密钥文件的内容,复制到服务器用户目录下的./ssh/authorized_keys的文件中

在客户端就可以实现免密登陆

ssh userid@server_ip