SCP免密码传输

  通常用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

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值