Console9

How to copy files over SSH with SCP

Transfer files between local and remote machines securely using the SCP command over an SSH connection.

Transfer files and directories between a local machine and a remote server using SCP (Secure Copy Protocol) over an encrypted SSH connection.

Prerequisites

  • SSH access to the remote server (password or key-based authentication)
  • SCP installed (bundled with OpenSSH on Linux, macOS, and Windows 10+)

Step-by-Step: Copy Files Over SSH with SCP

1. Copy a Local File to a Remote Server with SCP

SCP uploads a file from the local machine to a specified path on the remote server:

scp localfile.txt user@203.0.113.50:/home/user/

SCP uses the SSH protocol for authentication and encryption. The file transfers over the same port 22 connection that SSH uses.

2. Copy a Remote File to the Local Machine with SCP

SCP downloads a file from the remote server to the current local directory:

scp user@203.0.113.50:/var/log/app.log ./

3. Copy an Entire Directory Recursively with SCP

SCP copies directories and their contents with the -r (recursive) flag:

scp -r /local/project/ user@203.0.113.50:/home/user/project/

4. Copy Files Using a Non-Standard SSH Port with SCP

SCP uses the -P (uppercase) flag to specify the SSH port:

scp -P 2222 localfile.txt user@203.0.113.50:/home/user/

Note: SCP uses -P for port, while SSH uses -p. This inconsistency is a common source of confusion.

How to Verify the File Transfer with SCP

List the transferred file on the remote server:

ssh user@203.0.113.50 "ls -la /home/user/localfile.txt"

Common Issues When Copying Files with SCP

SCP transfers the entire file every time— SCP does not support incremental or delta transfers. For large files that change frequently, use Rsyncover SSH instead: rsync -avz -e ssh localfile.txt user@host:/path/. Rsync transfers only the changed bytes.

"Permission denied" during SCP transfer— The remote user lacks write permission to the target directory. Verify permissions with ssh user@host "ls -la /target/directory/".