1. 程式人生 > >配置SSH登入無密碼驗證

配置SSH登入無密碼驗證

如用scp  從linux機器A  複製到  linux 機器B

一般用scp複製需要密碼,此時配置好ssh無密碼驗證,就方便多了。

1:機器A上執行:ssh-keygen -t rsa   會在/root/.ssh下面生成兩個檔案,id_rsa  ,id_rsa.pub

[[email protected] .ssh]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
/root/.ssh/id_rsa already exists.
Overwrite (y/n)? y
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:
63:d5:d8:97:c7:65:34:4e:e7:89:69:3d:e4:15:11:d6

[email protected]
The key's randomart image is:
+--[ RSA 2048]----+
|               [email protected]|
|           +  X=E|
|          o o+o*+|
|         .  .. ..|
|        S        |
|       . .       |
|                 |
|                 |
|                 |
+-----------------+

2.我們需要的是公鑰:id_rsa.pub  檔案。

[[email protected] .ssh]# ll
total 16
-rw-------. 1 root root  816 Aug 12 09:10 authorized_keys
-rw-------. 1 root root 1675 Aug 12 09:12 id_rsa
-rw-r--r--. 1 root root  408 Aug 12 09:12 id_rsa.pub
-rw-r--r--. 1 root root  790 Aug 11 16:21 known_hosts

3:將公鑰傳給B機器。

[[email protected] .ssh]# ssh-copy-id -i /root/.ssh/id_rsa.pub

[email protected]
[email protected]'s password: 
Permission denied, please try again.
[email protected]'s password: 
Now try logging into the machine, with "ssh '[email protected]'", and check in:

  .ssh/authorized_keys

to make sure we haven't added extra keys that you weren't expecting.

4:預設會在B機器的authorized_keys檔案後面追加 A機器的公鑰認證

[[email protected] .ssh]# pwd
/root/.ssh
[[email protected] .ssh]# ll
total 16
-rw-------. 1 root root 1224 Aug 12 09:17 authorized_keys
-rw-------. 1 root root 1675 Aug 12 09:12 id_rsa
-rw-r--r--. 1 root root  408 Aug 12 09:12 id_rsa.pub
-rw-r--r--. 1 root root  790 Aug 11 16:21 known_hosts