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.

Think of it like this: SCP is like using 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:

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/
Quotes Matter: When using wildcards or special characters with remote paths, put the entire remote path in quotes to prevent your local shell from expanding them.

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/
Trailing Slash: Like 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
Common Mistake: SCP uses -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:

large-file.zip 48% 240MB 3.2MB/s 01:15 ETA

Progress indicator shows:

When to Use SCP

SCP is best for:

For Syncing: If you need to keep directories synchronized or transfer large amounts of data efficiently, rsync (next lesson) is usually better than SCP.

Troubleshooting SCP

Permission Denied

scp: /var/www/html/file.txt: Permission denied

Solution: You don't have write permission in that directory. Either:

No Such File or Directory

scp: /home/valente/nofile.txt: 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:

  1. Copy a single text file to a remote server
  2. Upload your nginx website directory using scp -r
  3. Download a log file from a remote server
  4. Copy multiple files using wildcards
  5. Use scp -p to preserve file timestamps
  6. Practice using SCP with your SSH config shortcut
  7. Transfer files between two remote systems
Pro Tip: For deployments and backups, create simple shell scripts that use SCP commands. Example: deploy-website.sh that uploads all your web files with one command!

Key Takeaways

Next Up: Rsync takes file transfer to the next level. Learn how to efficiently synchronize directories and only transfer what's changed!