frame

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In Register

[SCP] Transferring files between servers with SCP

GiedriusGiedrius Guest
edited April 2020 in Various Tutorials

SCP(Secure Copy) is an easy way to transfer files between two servers that uses SSH so it provides the same level of security. This method requires access to both servers, so you won't be able to transfer files without having access to the remote servers.

Copying files from a local server to a remote server

Below is the syntax for the command to copy local data to another server

scp test_file.txt remote_server_username@remote_server_IP:/remote/directory

Explanation of syntax is below:

scp - main command;
test_file.txt - a file that is being transferred;
remote_server_username - the user that you will be using to connect to a remote server, for example, root;
remote_server_IP - the IP of a remote server;
/remote/directory - this is the directory path that a file will be copied to. Make sure it is created on the remote server.

The example s below:

scp 1G.test [email protected] /home

The example shows how a file "1G.test" is being copied to a remote server's /home folder. Once the command is executed, you will be asked to confirm if you want to continue the connection.

Are you sure you want to continue connecting (yes/no)?

Enter "yes" and you will be prompted for a remote server password. As soon as you enter it, the file transfer begins and you will be able to see the progress bar as well.

Copying files from a remote server to a local server

This is basically the vice versa of the previous command, you can check the syntax below:

scp remote_server_username@remote_server_IP:/remote/file.txt /local/directory

The command is simple, just remember that now you are declaring the remote server's file path and local server's directory to copy.

Copying files from a remote server to another remote server

If you have more than two servers, you can use scp command to copy files from one remote server to another remote server, it will help you to manage and move files without actually connecting to each server every time manually. The syntax is below:

scp remote_server_username1@remote_server1_IP:/files/file.txt remote_server_ username2@remote_server2_IP:/files

This example will copy the file "/files/file.txt", from server1 to a folder "/files" on server2. Also, you will need login details to both servers.

Additional options for SCP

You can copy multiple files by simply declaring a few of them:

scp file1.txt file2.txt archive.gzip remote_server_username@remote_server_IP:/home

The example above shows how to multiple files to a remote server.

You can copy a full directory as well with option -r, here is the example:

scp -r /home/backups/backup1 remote_server_username@remote_server_IP:/home

The example above shows how to use option -r, to copy the whole folder "/home/backups/backup1" to a remote server.

SCP command works with custom SSH port as well, the option for that is -P, for example:

scp -P 1234 file1.txt remote_server_username@remote_server_IP:/home

The example above allows transferring the file to a remote server that uses custom SSH port 1234 with an option -P. By default, the command uses default SSH port 22.

Note. The SCP works on Windows as well, you will just need some SSH client.

More information on SCP documentation can be found here.

Tagged:
Sign In or Register to comment.

Time4VPS

Learn how to install a web and database server, email, FTP client or other applications. Discover and share information on server security or optimization recommendations.
Feel free to join our constantly expanding community, participate in discussions, strengthen your knowledge on Linux and Windows server management!
© 2013 - 2024 Time4VPS. All rights reserved.

Get In Touch