Introduction
SCP (Secure Copy Protocol) is a network protocol that allows you to securely transfer files and directories between hosts over an encrypted SSH connection. Built on top of SSH, SCP provides authentication and encryption, making it the preferred method for secure file transfers in Unix-like systems. Whether you’re a system administrator managing remote servers or a developer deploying applications, mastering SCP commands is essential for secure and efficient file operations.
This cheatsheet covers the most commonly used SCP commands, from basic file transfers to advanced techniques that will streamline your workflow.
Basic Syntax
scp [options] source destination
Essential SCP Commands
Basic File Transfer
Copy file from local to remote:
scp file.txt user@remote_host:/path/to/destination/ scp file.txt user@192.168.1.100:/home/user/
Copy file from remote to local:
scp user@remote_host:/path/to/file.txt /local/destination/ scp user@192.168.1.100:/home/user/file.txt ./
Copy file between two remote hosts:
scp user1@host1:/path/to/file.txt user2@host2:/path/to/destination/
Directory Operations
Copy entire directory recursively:
scp -r /local/directory user@remote_host:/remote/path/ scp -r user@remote_host:/remote/directory /local/path/
Copy multiple files:
scp file1.txt file2.txt user@remote_host:/destination/ scp user@remote_host:"/path/to/*.txt" /local/destination/
Common Options and Flags
Preserve file attributes (-p):
scp -p file.txt user@remote_host:/destination/
Verbose output (-v):
scp -v file.txt user@remote_host:/destination/
Quiet mode (-q):
scp -q file.txt user@remote_host:/destination/
Compress data during transfer (-C):
scp -C large_file.zip user@remote_host:/destination/
Specify SSH port (-P):
scp -P 2222 file.txt user@remote_host:/destination/
Use specific SSH key (-i):
scp -i ~/.ssh/my_key file.txt user@remote_host:/destination/
Limit bandwidth (-l):
scp -l 1000 file.txt user@remote_host:/destination/
Advanced Commands
Combining Multiple Options
Recursive copy with compression and verbose output:
scp -rCv /local/directory user@remote_host:/remote/path/
Transfer with custom SSH key and port:
scp -i ~/.ssh/custom_key -P 2222 file.txt user@remote_host:/destination/
Preserve attributes with quiet mode:
scp -pq file.txt user@remote_host:/destination/
Working with Special Characters and Spaces
Files with spaces in names:
scp "file with spaces.txt" user@remote_host:"/path/with spaces/" scp file\ with\ spaces.txt user@remote_host:/destination/
Using wildcards safely:
scp "user@remote_host:/path/to/*.log" /local/logs/
Configuration File Usage
Using SSH config file settings:
# If you have a host defined in ~/.ssh/config scp file.txt myserver:/destination/
Example SSH config entry:
Host myserver HostName 192.168.1.100 User myuser Port 2222 IdentityFile ~/.ssh/my_private_key
Practical Examples
Backup Operations
Backup local directory to remote server:
scp -r /important/data user@backup-server:/backups/$(date +%Y%m%d)/
Download logs from remote server:
scp "user@server:/var/log/application-$(date +%Y%m%d).log" ./logs/
Development Workflows
Deploy application files:
scp -r ./dist/ user@production-server:/var/www/html/
Download database backup:
scp user@db-server:/backups/database.sql.gz ./
Batch Operations
Transfer multiple specific files:
scp config.yml database.sql static.tar.gz user@server:/deployment/
Copy files matching pattern:
scp user@server:"/logs/app-*.log" /local/analysis/
Troubleshooting Common Issues
Connection Problems
Test SSH connectivity first:
ssh user@remote_host
Debug connection issues:
scp -v file.txt user@remote_host:/destination/
Permission Issues
Check file permissions:
ls -la file.txt ssh user@remote_host 'ls -la /destination/path'
Ensure destination directory exists:
ssh user@remote_host 'mkdir -p /destination/path'
Performance Optimization
For large files, use compression:
scp -C large_database.sql user@server:/backups/
For many small files, consider archiving first:
tar czf - /source/directory | ssh user@server 'cd /destination && tar xzf -'
Security Best Practices
- Use SSH keys instead of passwords when possible
- Verify host fingerprints on first connection
- Use non-standard SSH ports when available
- Regularly rotate SSH keys
- Monitor file transfer logs
- Use specific user accounts with minimal privileges
Final Thoughts
SCP remains one of the most reliable and secure methods for transferring files between systems. While newer tools offer additional features, SCP’s simplicity and ubiquity make it an essential skill for anyone working with remote systems. The commands in this cheatsheet cover the vast majority of use cases you’ll encounter in daily operations.
Remember to always verify your transfers, especially for critical data, and consider the security implications of your file transfer methods. With these commands in your toolkit, you’ll be able to handle secure file transfers efficiently and confidently.
Keep this cheatsheet handy, and don’t hesitate to combine options to suit your specific needs. The power of SCP lies not just in its individual commands, but in how you can combine them to create efficient, secure workflows for your particular environment.