Linux下scp无需输入密码获取文件——修正普遍错误版本

通常用scp命令通过ssh获取对方linux主机文件的时候都需要输入密码确认,如果需要在主机上做一个自动获取文件的shell脚本,这样显然是不行的,通过下面的设置,可以不需要输入密码,直接获到文件。 这里假设主机A用来获到主机B的文件。

在主机A上执行如下命令来生成配对密钥:

ssh-keygen -t rsa

显示信息如下:

Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory ‘/root/.ssh’.
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:
45:13:ec:31:f9:3d:25:54:7b:5f:03:40:ba:e9:76:cd root@bindiry-ubuntu

按三次回车。

为 ~/.ssh 目录设置权限:

chmod -R 700 .ssh

将 .ssh 目录中的 id_rsa.pub 文件复制到 主机B 的 ~/.ssh/ 目录中,并改名为 authorized_keys ,同样给该目录设置权限chmod -R 700 .ssh。

到主机A中执行命令和主机B建立信任,例(假设主机B的IP为:192.168.0.3):

scp ~/.ssh/id_rsa.pub 192.168.0.3:/root/.ssh/authorized_keys

下面就可以用scp命令不需要密码来获取主机B的文件了

scp root@192.168.0.3:/var/account/temp.log /var/account/

 

posted @ 2013-01-16 10:30  八木_八木  阅读(1187)  评论(0)    收藏  举报