1. 制做密钥对php
首先在服务器上制做密钥对。登陆到打算使用密钥登陆的帐户,而后执行如下命令:shell
[root@host ~]$ 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. <== 公钥
密钥锁码在使用私钥时必须输入,这样就能够保护私钥不被盗用。固然,也能够留空,实现无密码登陆。服务器
如今,在 root 用户的家目录中生成了一个 .ssh 的隐藏目录,内含两个密钥文件。id_rsa 为私钥,id_rsa.pub 为公钥。ssh
2. 在服务器上安装公钥ide
键入如下命令,在服务器上安装公钥:工具
[root@host .ssh]$ chmod 600 authorized_keys
[root@host .ssh]$ chmod 700 ~/.ssh
3. 设置 SSH,打开密钥登陆功能spa
编辑 /etc/ssh/sshd_config 文件,进行以下设置:.net
RSAAuthentication yes
PubkeyAuthentication yes
另外,请留意 root 用户可否经过 SSH 登陆:rest
PermitRootLogin yes
当你完成所有设置,并以密钥方式登陆成功后,再禁用密码登陆:code
PasswordAuthentication no
最后,重启 SSH 服务:
[root@host .ssh]$ service sshd restart
4. 将私钥下载到客户端,而后转换为 PuTTY 能使用的格式
使用 WinSCP、SFTP 等工具将私钥文件 id_rsa 下载到客户端机器上。而后打开 PuTTYGen,单击 Actions 中的 Load 按钮,载入你刚才下载到的私钥文件。若是你刚才设置了密钥锁码,这时则须要输入。
载入成功后,PuTTYGen 会显示密钥相关的信息。在 Key comment 中键入对密钥的说明信息,而后单击 Save private key 按钮便可将私钥文件存放为 PuTTY 能使用的格式。
从此,当你使用Xshell或 PuTTY 登陆时,能够在左侧的 Connection -> SSH -> Auth 中的 Private key file for authentication: 处选择你的私钥文件,而后便可登陆了,过程当中只需输入密钥锁码便可。
转自:http://blog.csdn.net/permike/article/details/52386868