ItsmeITItsmeIT
  • Linux
    • Debian
    • Ubuntu
  • PC/Windows
Reading: How to Transfer Files Between Linux Servers Using SCP and SSH
Share
Notification Show More
Font ResizerAa
ItsmeITItsmeIT
Font ResizerAa
  • Categories
    • Linux
    • Debian
    • Ubuntu
    • PC/Windows
Have an existing account? Sign In
Follow US
© 2025 ItsmeIT. All rights reserved.

Home » Linux » Debian » How to Transfer Files Between Linux Servers Using SCP and SSH

How to Transfer Files Between Linux Servers Using SCP and SSH

avatar
By
Loibv
avatar
ByLoibv
The ItsmeIT team – delivering cutting-edge updates, tech trends, and insider knowledge from the world of technology.
Follow:
Last updated: August 26, 2025

SCP (secure copy) is an extremely useful command-line tool for quickly and securely transferring files between two servers. Personally, I have used SCP hundreds of times for web projects or data backups, and I always find it both simple and powerful. By leveraging SCP with SSH, you can easily copy and move files and directories between Linux or Ubuntu servers. SCP uses SSH encryption and authentication to ensure the security of your file transfers. I’ve used this method dozens of times to sync files between servers, and it’s never let me down—not even once with large backups.

Why Use SCP?

In my experience, SCP stands out because it’s fast, doesn’t require extra setup, and keeps everything encrypted over SSH—what more do you need?

  • Fast and straightforward: No need to install extra software.
  • Secure: Uses SSH connections; all data is encrypted.
  • Flexible: Supports files, folders, SSH keys, and custom ports.
  • Highly compatible: Works well on all Linux distributions (Ubuntu, CentOS, Debian, etc.)

4 Ways to Use SCP to Transfer Data in Linux/Ubuntu

As we mentioned earlier, you can transfer any data, files, or directories from localhost to a server (VPS) or from one server to another. Here are 4 methods:

Method 1: Copy File from Localhost to Server using SSH

Suppose you have a backup file named mydb_backup.sql located in the /home/itsmeit/backups/ directory on your local machine. You want to transfer it to a server with the IP address 203.113.88.120, using the ubuntu login account, and save it in the /var/backups/ directory.

scp /home/itsmeit/backups/mydb_backup.sql ubuntu@203.113.88.120:/var/backups/

In this command:

  • /home/itsmeit/backups/mydb_backup.sql: the path of the backup database file on your local machine
  • ubuntu: the user name on the server
  • 203.113.88.120: the IP address of the server
  • /var/backups/: the destination directory on the server where the file will be saved

After running the command, the terminal will prompt you to enter the password for the ubuntu account on the server. If the password is correct, the file will be copied to the /var/backups/ directory on the server.

Method 2: Transfer File to Server Using SCP with SSH Key

If your server uses SSH key-based authentication, you can use the -i option to specify the path to your private key file when using the scp command. This allows you to securely transfer files from your local machine or between servers without needing a password.

Command syntax:

scp -i <path_to_private_key> <source_file_path> <username>@<server_ip_address>:<destination_file_path>

Where:

  • <path_to_private_key>: Path to your .pem or .ppk SSH private key file
  • <source_file_path>: Full path of the file you want to transfer
  • <username>: Username to log in to the server
  • <server_ip_address>: IP address of the destination server
  • <destination_file_path>: Folder path on the server where you want to store the file

Example:

You have a plugin file my-plugin.zip on your local machine located at:

/home/itsmeit/projects/my-plugin.zip

You want to transfer this file to your WordPress server with IP 45.77.11.22, using the root account, and you are using the SSH private key vps-key.pem. You want to store the file in the directory /var/www/html/wp-content/plugins/ on the server.

SCP Command:

scp -i ~/vps-key.pem /home/itsmeit/projects/my-plugin.zip root@45.77.11.22:/var/www/html/wp-content/plugins/

Explanation:

  • -i ~/vps-key.pem: Path to your SSH private key (vps-key.pem) used to authenticate the secure connection to the server.
  • /home/itsmeit/projects/my-plugin.zip: Path to the file my-plugin.zip on your local machine that you want to transfer.
  • root@45.77.11.22: The root user on the server with IP 45.77.11.22.
  • /var/www/html/wp-content/plugins/: The destination directory on the server where you want to save the plugin file.

After transferring the file to the server:

Once the file is transferred, SSH into the server and extract the file to the WordPress plugin directory as follows:

cd /var/www/html/wp-content/plugins/
unzip my-plugin.zip

Once the plugin’s unzipped into /wp-content/plugins/, I just log into the dashboard, go to the Plugins section, and hit ‘Activate’—same routine every time.

transfers data to vps using ssh key.
Screenshot showing plugin file successfully transferred to the web folder.

In the screenshot above, we have successfully transferred the zip file from Ubuntu (localhost) to the Server via SCP.

Method 3: Using SCP to Transfer File to Server with SSH Key + Port

If your server uses a dedicated port for SSH connections instead of the default port 22, you can use the -P option to specify the port for the scp command. This allows you to transfer files between servers or from your local machine to a server that uses a non-standard port for SSH connections.

Command syntax:

scp -P <port_number> -i <path_private_key> <source_file_path> <username>@<server_ip_address>:<destination_file_path>

Where:

  • -P <port_number>: Specifies the port number for SSH connections if the server does not use the default port 22.
  • -i <path_private_key>: Path to your SSH private key file (.pem or .ppk).
  • <source_file_path>: Full path of the file you want to transfer from your local machine.
  • <username>: Username on the destination server.
  • <server_ip_address>: IP address of the destination server.
  • <destination_file_path>: The directory on the server where you want to store the file.

Example:

Let’s say your server is using port 2200 for SSH connections, and you want to transfer the file backup.tar.gz located at /home/itsmeit/backup.tar.gz from your local machine to the server. The server has the IP address 45.77.11.22, and you are using the vps-key.pem SSH private key for authentication. You want to store the file in the directory /var/www/backups/ on the server.

SCP Command:

scp -P 2200 -i ~/vps-key.pem /home/itsmeit/backup.tar.gz root@45.77.11.22:/var/www/backups/

Explanation:

  • -P 2200: Specifies that SSH should use port 2200 instead of the default port 22.
  • -i ~/vps-key.pem: Path to your private SSH key (vps-key.pem) used for secure authentication.
  • /home/itsmeit/backup.tar.gz: Path to the file backup.tar.gz on your local machine.
  • root@45.77.11.22: The root user on the server with IP 45.77.11.22.
  • /var/www/backups/: The destination folder on the server where the file should be saved.

After transferring the file:

After running the above command, the backup.tar.gz file will be transferred to the server. You can SSH into the server and extract the backup file as follows:

cd /var/www/backups/
tar -xzvf backup.tar.gz

Once it’s unpacked, I can dig into the backup files or start restoring things as needed.

This method is useful when your server is configured with a custom SSH port, and you need to specify that port in the SCP command. Using the -P option ensures that your SCP command connects to the correct port for the SSH connection.

transfers data using ssh key and port 22
File successfully transferred from localhost to server using a custom SSH port

Method 4: Transfer Folder from Local Machine to Server Using SCP

So far, we’ve just looked at moving single files. But in real-world projects, I often need to transfer entire directories — like a local website folder from my Desktop to a server. In those cases, I just add the -r flag to the scp command to copy the whole folder along with everything inside it.

Command Syntax:

scp -r <source_directory_path> <username>@<server_ip_address>:<target_directory>

Explanation of parameters:

  • -r: Copy the directory and all of its contents recursively.
  • <source_directory_path>: The path to the directory you want to transfer from your local machine.
  • <username>: The username on the destination server.
  • <server_ip_address>: The IP address of the destination server.
  • <target_directory>: The target directory on the server.

Example:

Suppose you have a directory named my_website_files located at /home/itsmeit/Desktop/my_website_files/, and you want to transfer it to the /var/www/html/ directory on a server with IP address 45.77.11.22 using the root user.

SCP Command:

scp -r /home/itsmeit/Desktop/my_website_files root@45.77.11.22:/var/www/html/

This command will transfer the entire my_website_files directory from your Desktop to the /var/www/html/ directory on your server.

Using SCP with SSH Keys and Custom Ports

If your server uses SSH key authentication and runs on a non-default port (other than 22), you need to combine the -i (private key) and -P (port) options in the scp command.

Command Syntax:

scp -P <port_number> -i <private_key_path> -r <source_directory_path> <username>@<server_ip_address>:<destination_directory_path>

Where:

  • -P <port_number>: Specifies the SSH port (if it’s not the default 22).
  • -i <private_key_path>: Path to your private key file.
  • -r: Recursively copy the directory and its contents.
  • <source_directory_path>: The directory on your local machine.
  • <destination_directory_path>: The target directory on the server.

Example: SCP with SSH Key and Custom Port (2200).

Suppose your server:

  • Uses SSH port: 2200
  • Uses SSH key: vps-key.pem
  • IP address: 45.77.11.22
  • Directory to send: /home/itsmeit/Desktop/my_website_files/
  • Account: root
  • Destination directory: /var/www/html/

The SCP command would be:

scp -P 2200 -i ~/vps-key.pem -r /home/itsmeit/Desktop/my_website_files root@45.77.11.22:/var/www/html/

After transferring the directory:

You can SSH into the server to verify by running:

cd /var/www/html/
ls

If everything went right, ls should show the my_website_files directory sitting there, ready to go. Now, you can access, edit, or deploy the content of your website from that directory.

Transfer of the directory to the VPS completed
Transfer of the directory to the VPS completed

Using scp -r allows you to copy the entire directory structure efficiently and easily. I once had to move a website with an image library close to 50GB, and scp -r was truly a lifesaver at that time. It’s the ideal choice when you need to deploy a website from your local machine to a server, back up configuration data, or synchronize content between servers — especially useful for those who work remotely frequently.

Share This Article
Facebook Reddit Telegram Threads
avatar
ByLoibv
Follow:
The ItsmeIT team – delivering cutting-edge updates, tech trends, and insider knowledge from the world of technology.
Leave a Comment

Leave a Reply Cancel reply

You must be logged in to post a comment.

Most Popular

Install Docker on Linux
How to Install Docker on Linux (Ubuntu, Debian, RHEL, AlmaLinux)
Install Exim4 configure mail server Ubuntu
How to Set Up a Mail Server Using Exim4 on Ubuntu/Linux
Install PHP 8.3 Debian
How to Install PHP 8.1/8.2/8.3 on Debian 11/12
ItsmeITItsmeIT
Follow US
© 2025 ItsmeIT. All Rights Reserved.
  • Privacy Policy
  • Terms of Service
Logo ItsmeIT logo
Welcome Back!

Sign in to your account

Continue with Google
Register   |Lost your password?