当前位置:首页 > 服务器运维 > linux > 设置ssh使用密钥登陆

设置ssh使用密钥登陆

DChen3年前 (2021-03-31)linux1.36 K0
1、制作密钥对
首先在服务器上制作密钥对。首先用密码登陆到你打算使用密钥登录的账号,然后执行以下命令:
[~] ssh-keygen    // 建立密钥对
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):  // 按 Enter 
Created directory '/root/.ssh'. 
Enter passphrase (empty for no passphrase):    // 输入密钥锁码,或直接按 Enter 留空
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: 0f:d3:e7:1a:1c:bd:5c:03:f1:19:f1:22:df:9b:cc:08 root@host
密钥锁码在使用私钥时必须输入,这样就可以保护私钥不被盗用。
现在,在root用户的家目录中生成了一个.ssh的隐藏目录,内含两个密钥文件。id_rsa为私钥,id_rsa.pub为公钥。
2、在服务器上安装公钥
[~] cd .ssh
[.ssh] cat id_rsa.pub >> authorized_keys
为了确保连接成功,请保证以下文件权限正确
[.ssh] chmod 600 authorized_keys
[.ssh] chmod 700 ~/.ssh

3、设置SSH,打开密钥登录功能,编辑/etc/ssh/sshd_config文件,进行如下设置:

[~] vim /etc/ssh/sshd_config
RSAAuthentication yes
PubkeyAuthentication yes
请留意root用户能否通过ssh登录:
PermitRootLogin yes
当你完成全部设置,并以密钥方式登录成功后,再禁用密码登录:
PasswordAuthentication no
最后,重启ssh服务
[~] service sshd restart
4、配置SecureCRT使用公钥认证
    1)、下载id_rsa私钥,id_rsa.pub公钥到本地(采用SecureCRT生成则忽略)
    2)、在SecureCRT中,打开会话选项
    3)、在“分类”(Category)中选择SSH2
    4)、在右边的“身份验证(Authentication)”中选择“公钥(PublicKey)”
    5)、点击旁边的“属性(Properites)”,选择“使用会话公钥设置(Use session public keysetting)”
    6)、在“使用身份文件(Use identify file)”的输入框中,选择之前生成的id_rsa或者Identify
    7)、注意:id_rsa和id_rsa.pub或者Identify和Identify.pub要保存在一起,并符合keyname和keyname.pub的对应格式


取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

版权声明:本文由“憨小猪”发布,如需转载请注明出处。

本文链接:https://www.phper.red/post/39.html

标签: linuxssh

发表评论

访客

看不清,换一张

◎欢迎参与讨论,请在这里发表您的看法和观点。