SSH密钥设置
建立密钥对使用如下命令
ssh-keygen
执行ssh-keygen显示如下
[root@host ~]$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory '/root/.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.
The key fingerprint is:
0f:d3:e7:1a:1c:bd:5c:03:f1:19:f1:22:df:9b:cc:08 root@host
有以下几处需要注意,可根据个人情况定是输入密钥锁码,或直接按 Enter 留空
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
生成的私钥和公钥会保存在/root/.ssh路径,为id_rsa 私钥,id_rsa.pub为公钥
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
在服务器上安装公钥
进入/root/.ssh 执行如下命令
cat id_rsa.pub >> authorized_keys
如此便完成了公钥的安装。为了确保连接成功,请保证下面文件权限正确
[root@host .ssh]$ chmod 600 authorized_keys
设置ssh秘钥登录功能,编辑/etc/ssh/sshd_config文件,进行如下设置:
RSAAuthentication yes #RSA认证
PubkeyAuthentication yes #pubkey认证
另外,请留意 root 用户能否通过 SSH 登录:
PermitRootLogin yes #允许root登
当你完成全部设置,并以密钥方式登录成功后,再禁用密码登录:
PasswordAuthentication no
最后,重启 SSH 服务:
sudo service ssh restart
sshkey生成参考
本地私钥连接
在putty上登录时需要把密钥格式转换成putty可用格式,下载putty密钥生成工具 puttygen.exe进行如下操作
加载私钥文件
点击显示所有文件,找到你的私钥文件
保存转换后的私钥文件
输入要连接的主机IP ,选中SSH的Auth
点击browse选中你生成的ppk文件
点击open打开
使用PuTTY密钥认证机制远程登录Linux