Lesson 8.3: SCP - Secure Copy
Now that you can connect to remote systems, let's learn how to transfer files between them. SCP (Secure Copy Protocol) lets you copy files over SSH - it's encrypted, secure, and straightforward.
cp command, but for copying files between computers. It uses SSH in the background, so it's secure and works with your SSH keys.
What is SCP?
SCP is a file transfer protocol built on top of SSH:
- Secure: All data is encrypted during transfer
- Simple: Similar syntax to regular
cpcommand - Authenticated: Uses SSH credentials (keys or passwords)
- Fast: Efficient for one-time file transfers
- Familiar: If you know SSH, you know most of SCP
Basic SCP Syntax
The general format is similar to the cp command:
scp source destination
Remote locations use the SSH format:
username@hostname:/path/to/file
Copying Files TO a Remote Server
Upload files from your local machine to a remote server:
Single File
# Copy a single file to remote server
scp myfile.txt valente@192.168.1.100:/home/valente/
# Copy to specific remote directory
scp document.pdf valente@myserver.com:/var/www/html/downloads/
# Copy and rename
scp report.txt valente@myserver.com:/home/valente/monthly-report.txt
Multiple Files
# Copy multiple files to same destination
scp file1.txt file2.txt file3.txt valente@myserver.com:/home/valente/
# Use wildcards
scp *.jpg valente@myserver.com:/home/valente/pictures/
scp *.html valente@myserver.com:/var/www/html/
Real-World Example: Uploading Website Files
# Upload your nginx website
cd ~/my-website
scp index.html valente@myserver.com:/var/www/html/
scp about.html valente@myserver.com:/var/www/html/
scp contact.html valente@myserver.com:/var/www/html/
Copying Files FROM a Remote Server
Download files from a remote server to your local machine:
Single File
# Copy remote file to current directory
scp valente@myserver.com:/home/valente/data.csv ./
# Copy to specific local directory
scp valente@myserver.com:/var/log/nginx/access.log ~/logs/
# Copy and rename locally
scp valente@myserver.com:/home/valente/config.txt ./server-config.txt
Multiple Files
# Download multiple files
scp valente@myserver.com:"/home/valente/*.log" ./logs/
# Download specific files
scp "valente@myserver.com:/var/www/html/{index.html,style.css}" ./backup/
Copying Directories Recursively
Use the -r flag to copy entire directories:
Upload Directory
# Copy entire directory to remote server
scp -r my-website/ valente@myserver.com:/var/www/
# Result on server:
# /var/www/my-website/
# ├── index.html
# ├── about.html
# └── css/
# └── style.css
Download Directory
# Copy entire directory from remote server
scp -r valente@myserver.com:/home/valente/projects/ ./backups/
# Download website for backup
scp -r valente@myserver.com:/var/www/html/ ./website-backup/
cp, trailing slashes can matter. dir/ copies the contents, dir copies the directory itself. Test with small directories first!
Useful SCP Options
Preserve File Attributes (-p)
Keep original timestamps, permissions, and modes:
# Preserve modification times and permissions
scp -p important-file.txt valente@myserver.com:/backup/
# Useful for backups where timestamps matter
scp -pr website/ valente@myserver.com:/backup/
Specify SSH Port (-P)
If the remote SSH server uses a non-standard port:
# Connect to SSH on port 2222
scp -P 2222 file.txt valente@myserver.com:/home/valente/
# Note: SCP uses CAPITAL -P, SSH uses lowercase -p!
# This is a common source of confusion
-P (uppercase) for port, while SSH uses -p (lowercase). Easy to mix up!
Limit Bandwidth (-l)
Prevent SCP from using all available bandwidth:
# Limit to 1000 Kbit/s (about 125 KB/s)
scp -l 1000 large-file.zip valente@myserver.com:/home/valente/
# Useful when transferring large files on shared connections
Verbose Output (-v)
See detailed progress and debug information:
# See what's happening during transfer
scp -v file.txt valente@myserver.com:/home/valente/
# Multiple v's for even more detail
scp -vvv file.txt valente@myserver.com:/home/valente/
Quiet Mode (-q)
Suppress progress meter and warnings:
# Silent operation (useful in scripts)
scp -q file.txt valente@myserver.com:/home/valente/
Use Specific SSH Key (-i)
# Use a specific SSH key
scp -i ~/.ssh/work_key file.txt valente@workserver.com:/data/
# Combine with other options
scp -i ~/.ssh/work_key -P 2222 file.txt valente@server.com:/home/valente/
Combining Options
You can use multiple options together:
# Recursive, preserve attributes, custom port
scp -rp -P 2222 my-website/ valente@myserver.com:/var/www/
# Verbose recursive copy with specific key
scp -v -r -i ~/.ssh/work_key projects/ valente@backup.com:/backups/
# Or combine flags
scp -vrp website/ valente@myserver.com:/var/www/
Using SCP with SSH Config
If you've set up SSH config shortcuts (from Lesson 8.1), SCP uses them too:
# ~/.ssh/config
Host webserver
HostName myserver.com
User valente
Port 2222
IdentityFile ~/.ssh/work_key
Now use the short name:
# SCP uses your SSH config automatically
scp file.txt webserver:/home/valente/
scp -r website/ webserver:/var/www/
# Much easier than typing everything!
Real-World Examples
Deploy Your Nginx Website
# From your nginx project in Module 7
cd ~/my-website
# Upload all HTML files
scp *.html valente@myserver.com:/var/www/html/
# Upload CSS directory
scp -r css/ valente@myserver.com:/var/www/html/
# Upload images directory
scp -r images/ valente@myserver.com:/var/www/html/
Backup Website from Server
# Download complete website for local backup
scp -rp valente@myserver.com:/var/www/html/ ~/website-backups/$(date +%Y-%m-%d)/
# This creates a dated backup: ~/website-backups/2024-12-11/
Download Log Files
# Download nginx logs for analysis
scp valente@myserver.com:/var/log/nginx/access.log ~/logs/
scp valente@myserver.com:/var/log/nginx/error.log ~/logs/
# Or download all logs at once
scp "valente@myserver.com:/var/log/nginx/*.log" ~/logs/
Copy Between Two Remote Servers
# Copy directly from one server to another
# (data routes through your computer)
scp valente@server1.com:/data/file.txt valente@server2.com:/backup/
# This is useful but slower than server-to-server transfer
SCP Progress Indicator
By default, SCP shows a progress bar during transfer:
Progress indicator shows:
- Filename: File being transferred
- Percentage: How much is complete
- Amount: Data transferred so far
- Speed: Current transfer rate
- ETA: Estimated time remaining
When to Use SCP
SCP is best for:
- One-time transfers: Quick file copies when you need them
- Small to medium files: Documents, configs, scripts
- Simple tasks: When you just need to copy something
- Debugging: Quickly grab a config or log file
Troubleshooting SCP
Permission Denied
Solution: You don't have write permission in that directory. Either:
- Copy to a directory you own (like
/home/valente/) - Use sudo on the remote server to move files after copying
No Such File or Directory
Solution: Double-check file paths on both local and remote systems
Connection Issues
If SCP can't connect, check SSH first:
# Test SSH connection first
ssh valente@myserver.com
# If SSH works, SCP should work too
# If SSH fails, fix SSH first
SCP vs SFTP vs Rsync
| Tool | Best For | Advantages |
|---|---|---|
| SCP | Quick one-time copies | Simple, familiar syntax |
| SFTP | Interactive file management | Browse, resume, batch operations |
| Rsync | Syncing, large transfers | Efficient, only copies changes |
Quick Reference
# Upload single file
scp file.txt user@host:/path/
# Upload multiple files
scp file1.txt file2.txt user@host:/path/
# Upload directory recursively
scp -r directory/ user@host:/path/
# Download file
scp user@host:/path/file.txt ./
# Download directory
scp -r user@host:/path/directory/ ./
# Custom port (note: capital P!)
scp -P 2222 file.txt user@host:/path/
# Preserve attributes
scp -p file.txt user@host:/path/
# Recursive with preserved attributes
scp -rp directory/ user@host:/path/
# Use specific SSH key
scp -i ~/.ssh/key file.txt user@host:/path/
# Verbose mode
scp -v file.txt user@host:/path/
# Common combination
scp -rp -P 2222 directory/ user@host:/path/
Practice Exercise
Try these tasks to master SCP:
- Copy a single text file to a remote server
- Upload your nginx website directory using
scp -r - Download a log file from a remote server
- Copy multiple files using wildcards
- Use
scp -pto preserve file timestamps - Practice using SCP with your SSH config shortcut
- Transfer files between two remote systems
deploy-website.sh that uploads all your web files with one command!
Key Takeaways
- SCP securely copies files over SSH connections
- Syntax:
scp source destination(likecpbut over network) - Use
-rfor directories,-pto preserve attributes - SCP uses uppercase
-Pfor port (unlike SSH's lowercase-p) - Works with SSH keys and config files automatically
- Best for one-time file transfers; use rsync for syncing
- Always test with small files first before big transfers
Linux 101