现在的位置: 首页 > 综合 > 正文

SSH无密码远程访问Linux主机

2018年05月13日 ⁄ 综合 ⁄ 共 1948字 ⁄ 字号 评论关闭

http://www.yunsec.net/a/school/xtrm/linux/2012/0107/9853.html

http://www.linuxso.com/linuxxitongguanli/2351.html

http://www.360doc.com/content/11/0630/21/7102324_130684333.shtml

SSH免密码远程访问Linux主机

分2种情况讨论:本机是Linux或者Windows

1.本机是Linux

使用下例中ssky-keygen和ssh-copy-id,仅需通过3个步骤的简单设置而无需输入密码就能登录远程Linux主机。
ssh-keygen 创建公钥和密钥。
ssh-copy-id 把本地主机的公钥复制到远程主机的authorized_keys文件上。
ssh-copy-id 也会给远程主机的用户主目录(home)和~/.ssh, 和~/.ssh/authorized_keys设置合适的权限 。

步骤1:
用 ssh-key-gen 在本地主机上创建公钥和密钥

root@local-host$ ssh-keygen -t  rsa
Enter file in which to save the key (/home/root/.ssh/id_rsa):[Enter key]
Enter passphrase (empty for no passphrase): [Press enter key]
Enter same passphrase again: [Pess enter key]
Your identification has been saved in /home/root/.ssh/id_rsa.
Your public key has been saved in /home/root/.ssh/id_rsa.pub.
The key fingerprint is: 33:b3:fe:af:95:95:18:11:31:d5:de:96:2f:f2:35:f9
root@local-host

步骤2:
用 ssh-copy-id 把公钥复制到远程主机上

root@local-host$ ssh-copy-id -i ~/.ssh/id_rsa.pub  root@192.168.0.3
root@remote-host‘s password:
Now try logging into the machine, with ―ssh ?remote-host‘‖, and check in:
.ssh/authorized_keys to make sure we haven‘t added extra keys that you weren‘t expecting.
[注: ssh-copy-id 把密钥追加到远程主机的 .ssh/authorized_key 上.]

步骤3:
直接登录远程主机

root@local-host$ ssh remote-host
Last login: Sun Nov 16 17:22:33 2008 from 192.168.1.2
[注:
SSH 不会询问密码.] 

root@remote-host$
[注: 你现在已经登录到了远程主机上]

2.本机是Windows(使用Putty登录)

经常需要使用Putty登录Linux服务器进行操作。平时都使用用户名和密码直接登录,后来发现比较麻烦,准备采用Public / Private Key的方式直接验证。下面是我使用的方法,可供参考。

Putty提供了一个生成Key的工具 —— Putty Key Generator,在安装Putty时附带安装上了。打开它,点击“Generate“按钮,并不停的摇晃鼠标,即完成Key的生成。接着的过程比较重要:

第一步:单击“Save private Key“,将私钥保存至本地目录。
第二步:将上方的”Public Key for pasting into OpenSSH authorized_keys file:“中的内容copy到剪贴板。
第三步:修改putty针对某服务器的登录设置, Connection部分的SSH / Auth 设置中,将刚才保存的Private Keys选择进来(Private key file for authenticaion)。此外,还需要在Connection / Data 的 Login details 的 Auto-login username 中填入登录的用户名。
第四步:登录服务器,第一次需要输入用户名和密码,然后进入~/.ssh目录,打开authorized_keys文件,将刚才copy to clipboard的内容复制到这个文件中。如果这个文件已有内容,一定要另起新的一行再粘贴。保存退出。

点击查看原图

经过上面几步,再登录的时就可以不需要输入用户名和密码,直接登录了。

#########

相关:ssh-add命令 (ssh-add id_rsa)

抱歉!评论已关闭.