通常用scp命令通过ssh获取对方linux主机文件的时候都需要输入密码 确认,如果需要在主机上做一个自动获取文件的shell脚本,这样显然 是不行的,通过下面的设置,可以不需要输入密码,直接获到文件。
这里假设 主机A用来获到主机B的文件。
1. 在主机A上执行如下命令来生成配对密钥:
[postgres@192 data]$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/postgres/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/postgres/.ssh/id_rsa.
Your public key has been saved in /home/postgres/.ssh/id_rsa.pub.
The key fingerprint is:
b7:12:93:e4:2e:99:f4:a0:a4:24:8d:b4:2d:db:65:8b postgres@192.168.2.105
The key's randomart image is:
+--[ RSA 2048]----+
| |
| |
| . . |
|.oo o . |
|ooo..oo S . |
| o+o+o.* + . |
| ..E..+ + . |
| . . |
| |
+-----------------+
[postgres@192 data]$
按三次回车。
2. 为 ~/.ssh 目录设置权限:
chmod -R 755 .ssh
将 .ssh 目录中的 id_rsa.pub 文件复制到 主机B 的 ~/.ssh/ 目录中,并改名为 authorized_keys ,同样给该目录设置权限。
3. 到主机A中执行命令和主机B建立信任,例(假设主机B的IP 为:192.168.0.3):
scp ~/.ssh/id_rsa.pub 192.168.0.3:/home/postgres/.ssh/authorized_keys
4. 下面就可以用scp命令不需要密码来获取主机B的文件了
5. 配置好后遇到的问题
scp aa.txt 192.168.2.103:/home/postgres/
Nasty PTR record "192.168.2.103" is set up for 192.168.2.103, ignoring
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: UNPROTECTED PRIVATE KEY FILE! @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Permissions 0755 for '/home/postgres/.ssh/id_rsa' are too open.
It is required that your private key files are NOT accessible by others.
This private key will be ignored.
bad permissions: ignore key: /home/postgres/.ssh/id_rsa
postgres@192.168.2.103's password:
权限问题导致
解决:
来只要把~/.shh/id_rsa权限降到0600就ok了
输入命令
[postgres@192 ~]$ chmod 600 ~/.ssh/id_rsa
[postgres@192 ~]$
然后就可以免密钥传输了。
参考:
http://blog.chinaunix.net/uid-23823046-id-2657079.html
http://blog.csdn.net/menglei8625/article/details/7395087