[关闭]
@ysongzybl 2015-06-08T04:33:20.000000Z 字数 1429 阅读 906

SSH login without password

linux SSH


Your aim

You want to use Linux and OpenSSH to automate your tasks. Therefore you need an automatic login from host A / user a to Host B / user b. You don't want to enter any passwords, because you want to call ssh from a within a shell script.

How to do it

First log in on A as user a and generate a pair of authentication keys. Do not enter a passphrase:

  1. a@A:~> ssh-keygen -t rsa
  2. Generating public/private rsa key pair.
  3. Enter file in which to save the key (/home/a/.ssh/id_rsa):
  4. Created directory '/home/a/.ssh'.
  5. Enter passphrase (empty for no passphrase):
  6. Enter same passphrase again:
  7. Your identification has been saved in /home/a/.ssh/id_rsa.
  8. Your public key has been saved in /home/a/.ssh/id_rsa.pub.
  9. The key fingerprint is:
  10. 3e:4f:05:79:3a:9f:96:7c:3b:ad:e9:58:37:bc:37:e4 a@A
  11. The key's randomart image is:
  12. +--[ RSA 2048]----+
  13. | E .. . |
  14. | . . .. o |
  15. |.. . . .o B |
  16. |.o. . .+ B o |
  17. |oo . S o o |
  18. |+ . . |
  19. |+. |
  20. |+ |
  21. |.. |
  22. +-----------------+

Now use ssh to create a directory ~/.ssh as user b on B. (The directory may already exist, which is fine):

  1. a@A:~> ssh b@B mkdir -p .ssh
  2. b@B's password:

Finally append a's new public key to b@B:.ssh/authorized_keys and enter b's password one last time:

  1. a@A:~> cat .ssh/id_rsa.pub | ssh b@B 'cat >> .ssh/authorized_keys'
  2. b@B's password:

From now on you can log into B as b from A as a without password:

a@A:~> ssh b@B

A note from one of our readers: Depending on your version of SSH you might also have to do the following changes:

Error:
Agent admitted failure to sign using the key.
Use command

  1. ssh-add

to fix this error

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注