Skip to main content
Blog|
How-to guides

How to transfer files over SSH using SCP

|
Mar 18, 2026|10 min read
HOW-TO GUIDESHow to transfer files over SSHusing SCPHOSTNEYhostney.comMarch 18, 2026

SCP (Secure Copy Protocol) copies files between your local machine and a remote server over SSH. It uses the same encrypted connection and the same credentials as your regular SSH session. If you can SSH into a server, you can use SCP to transfer files to and from it without any additional setup.

SCP is a command line tool. You run it from your local terminal, not from inside an SSH session. It connects, transfers the file, and disconnects. There is no interactive session like SFTP – you specify the source and destination in a single command and SCP handles the rest.

This guide covers SCP syntax, practical examples for every common scenario, and how SCP compares to SFTP and rsync.

How SCP works#

SCP runs on top of SSH. When you execute an SCP command, it opens an SSH connection to the remote server, authenticates using your SSH credentials (password or key), transfers the file through the encrypted channel, and closes the connection. The file data never travels in plaintext.

Because SCP uses SSH, it inherits all of SSH’s security properties: encrypted transport, host key verification, and support for both password and key-based authentication. If you have SSH key authentication configured, SCP uses it automatically – no password prompt.

SCP operates in one direction per command. You either push a file from local to remote, or pull a file from remote to local. Each command is a single transfer operation.

Basic SCP syntax#

scp [options] source destination

Either the source or the destination (or both) can be a remote path. Remote paths use the format user@host:path . Local paths are just regular file paths.

Copying files to a remote server#

Upload a single file

scp file.txt john@example.com:/home/john/

This copies file.txt from your current local directory to /home/john/ on the remote server.

Upload to a specific remote directory

scp file.txt john@example.com:/var/www/html/

Upload and rename the file

scp file.txt john@example.com:/home/john/newname.txt

Upload multiple files

scp file1.txt file2.txt file3.txt john@example.com:/home/john/

You can list as many files as you need. They all go to the same remote destination.

Upload using a wildcard

scp *.txt john@example.com:/home/john/documents/

Your local shell expands the wildcard before SCP runs, so all matching files are transferred.

Copying files from a remote server#

Download a single file

scp john@example.com:/home/john/file.txt .

The . at the end means “current local directory.” The file is downloaded to wherever you ran the command.

Download to a specific local path

scp john@example.com:/home/john/file.txt /home/localuser/downloads/

Download and rename

scp john@example.com:/home/john/file.txt localcopy.txt

Download multiple files

scp john@example.com:"/home/john/file1.txt /home/john/file2.txt" .

Note the quotes around the remote paths. Without them, only the first path is treated as remote.

Recursive directory copy#

To copy an entire directory and everything inside it, use the -r flag:

Upload a directory to a remote server

scp -r /home/localuser/project john@example.com:/var/www/html/

This copies the project directory and all its contents (subdirectories, files, everything) to the remote server. The directory itself is created at the destination, so the result is /var/www/html/project/ .

Download a directory from a remote server

scp -r john@example.com:/var/www/html/project /home/localuser/backup/

Important note about recursive copy

SCP follows symbolic links during recursive copies. If your directory contains symlinks that point outside the directory, SCP copies the linked files as regular files. This can result in unexpected data being transferred. If your directory structure relies on symlinks, rsync gives you more control over how they are handled.

Specifying a port#

If the remote server runs SSH on a non-standard port, use the -P flag (capital P):

scp -P 2222 file.txt john@example.com:/home/john/
scp -P 2222 john@example.com:/home/john/file.txt .

This is a common source of confusion. SCP uses `-P` (uppercase) for the port. SSH uses `-p` (lowercase). SFTP also uses `-P` (uppercase). There is no good reason for the inconsistency – it is just how the tools were implemented. See What port does SSH use for more on SSH ports, non-standard port configuration, and SSH config file setup.

Using SSH keys with SCP#

If you have SSH key authentication set up, SCP uses it automatically. Your key is picked up from the default location ( ~/.ssh/id_rsa , ~/.ssh/id_ed25519 , etc.) without any extra flags.

To specify a particular key file:

scp -i ~/.ssh/id_ed25519 file.txt john@example.com:/home/john/

SSH key authentication is more secure than passwords and eliminates the password prompt entirely. If you have not set up SSH keys yet, see the SSH keys guide for instructions.

Preserving file attributes#

The -p flag (lowercase) preserves the file’s modification time, access time, and permissions:

scp -p file.txt john@example.com:/home/john/

Without this flag, the copied file gets the current timestamp and default permissions on the destination. Use -p when you need the remote copy to match the original exactly.

Limiting bandwidth#

To avoid saturating your connection during large transfers:

scp -l 5000 largefile.zip john@example.com:/home/john/

The -l flag sets a bandwidth limit in Kbit/s. 5000 Kbit/s is approximately 625 KB/s. Useful when you are on a shared connection and do not want your file transfer to consume all available bandwidth.

Enabling compression#

For text files and uncompressed data, compression can speed up the transfer:

scp -C file.txt john@example.com:/home/john/

The -C flag enables SSH compression. This reduces the amount of data sent over the wire, which helps on slow connections. It has little effect on already-compressed files like ZIP archives, tarballs with gzip, or JPEG images – compressing compressed data does not save anything.

Verbose output for debugging#

If a transfer fails or hangs, verbose mode shows what is happening:

scp -v file.txt john@example.com:/home/john/

This outputs detailed information about the SSH connection, authentication, and transfer progress. It is the first thing to try when SCP is not working as expected.

Combining flags#

Flags can be combined. For example, to recursively copy a directory on a non-standard port with compression and preserved timestamps:

scp -rCpP 2222 /home/localuser/project john@example.com:/var/www/html/

Or written more readably:

scp -r -C -p -P 2222 /home/localuser/project john@example.com:/var/www/html/

Both forms are equivalent. Use whichever you find clearer.

Copying between two remote servers#

SCP can copy files directly between two remote servers:

scp john@server1.com:/home/john/file.txt jane@server2.com:/home/jane/

By default, the data routes through your local machine. Your machine opens SSH connections to both servers and shuttles the data between them. This works but is slower than a direct server-to-server transfer because every byte travels across two network hops instead of one.

If you want the transfer to happen directly between the two servers (bypassing your local machine), use the -3 flag:

scp -3 john@server1.com:/home/john/file.txt jane@server2.com:/home/jane/

Wait, that is backwards. The -3 flag actually forces the data through the local machine (which is the default in newer OpenSSH versions anyway). For a direct server-to-server transfer, the better approach is to SSH into the source server and run SCP from there:

ssh john@server1.com "scp /home/john/file.txt jane@server2.com:/home/jane/"

This way the file goes directly from server1 to server2 without touching your local machine. This is the approach recommended in the WordPress migration guide for moving site files between servers.

SCP on Windows#

Windows 10 and 11 include OpenSSH by default, so SCP works in PowerShell and Command Prompt without installing anything:

scp file.txt john@example.com:/home/john/

All commands in this guide work identically on Windows. The only difference is that local paths use Windows path format:

scp C:\Users\john\file.txt john@example.com:/home/john/

If OpenSSH is not installed, open PowerShell as administrator and run:

Add-WindowsCapability -Online -Name OpenSSH.Client~~~~0.0.1.0

SCP vs SFTP vs rsync#

All three tools transfer files over SSH. They share the same encryption, the same authentication, and the same port. The difference is in how they handle the transfer.

SCP

SCP is the simplest of the three. One command, one transfer. It copies files from point A to point B. There is no interactive session, no file browsing, and no incremental sync. You tell it what to copy and where, and it does it.

SCP is best for quick, one-off transfers – uploading a configuration file, downloading a log, pushing a script to a server. It is the fastest way to move a file when you already know the source and destination paths.

SFTP

SFTP provides an interactive session where you can browse directories, upload and download files, manage permissions, rename files, and create directories. It is more like a remote file manager than a copy command.

SFTP is better when you need to explore the remote filesystem, transfer multiple files in different directions during one session, or manage files beyond just copying them. See the SFTP commands reference for the full list of what you can do in an SFTP session.

rsync

rsync is designed for synchronization. It compares the source and destination and only transfers the differences. If you have a 500 MB directory and change one file, rsync transfers only that file (and only the changed blocks within it, if using the delta algorithm). SCP would re-transfer the entire directory.

rsync also handles interrupted transfers gracefully – you can resume where you left off. It supports excluding files by pattern, deleting files at the destination that no longer exist at the source, and preserving complex attributes like symlinks, hardlinks, and extended attributes.

rsync is the right tool for recurring transfers, backups, deployments, and keeping two directories in sync. For one-off file copies, SCP is simpler.

Comparison

SCPSFTPrsync
Transfer typeFull copyFull copyIncremental (only differences)
InteractiveNoYesNo
Resume supportNoPartial (append)Yes
File managementCopy onlyFull (rename, delete, chmod, mkdir)Sync with delete option
Typical useQuick one-off transfersInteractive file managementRecurring sync, backups, deployments
ComplexityLowestMediumHighest

Which should you use

For a quick file copy where you know the exact paths, SCP. For interactive file management, SFTP. For syncing directories, backups, or deployments where you want incremental transfers, rsync.

In practice, most people use all three depending on the situation. They are not competing tools – they complement each other.

Common errors and fixes#

Permission denied

scp: /var/www/html/file.txt: Permission denied

Your user does not have write permission to the destination directory. Check the directory permissions on the remote server with ls -la and ensure your user owns the directory or has write access.

No such file or directory

scp: /home/john/nonexistent.txt: No such file or directory

The source path does not exist. Double-check the path. On remote servers, use an SSH session to verify the file exists and confirm the full path with pwd and ls .

Connection refused

ssh: connect to host example.com port 22: Connection refused

SSH is not running on the server, the port is wrong, or a firewall is blocking the connection. Verify the correct port with your hosting provider and check that your IP is not blocked. See SSH connection refused: what it means and how to fix it for a full diagnosis walkthrough.

Host key verification failed

Host key verification failed.

The server’s host key does not match what is stored in your ~/.ssh/known_hosts file. This can happen legitimately when a server is rebuilt or migrated, but it can also indicate a man-in-the-middle attack. Verify the change with your hosting provider before accepting the new key.

To update the stored key after confirming the change is legitimate:

ssh-keygen -R example.com

Then connect again and accept the new key.

Connection timed out

ssh: connect to host example.com port 22: Connection timed out

The server is unreachable. Check your network connection, verify the hostname resolves correctly, and confirm the server is online. If the server uses a non-standard SSH port, make sure you are specifying it with -P .

SCP access on Hostney#

On Hostney, SCP works out of the box with your SSH credentials. Every account gets SSH access, and SCP uses the same connection. No additional configuration is needed.

Connect using:

  • Host: your server hostname from the control panel
  • Port: 22
  • Username: your account username
  • Authentication: SSH key (configured through the SSH Keys section in the control panel)

Example upload to your web root:

scp file.txt john@your-server.hostney.com:/var/www/html/

SSH key authentication is required. Once your public key is added through the control panel, SCP picks it up automatically.

SCP command reference#

CommandWhat it does
scp file user@host:/path/ Upload file to remote server
scp user@host:/path/file . Download file from remote server
scp -r dir user@host:/path/ Upload directory recursively
scp -r user@host:/path/dir . Download directory recursively
scp -P 2222 file user@host:/path/ Use non-standard SSH port
scp -i ~/.ssh/key file user@host:/path/ Use specific SSH key
scp -p file user@host:/path/ Preserve timestamps and permissions
scp -C file user@host:/path/ Enable compression
scp -l 5000 file user@host:/path/ Limit bandwidth (Kbit/s)
scp -v file user@host:/path/ Verbose output for debugging
scp -rCp dir user@host:/path/ Recursive, compressed, preserved attributes