码迷,mamicode.com
首页 > 其他好文 > 详细

远程使用 key 证书登录

时间:2018-07-27 16:17:26      阅读:188      评论:0      收藏:0      [点我收藏+]

标签:注释   auth   文件   取消   回车   软件   vim   文件中   ref   

远程登录使用 key 证书登录

配置密钥登录的方法很简单,过程不外乎:
一:开启 sshd 服务的密钥验证功能;
二:生成密钥;
三:添加密钥到账户的 authorized_keys 文件中授权;
四:保存私钥和公钥到本地电脑添加到 SSH 客户端软件;
五:本地测试密钥登录成功后,关闭 sshd 服务的密码验证;

1.开启密钥登录:
vim /etc/ssh/sshd_config
将以下三行配置文件前的注释取消,然后保存重启sshd服务
(系统不同,所需取消的注释也会有变化)
RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys
service sshd restart

2.生成用户密钥文件
ssh-keygen -t rsa
连续三次回车即可,ssh-keygen 有三种加密方法,rsa只是其中一种,详解和用法可以自行搜索下。
然后进入到用户家目录.ssh 目录中可以看到生成了两个文件
ls -l /root/.ssh
total 8
-rw------- 1 root root 1675 Sep 7 13:18 id_rsa
-rw-r--r-- 1 root root 393 Sep 7 13:18 id_rsa.pub
id_rsa 是用户私钥文件,id_rsa.pub 即为公钥文件

3.导入公钥文件id_rsa.pub 到 authorized_keys 进行授权:
cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys

4.下载密钥文件 id_rsa 和 id_rsa.pub 到本地电脑上妥善保存,添加到 ssh 客户端(个人喜欢使用xshell )

5.测试了使用密钥能够 SSH 登陆后可以再次编辑 /etc/ssh/sshd_config 文件,关闭密码验证。
千万得确认能够使用密钥文件登陆到服务器才能关闭密码验证,不然就没法远程了。
vim /etc/ssh/sshd_config
把# PasswordAuthentication yes 改成PasswordAuthentication no
PasswordAuthentication no
保存退出,重启sshd服务
现在已经配置完成,为了安全考虑最好是从服务器上删除刚才生成的两个密钥文件 id_rsa 和 id_rsa.pub
ssh 简单识别系统一键部署

远程使用 key 证书登录

标签:注释   auth   文件   取消   回车   软件   vim   文件中   ref   

原文地址:http://blog.51cto.com/12384628/2151123

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!