SCP - Secure Copy¶
Overview¶
SCP (Secure Copy Protocol) copies files between hosts over SSH. Simple, secure, and available wherever SSH is.
┌──────────────────────────────────────────────────────────────────────────┐
│ SCP Transfer │
│ │
│ Local Machine Remote Server │
│ ┌─────────────────┐ ┌─────────────────┐ │
│ │ │ SSH Tunnel │ │ │
│ │ file.txt ─┼──────────────────────▶│ file.txt │ │
│ │ │ Encrypted │ │ │
│ └─────────────────┘ └─────────────────┘ │
│ │
└──────────────────────────────────────────────────────────────────────────┘
Basic Syntax¶
Locations can be: - Local: /path/to/file - Remote: user@host:/path/to/file
Copy To Remote¶
Single File¶
Multiple Files¶
Directory¶
Copy From Remote¶
Single File¶
Multiple Files¶
Directory¶
Copy Between Remote Hosts¶
Routing
By default, traffic goes through your local machine. Use -3 to force this, or configure ProxyJump for direct transfer.
Common Options¶
Port¶
Identity File¶
Recursive (Directories)¶
Preserve Attributes¶
Compression¶
Bandwidth Limit¶
Quiet Mode¶
Verbose¶
Using SSH Config¶
SCP uses ~/.ssh/config:
# ~/.ssh/config
Host myserver
HostName server.example.com
User admin
IdentityFile ~/.ssh/server_key
Port 2222
Then simply:
Through Jump Host¶
With ProxyJump¶
Using Config¶
Progress and Statistics¶
Show Progress (Default)¶
Hide Progress¶
Handling Special Characters¶
Spaces in Paths¶
# Quote the entire remote path
scp "user@host:~/path with spaces/file.txt" ./
# Or escape
scp user@host:~/path\ with\ spaces/file.txt ./
Special Characters¶
Large File Transfers¶
With Compression¶
Resumable (Not Native)¶
SCP doesn't support resume. For large files, use rsync instead:
Monitor Progress with pv¶
Batch Operations¶
Multiple Hosts¶
Multiple Files with Wildcards¶
From File List¶
Exit Codes¶
| Code | Meaning |
|---|---|
| 0 | Success |
| 1 | General error |
| 2 | Connection error |
| 65 | Host not allowed |
| 66 | Protocol error |
Comparison with Alternatives¶
| Feature | SCP | SFTP | rsync |
|---|---|---|---|
| Simple copies | ✅ | ✅ | ✅ |
| Interactive | ❌ | ✅ | ❌ |
| Resume transfers | ❌ | ✅ | ✅ |
| Delta sync | ❌ | ❌ | ✅ |
| Preserve permissions | ✅ | ✅ | ✅ |
| Bandwidth limit | ✅ | ✅ | ✅ |
| Available everywhere | ✅ | ✅ | ⚠️ |
When to Use SCP¶
Use SCP for: - Quick, simple file copies - Small to medium files - One-off transfers - Scripts needing simple syntax
Use SFTP for: - Interactive file management - Browsing remote directories - Resume support
Use rsync for: - Large files (resume support) - Directory synchronization - Incremental backups - Bandwidth optimization
Troubleshooting¶
Permission Denied¶
# Check remote permissions
ssh user@host "ls -la ~/target/"
# Check you're using correct user
scp file.txt correctuser@host:~/
Connection Refused¶
Slow Transfers¶
Path Issues¶
# Use absolute paths
scp file.txt user@host:/home/user/
# Quote paths with spaces
scp file.txt "user@host:/path with spaces/"
Security Considerations¶
- Uses SSH encryption - Traffic is secure
- Respects SSH config - Same security as SSH
- Key-based auth - Use keys, not passwords
- Verify host keys - Don't skip host key verification