SCP Command Reference

Secure Copy Protocol commands for transferring files over SSH

Basic File Transfer

Copy File to Remote Server

# Basic syntax
scp /path/to/local/file.txt [email protected]:/path/to/remote/

# Copy to home directory
scp document.pdf [email protected]:~/

# Copy with specific port
scp -P 2222 file.txt [email protected]:~/

Transfer a local file to a remote server via SSH

Copy File from Remote Server

# Download from remote server
scp [email protected]:/path/to/remote/file.txt /path/to/local/

# Download to current directory
scp [email protected]:~/backup.tar.gz .

# Download with specific port
scp -P 2222 [email protected]:~/file.txt ./

Download a file from remote server to local machine

Copy Between Remote Servers

# Copy between two remote servers
scp [email protected]:/path/to/file.txt [email protected]:/path/to/destination/

# Using intermediate host
scp -3 [email protected]:/file.txt [email protected]:/dest/

Transfer files between two remote servers through local machine

Directory Transfer

Copy Directory Recursively

# Upload entire directory
scp -r /path/to/local/directory [email protected]:~/remote/path/

# Download entire directory
scp -r [email protected]:~/remote/directory /path/to/local/

# Copy with compression
scp -r -C /large/directory [email protected]:~/

Transfer entire directories with all contents recursively

Copy Multiple Files

# Multiple specific files
scp file1.txt file2.txt file3.txt [email protected]:~/

# Using wildcards
scp *.pdf [email protected]:~/documents/

# Multiple files from remote
scp [email protected]:"~/file1.txt ~/file2.txt" ./

Transfer multiple files in a single command

Advanced Options

Bandwidth and Speed Control

# Limit bandwidth (in Kbit/s)
scp -l 1000 largefile.zip [email protected]:~/

# Enable compression for faster transfer
scp -C largefile.tar [email protected]:~/

# Combine compression and bandwidth limit
scp -C -l 5000 file.zip [email protected]:~/

Control transfer speed and enable compression

Preserve Attributes

# Preserve modification times and permissions
scp -p file.txt [email protected]:~/

# Recursive with preserved attributes
scp -rp /local/directory [email protected]:~/

# Quiet mode (suppress progress)
scp -q file.txt [email protected]:~/

Maintain file attributes, timestamps, and permissions

Using SSH Keys

# Use specific SSH key
scp -i ~/.ssh/custom_key file.txt [email protected]:~/

# With different cipher
scp -c aes256-ctr file.txt [email protected]:~/

# Disable strict host key checking (use with caution)
scp -o StrictHostKeyChecking=no file.txt [email protected]:~/

Specify custom SSH keys and connection options

IPv4/IPv6 and Verbose Mode

# Force IPv4
scp -4 file.txt [email protected]:~/

# Force IPv6
scp -6 file.txt [email protected]:~/

# Verbose mode for debugging
scp -v file.txt [email protected]:~/

# Extra verbose
scp -vv file.txt [email protected]:~/

Control IP version and enable debugging output

Common Use Cases

Backup and Restore

# Backup database to remote server
scp -C database_backup.sql.gz [email protected]:~/backups/

# Restore from backup server
scp [email protected]:~/backups/latest.sql.gz ./

# Backup with timestamp
scp backup-$(date +%Y%m%d).tar.gz [email protected]:~/backups/

Transfer backups to and from remote servers

Web Deployment

# Deploy website files
scp -r ./dist/* [email protected]:/var/www/html/

# Upload single page
scp index.html [email protected]:/var/www/html/

# Upload with specific permissions preserved
scp -rp ./public [email protected]:/var/www/

Deploy web application files to production servers

Log Collection

# Download application logs
scp [email protected]:/var/log/app/*.log ./logs/

# Download compressed logs
scp [email protected]:"/var/log/nginx/access.log.*.gz" ./

# Download system logs
scp [email protected]:/var/log/syslog ./

Retrieve log files from remote servers for analysis

Security Best Practices

Secure Authentication

# Use SSH key authentication (recommended)
scp -i ~/.ssh/id_ed25519 file.txt [email protected]:~/

# Specify known_hosts file
scp -o UserKnownHostsFile=~/.ssh/known_hosts file.txt [email protected]:~/

# Use SSH config file settings
scp -F ~/.ssh/config file.txt server-alias:~/

Always use key-based authentication instead of passwords

Verify Transfers

# Generate checksum before transfer
sha256sum file.txt > file.txt.sha256
scp file.txt file.txt.sha256 [email protected]:~/

# Verify on remote server
ssh [email protected] "cd ~ && sha256sum -c file.txt.sha256"

# Compare file sizes
ls -lh file.txt
ssh [email protected] "ls -lh ~/file.txt"

Verify file integrity after transfers using checksums

Troubleshooting

Connection Issues

# Debug connection problems
scp -v file.txt [email protected]:~/

# Test SSH connection first
ssh -v [email protected]

# Check if port is correct
scp -P 22 file.txt [email protected]:~/

# Bypass proxy
scp -o ProxyCommand=none file.txt [email protected]:~/

Diagnose and resolve connection problems

Permission Errors

# Check local file permissions
ls -l /path/to/file.txt

# Check remote directory permissions
ssh [email protected] "ls -ld ~/target/directory"

# Ensure write permissions on remote
ssh [email protected] "chmod 755 ~/target/directory"

Resolve permission-related transfer failures

Performance Issues

# Enable compression for large files
scp -C largefile.tar.gz [email protected]:~/

# Use faster cipher (less secure but faster)
scp -c arcfour file.txt [email protected]:~/

# Increase buffer size (may help on fast networks)
scp -o "IPQoS=throughput" file.txt [email protected]:~/

Optimize transfer speed for large files or slow networks

Common Options Reference

OptionDescription
-rRecursively copy directories
-pPreserve modification times, access times, and modes
-P portSpecify port number
-CEnable compression
-i keyfileSpecify identity (private key) file
-l limitLimit bandwidth in Kbit/s
-qQuiet mode (suppress progress meter)
-vVerbose mode (debugging)
-4Force IPv4
-6Force IPv6