目的: 实现自动SCP(加密远程复制文件),以节省人工手动操作远程复制文件
假设本地服务器为web 远程服务器为202...***

在web上的工作
1.生成ssh客户密匙


#ssh-keygen -t rsa

屏幕显示(为生成公匙密匙文件和等,全部回车即可)
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):   

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:
d1:69:34:80:56:2a:2d:df:70:2d:10:ac:63:5e:1e:1c root@web

生成后,将本地的id_rsa.pub复制到远程的./ssh目录下,更改名称为authorized_keys2

sftp root@远程IP

sftp 202.108.59.23
Connecting to 202.***.***.***...
root@202.***.***.***'s password: (输入密码)

sftp> 
sftp>cd .ssh
sftp>put id_rsa.pub
Uploading id_rsa.pub to /.ssh/id_rsa.pub
id_rsa.pub                                  # 上传id_rsa.pub到远程,以便形成密匙对应关系

传输完毕后

ssh登录到远程目标服务器(202...***)

#cd .ssh
#mv id_rsa.pub authorized_keys2         形成密匙对应关系

然后回到web1下

# scp 202.108.59.23:/usr/local/apache2/logs/hoho /root/.ssh/    复制远程的hoho文件到本地的/root/.ssh目录下,无需输入密码
hoho                              100%   40KB  40.2KB/s   00:00 (屏显状态值,显示已传输完毕,文件可正常使用)
文档更新时间: 2019-07-09 10:57   作者:月影鹏鹏