File transfer between two servers

Table of contents

Method 1: Using SCP

Method 2: Use rsync

Use SSH keys


File transfer between two servers can usually use SCP (Secure Copy Protocol) or rsync command. Both methods are commonly used tools on UNIX and Linux systems for safely copying files and directories. Here are examples using both methods:

Method 1: Using SCP

SCP is a method of securely copying files between two servers. You can use the scp command to achieve this. Here is an example:

#Copy files from local server to remote server

scp /path/to/local/file.txt username@remote_server:/path/to/remote/directory/

The parameters here are explained as follows :

  1. /path/to/local/file.txt is the file path on the local server.
  2. username is the username on the remote server.
  3. remote_server is the address of the remote server.
  4. /path/to/remote/directory/ is the directory on the remote server where files are stored.

Example:

#Copy files from local server to remote server

scp /home/user/documents/file.txt [email protected]:/var/www/html/

This will copy file.txt from the local server to the remote server's /var/www/html/ directory.

Method 2: Use rsync

rsync is another powerful tool for file synchronization and replication. It can copy files incrementally, copying only the changed parts, thus improving efficiency. Here is an example:

# Use rsync to copy files from the local server to the remote server

rsync -avz /path/to/local/file.txt username@remote_server:/path/to/remote/directory/

The parameters here are explained as follows:

-avz means copy in archive mode, retain file attributes and recursive directories, and use compression to transfer data.

/path/to/local/file.txt is the file path on the local server.

username is the username on the remote server.

remote_server is the address of the remote server.

/path/to/remote/directory/ is the directory on the remote server where files are stored.

Example:

# Use rsync to copy files from the local server to the remote server

rsync -avz /home/user/documents/file.txt [email protected]:/var/www/html/

This will copy file.txt from the local server to the remote server's /var/www/html/ directory while preserving the file attributes.

Choosing to use SCP or rsync depends on your needs and preferences. Generally speaking, if you need simple file transfer, SCP is sufficient. If more complex file synchronization and backup are required, rsync is a more powerful tool.

Use SSH keys

The above method requires you to re-enter the account password after each file is transferred. This is suitable for one or several files, but if there are a large number of files that need to be transferred, an SSH key pair needs to be generated for file transfer.

  1. Generate SSH key pair:

    Open a terminal on your local computer and execute the following command to generate an SSH key pair (if the key pair already exists, skip to the next step):

    ssh-keygen -t rsa

    This will generate a public key file (usually ~/.ssh/id_rsa.pub) and a private key file (usually ~/.ssh/id_rsa). Do not share private key files.

  2. Copy the public key to the remote server:

    Use scpor other file transfer method to copy the public key to the remote server where you want to transfer the file. Here is scpan example of use:

    scp ~/.ssh/id_rsa.pub user@remote_server_ip:~/.ssh/

    Please replace userthe username on the remote server remote_server_ipwith the IP address or hostname of the remote server.

  3. Add the public key to the remote server's authorization file:

    Log in to the remote server and add the public key to ~/.ssh/authorized_keysthe file or create it if it does not exist:

    cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys

    If there are multiple public keys, each public key should be on a new line.

  4. Set the correct permissions:

    .sshMake sure the folders and files on the remote server authorized_keyshave the correct permissions to ensure SSH works properly. Permissions can be set using the following command:

    chmod 700 ~/.ssh chmod 600 ~/.ssh/authorized_keys
  5. Test SSH key authentication:

    Execute the following command on your local computer to ensure that you can log in to the remote server through SSH key authentication without entering a password:

    ssh user@remote_server_ip

    If everything is set up correctly, you should be able to log in to the remote server without a password.

  6. Use scpor rsyncperform file transfer:

    Now you can use scpor rsyncto transfer files between the two servers without entering a password:

    scp file.txt user@remote_server_ip:/path/to/destination/

    or

    rsync -avz -e "ssh" /path/to/source/ user@remote_server_ip:/path/to/destination/

    These commands will use SSH key authentication for secure file transfer.

Make sure your SSH key pair is well protected and do not share the private key file to ensure security.

おすすめ

転載: blog.csdn.net/qq_42458954/article/details/133079451