首页 > 代码库 > ssh无密码登陆服务器

ssh无密码登陆服务器

网上该类教程很多,但还是记录下,方便以后查询的时候使用。

两台服务器,内网ip分别是:

172.16.3.91 (本地机)

172.16.3.92 (远程机)

现在想在本地机上通过ssh 172.16.3.92登陆到远程机而不需要密码。


1.在本地机上用ssh-keygen 创建公钥私钥。

[root@rs-1 ~]# ssh-keygen -t rsa

Generating public/private rsa key pair.

Enter file in which to save the key (/root/.ssh/id_rsa): 

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:

dc:fc:ea:b8:bb:74:d7:75:4b:6a:27:82:28:27:be:d6 root@rs-1


这个过程会提示输入密码,记住直接按回车就行,不要输入密码!要是输入了密码,一会访问的时候就需要输入密码了。

创建之后能看到如下两个文件

[root@rs-1 .ssh]# ll

total 24

-rw------- 1 root root 1675 May  8 07:18 id_rsa

-rw-r--r-- 1 root root  391 May  8 07:18 id_rsa.pub


id_rsa是私钥

id_rsa.pub是公钥, 主要给远程机使用的


2.把公钥传到远程机上去。

[root@rs-1 .ssh]# scp id_rsa.pub root@172.16.3.92:~/.ssh/authorized_keys

Address 172.16.3.92 maps to localhost, but this does not map back to the address - POSSIBLE BREAK-IN ATTEMPT!

root@172.16.3.92‘s password: 

id_rsa.pub                                                                                                                                  100%  391     0.4KB/s   00:00    


3.ssh链接时候就不需要密码了,直接登陆到远程机

[root@rs-1 .ssh]# ssh 172.16.3.92

Address 172.16.3.92 maps to localhost, but this does not map back to the address - POSSIBLE BREAK-IN ATTEMPT!

Last login: Thu May  8 07:19:28 2014 from 172.16.3.91

[root@rs-2 ~]# 




参考:http://blog.csdn.net/gongqingkui/article/details/9023439