How to use the Linux ‘scp’ command without a password to make remote backups
How to create a public and private key pair to use ssh and scp without using a password, which lets you automate a remote server backup process.
目标:通过 server 上的脚本,通过 cron 每天备份文件到 client。
- h1,sunyongfeng@192.168.204.168,client,用于保存服务器备份文件
- h2,root@172.18.111.192,server,需要通过脚本,每天备份文件到 192.168.204.168
步骤:
- 在 server 端生成 rsa 密钥对,
ssh-keygen -t rsa
1 | root ssh-keygen -t rsa |
- 拷贝公钥到 client 192.168.204.168,注意此时从 172.18.111.192 scp 拷贝时还需要密码。
scp .ssh/id_rsa.pub sunyongfeng@192.168.204.168:/home/sunyongfeng/.ssh/authorized_keys
1 | root@ubuntu:~# scp .ssh/id_rsa.pub sunyongfeng@192.168.204.168:/home/sunyongfeng/.ssh/authorized_keys |
- 验证从 server 172.18.111.192 scp 到 192.168.204.168 已不需要密码。下文同时确认 ssh 远程 192.168.204.168 不需要密码。
1 | root@ubuntu:~# scp .ssh/id_rsa.pub sunyongfeng@192.168.204.168:/home/sunyongfeng/.ssh/authorized_keys |