How to Securely Transfer Files Between Servers Using the Linux scp Command

When you are logged into a local Linux machine and need to transfer a large log file or a database dump to a remote server, setting up an FTP server is entirely unnecessary and introduces severe security risks. Because almost every Linux server in the world runs an SSH (Secure Shell) daemon, you already possess a highly secure, encrypted tunnel between the two machines. You can leverage this existing tunnel to copy files seamlessly using the scp (Secure Copy Protocol) command directly from your terminal.

Understanding the Syntax

The scp command functions almost identically to the standard Linux cp (copy) command. You must specify a Source (what you want to copy) and a Destination (where you want it to go). The only difference is that one of those locations will include remote network coordinates.

The basic syntax always looks like this:
scp [options] [source] [destination]

To specify a remote server, you format it just like an SSH login:
username@server_ip_address:/path/to/directory/

Copying a Local File to a Remote Server (Pushing)

This is the most common use case. You have a file on your laptop (e.g., website_update.zip), and you want to upload it to your cloud server.

  1. Open your local terminal.
  2. Type the command: scp website_update.zip [email protected]:/var/www/html/
  3. Press Enter.

If you are not using SSH keys, the terminal will pause and ask you to type the password for the admin user on the remote server. Once authenticated, scp will display a progress bar as the file is encrypted, pushed across the network, and saved into the /var/www/html/ directory.

Copying a Remote File to Your Local Machine (Pulling)

You can also run scp in reverse. If a remote server generated a backup file (e.g., backup.sql) and you want to download it to your local laptop’s Downloads folder, you swap the source and destination.

scp [email protected]:/home/admin/backups/backup.sql ~/Downloads/

Notice that the remote server coordinates are now the Source (the first argument), and your local laptop directory (~/Downloads/) is the Destination.

Copying Entire Directories Recursively

By default, scp only copies single files. If you attempt to point it at an entire folder, it will throw an error. To copy a directory and all of the files and sub-folders contained within it, you must use the -r (recursive) flag.

scp -r /local/folder/project_files/ [email protected]:/home/admin/

This will recreate the project_files folder and its entire hierarchy on the remote server.

Specifying a Custom SSH Port

For security purposes, many system administrators change the default SSH port from 22 to a random high number (like 2222) to avoid automated scanning bots. If your remote server uses a custom port, the standard scp command will fail because it assumes port 22.

You must explicitly define the custom port using the uppercase -P flag (do not confuse this with the lowercase -p, which preserves file timestamps).

scp -P 2222 website_update.zip [email protected]:/var/www/html/

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.