Copy files between servers via SCP
Table of Contents
Contents
- Overview
- Copy a local file to a remote server
- Copy a remote file to the local machine
- Copy between two remote servers
- Copy a directory recursively
- Use a specific SSH private key
- Use a non-standard SSH port
- Preserve file metadata (timestamps, permissions)
- Limit bandwidth usage
- Copy with verbose output (useful for debugging)
- Common options summary
- Notes
Overview
scp (Secure Copy Protocol) copies files and directories between hosts over SSH.
It uses SSH for authentication and encryption, so no extra setup is needed beyond
an existing SSH configuration.
General syntax:
scp [options] <source> <destination>
Remote paths use the format user@host:/path.
Copy a local file to a remote server
scp /local/path/file.txt user@host:/remote/path/
Copy a remote file to the local machine
scp user@host:/remote/path/file.txt /local/path/
Copy between two remote servers
scp user1@host1:/path/file.txt user2@host2:/path/
Copy a directory recursively
Use -r to copy a full directory tree.
scp -r /local/dir user@host:/remote/path/
Use a specific SSH private key
scp -i ~/.ssh/id_rsa /local/file.txt user@host:/remote/path/
Use a non-standard SSH port
scp -P 2222 /local/file.txt user@host:/remote/path/
NOTE: The port flag for
scpis uppercase-P, unlikesshwhich uses lowercase-p.
Preserve file metadata (timestamps, permissions)
scp -p /local/file.txt user@host:/remote/path/
Limit bandwidth usage
Value is in Kbits/s.
scp -l 1024 /local/file.txt user@host:/remote/path/
Copy with verbose output (useful for debugging)
scp -v /local/file.txt user@host:/remote/path/
Common options summary
| Option | Description |
|---|---|
-r |
Recursive copy (directories) |
-P <port> |
Non-standard SSH port |
-i <key> |
Private key file |
-p |
Preserve file timestamps and permissions |
-l <kbps> |
Limit transfer bandwidth |
-v |
Verbose mode |
-C |
Enable compression |
-q |
Quiet mode (suppress progress) |
Notes
-
scprelies on the remotescpbinary being present on the target host. - For large or frequent transfers, consider
rsyncover SSH: it supports delta transfers and is more efficient for repeated syncs. - On RHEL 9 and newer systems,
scpmay use the SFTP subsystem by default. If transfers fail, check thatSubsystem sftpis enabled insshd_config.