scp Command

Securely copy files and directories between local and remote systems over SSH protocol.

Syntax

scp [OPTIONS] source destination scp [OPTIONS] source1 source2 ... destination

The scp (secure copy) command uses SSH protocol to securely transfer files between hosts on a network.

Common Options

Option Description
-r Recursively copy entire directories
-p Preserve modification times, access times, and modes
-v Verbose mode - show detailed progress
-q Quiet mode - suppress progress meter and warnings
-C Enable compression during transfer
-P port Specify SSH port (capital P)
-i keyfile Use specific SSH private key file
-o option Pass SSH options
-l limit Limit bandwidth in Kbit/s
-B Use batch mode (no password prompts)

Basic Examples

Local to remote transfer

# Copy single file to remote server scp file.txt user@remotehost:/path/to/destination/ # Copy file to user's home directory scp document.pdf [email protected]:~/ # Copy file with new name scp local.txt [email protected]:~/remote.txt # Copy multiple files scp file1.txt file2.txt file3.txt [email protected]:~/documents/

Transfer files from local system to remote server

Remote to local transfer

# Copy file from remote to local scp user@remotehost:/path/to/file.txt ./ # Copy to specific local directory scp [email protected]:~/document.pdf /home/user/downloads/ # Copy with new local name scp [email protected]:~/remote.txt ./local.txt # Copy from remote home directory scp [email protected]:~/backup.tar.gz ./

Transfer files from remote server to local system

Directory transfer

# Copy directory recursively to remote scp -r /local/directory [email protected]:/remote/path/ # Copy directory from remote to local scp -r [email protected]:/remote/directory ./ # Copy directory contents only scp -r /local/directory/* [email protected]:/remote/path/ # Copy directory with preserved permissions scp -rp /local/directory [email protected]:/backup/

Transfer entire directories between systems

Advanced Usage

Using different SSH ports and keys

# Use custom SSH port scp -P 2222 file.txt [email protected]:~/ # Use specific SSH key scp -i ~/.ssh/mykey.pem file.txt [email protected]:~/ # Combine port and key scp -P 2222 -i ~/.ssh/mykey.pem file.txt [email protected]:~/ # Use SSH config file settings scp -F ~/.ssh/config file.txt myserver:~/

Use custom SSH configurations for secure transfer

Performance optimization

# Enable compression for large files scp -C largefile.zip [email protected]:~/ # Limit bandwidth to 1000 Kbit/s scp -l 1000 bigfile.tar.gz [email protected]:~/ # Use cipher for faster transfer scp -o Cipher=aes128-ctr file.txt [email protected]:~/ # Disable strict host key checking scp -o StrictHostKeyChecking=no file.txt [email protected]:~/

Optimize transfer performance and handle network constraints

Batch operations and automation

# Batch mode (no interactive prompts) scp -B -i ~/.ssh/key file.txt [email protected]:~/ # Quiet mode for scripts scp -q file.txt [email protected]:~/ # Verbose mode for debugging scp -v file.txt [email protected]:~/ # Preserve file attributes scp -p file.txt [email protected]:~/

Configure scp for automated scripts and batch operations

Remote to Remote Transfer

Third-party transfer

# Copy between two remote servers scp user1@server1:/path/to/file user2@server2:/path/to/destination/ # Copy directory between remote servers scp -r user1@server1:/data/ user2@server2:/backup/ # Use different users on each server scp admin@server1:/logs/app.log backup@server2:/archives/ # Copy with compression between remotes scp -C user1@server1:/large/file.tar.gz user2@server2:/storage/

Transfer files directly between remote servers

Using jump hosts

# Copy through jump host scp -o ProxyJump=jumphost file.txt user@targethost:~/ # Multiple jump hosts scp -o ProxyJump=jump1,jump2 file.txt user@target:~/ # Copy from remote through jump host scp -o ProxyJump=jumphost user@target:~/file.txt ./ # Use SSH config for jump hosts scp -F ~/.ssh/config file.txt target-via-jump:~/

Transfer files through intermediate jump hosts

Practical Examples

Backup operations

# Backup home directory to remote server scp -r ~/important_files user@backup-server:/backups/$(date +%Y%m%d)/ # Backup with compression and preserved attributes scp -rCp /var/www/html admin@backup:/web-backups/ # Incremental backup script #!/bin/bash DATE=$(date +%Y%m%d_%H%M%S) scp -r /data/ backup@server:/backups/data_$DATE/ # Database backup transfer mysqldump -u user -p database | gzip | ssh user@backup 'cat > /backups/db_$(date +%Y%m%d).sql.gz'

Use scp for backup and archival operations

Web deployment

# Deploy website files scp -r ./website/* user@webserver:/var/www/html/ # Deploy with specific permissions scp -rp ./dist/* deploy@server:/var/www/myapp/ # Deploy single application file scp app.jar deploy@server:/opt/myapp/ # Deploy configuration files scp config/*.conf admin@server:/etc/myapp/

Deploy applications and websites using scp

Log collection

# Collect logs from multiple servers for server in web1 web2 web3; do scp user@$server:/var/log/app.log ./logs/$server-app.log done # Collect compressed logs scp user@server:/var/log/archive/*.gz ./log-archive/ # Collect logs with date range scp user@server:"/var/log/app-$(date +%Y%m%d)*.log" ./today-logs/ # Automated log collection script #!/bin/bash SERVERS="web1 web2 db1" DATE=$(date +%Y%m%d) for server in $SERVERS; do mkdir -p ./logs/$server scp -q user@$server:/var/log/*.log ./logs/$server/ done

Collect and centralize log files from remote systems

Security Considerations

SSH key authentication

# Generate SSH key pair ssh-keygen -t rsa -b 4096 -f ~/.ssh/scp_key # Copy public key to remote server ssh-copy-id -i ~/.ssh/scp_key.pub [email protected] # Use specific key for scp scp -i ~/.ssh/scp_key file.txt [email protected]:~/ # Set proper key permissions chmod 600 ~/.ssh/scp_key chmod 644 ~/.ssh/scp_key.pub

Set up secure key-based authentication

SSH configuration

# ~/.ssh/config example Host myserver HostName server.example.com User myuser Port 2222 IdentityFile ~/.ssh/myserver_key StrictHostKeyChecking yes # Use config with scp scp file.txt myserver:~/ # Disable password authentication scp -o PasswordAuthentication=no file.txt user@server:~/ # Use specific SSH options scp -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no file.txt user@server:~/

Configure SSH settings for secure and convenient transfers

Troubleshooting

Common Issues and Solutions
  • Permission denied - Check SSH key permissions and server access
  • Host key verification failed - Update known_hosts or use StrictHostKeyChecking=no
  • Connection refused - Verify SSH service is running and port is correct
  • No space left on device - Check disk space on destination
  • Slow transfer - Use compression (-C) or check network bandwidth

Debug and verbose output

# Verbose output for debugging scp -v file.txt user@server:~/ # Very verbose (debug level 1) scp -o LogLevel=DEBUG1 file.txt user@server:~/ # Maximum debug output scp -o LogLevel=DEBUG3 file.txt user@server:~/ # Test connection without transfer ssh -T [email protected]

Debug connection and transfer issues

Best Practices

Security Best Practices
  • Use SSH key authentication instead of passwords
  • Regularly rotate SSH keys and remove unused ones
  • Use non-standard SSH ports when possible
  • Enable SSH connection multiplexing for multiple transfers
  • Verify host keys and maintain known_hosts file
  • Use jump hosts for accessing internal networks
Performance Tips
  • Use compression (-C) for large files over slow connections
  • Limit bandwidth (-l) to avoid overwhelming network
  • Use faster ciphers for better performance
  • Consider rsync for incremental transfers
  • Use parallel transfers for multiple files

See also