Linux uses scp command to automatically backup files

In order to use scp under Linux without entering a password, ssh is another way to authenticate with a key pair.

1. One-to-one scp file transfer between two machines
1. Use the ssh-keygen command to generate a key. During the generation process, except for entering the password, all other default values ​​are selected: press Enter.
[root@xxx root]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):#Enter
Created directory '/root/.ssh '.
Enter passphrase (empty for no passphrase):#Enter
Enter same passphrase again:#Enter
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:
e0:f0:3b:d3:0a:3d:da:42:01:6a:61:2f:6c:a0:c6:e7 root@xxx
Generating RSA keys:
Key generation complete.
Two files will be generated in the user directory ~/.ssh/, id_rsa is the key, and id_rsa.pub is the public key

2. Change the public key access attribute in this key pair to 755
[root@xxx root]#chmod 755 /root/.ssh/id_rsa.pub

3. Copy id_rsa.pub to the backup machine and change its name to authorized_keys. Note that the name must be authorized_keys
[root@xxx root]# scp /root/.ssh/id_rsa. pub 192.168.1.92:/root/.ssh/authorized_keys
root@xxx's password:
id_rsa.pub 100% 218 3.2MB/s 00:00
[root@xxx root]#

and then use ssh scp sftp to access that machine, No need to enter a password. This enables automatic file transfer using the shell.

2. Multi-machine multi-to-one scp file transfer
       If files from multiple machines need to be transferred to one machine, it should be noted that the name of the machine is unique and cannot be repeated.
1. Each sending machine needs to do steps 1 and 2 of [two machines one-to-one scp file transfer], generate a local key and public key, and assign permissions to the public key id_rsa.pub
2. Transfer each machine The public keys of the machine are sent to the receiving machine, and these public keys are combined into an authorized_keys file on the receiving machine, preferably each public key is separated
3. On the receiving machine, send the authorized_keys file to each sending machine, and the location is placed under the respective user directory ~/.ssh/.
4. Execute ssh-add on each sending machine.
   ssh-add is used to add the key to the ssh-agent. SSH can communicate with the ssh-agent to obtain the key, so that the user does not need to enter the password manually.
5. Test that the sender sends the file to the receiver.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326430254&siteId=291194637