Sunday, March 15, 2009

072 在OpenSSH中创建一个无需密码的登录

你可以通过以下3个步骤,使用ssky-keygen 和ssh-copy-id命令,不用密码就可以登录到远程的Linux服务器。
ssh-keygen用于创建公钥和密钥,ssh-copy-id 命令可以把本地的公钥复制到远程主机的authorized_keys 文件中,ssh-copy-id 同时会给远程主机的主目录中的~/.ssh和~/.ssh/authorized_keys设置适当的权限。
第一步:在本地通过ssh-key-gen创建公钥和密钥
jsmith@local-host$ ssh-keygen

Generating public/private rsa key pair.
Enter file in which to save the key
(/home/jsmith/.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/jsmith/.ssh/id_rsa.
Your public key has been saved in
/home/jsmith/.ssh/id_rsa.pub.
The key fingerprint is:
33:b3:fe:af:95:95:18:11:31:d5:de:96:2f:f2:35:f9
jsmith@local-host

第二步:使用ssh-copy-id将公钥复制到远程主机上
jsmith@local-host$ ssh-copy-id -i ~/.ssh/id_rsa.pub
remote-host

jsmith@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中。
第三步:无需密码登录到远程主机
jsmith@local-host$ ssh remote-host

Last login: Sun Nov 16 17:22:33 2008 from 192.168.1.2

[注意:SSH没有向你询问密码]

jsmith@remote-host$ [现在你已经登录到远程主机]

No comments:

Post a Comment