scp
Overview
The scp
(secure copy) command securely transfers files between hosts over SSH. It provides encrypted file transfer with authentication and maintains file permissions and timestamps.
Syntax
scp [options] source destination
scp [options] source... destination
Common Options
Option | Description |
---|---|
-r |
Recursive copy (directories) |
-p |
Preserve timestamps and permissions |
-v |
Verbose output |
-q |
Quiet mode |
-C |
Enable compression |
-P port |
Specify SSH port |
-i keyfile |
Use specific SSH key |
-o option |
SSH options |
-l limit |
Bandwidth limit (Kbit/s) |
-S program |
SSH program to use |
File Transfer Patterns
Pattern | Description |
---|---|
file user@host:path |
Local to remote |
user@host:file path |
Remote to local |
user@host1:file user@host2:path |
Remote to remote |
*.txt user@host:dir/ |
Multiple files |
Key Use Cases
- Secure file transfer
- Remote backup
- Configuration deployment
- Log file collection
- Development file sync
Examples with Explanations
Example 1: Copy File to Remote
scp file.txt user@server:/home/user/
Copies local file to remote server
Example 2: Copy from Remote
scp user@server:/var/log/app.log ./
Downloads remote file to current directory
Example 3: Recursive Directory Copy
scp -r project/ user@server:/opt/
Copies entire directory structure
Authentication Methods
Password authentication:
scp file.txt user@server:/path/
SSH key authentication:
scp -i ~/.ssh/id_rsa file.txt user@server:/path/
SSH agent:
ssh-add ~/.ssh/id_rsa scp file.txt user@server:/path/
Advanced Options
Option | Description |
---|---|
-4 |
Force IPv4 |
-6 |
Force IPv6 |
-B |
Batch mode (no passwords/passphrases) |
-F configfile |
SSH config file |
-T |
Disable strict filename checking |
-3 |
Copy between remote hosts via local |
Common Usage Patterns
Preserve attributes:
scp -p file.txt user@server:/backup/
Compressed transfer:
scp -C largefile.tar user@server:/tmp/
Custom SSH port:
scp -P 2222 file.txt user@server:/path/
Performance Optimization
Enable compression for slow connections:
scp -C file.txt user@server:/path/
Limit bandwidth:
scp -l 1000 file.txt user@server:/path/
Use cipher optimization:
scp -o Cipher=aes128-ctr file.txt user@server:/path/
Batch Operations
Multiple files:
scp file1.txt file2.txt user@server:/backup/
Wildcard patterns:
scp *.log user@server:/logs/
From file list:
cat filelist.txt | xargs -I {} scp {} user@server:/dest/
Security Considerations
- Use SSH keys instead of passwords
- Verify host keys
- Use specific SSH configurations
- Limit user permissions on target
- Monitor transfer logs
Additional Resources
Best Practices
- Use SSH keys for automation
- Test with small files first
- Verify transfers with checksums
- Use compression for large files
- Monitor network usage
SSH Configuration
Create ~/.ssh/config
for easier usage:
Host myserver
HostName server.example.com
User myuser
Port 2222
IdentityFile ~/.ssh/mykey
Then use:
scp file.txt myserver:/path/
Error Handling
Connection refused:
scp -v file.txt user@server:/path/ # Debug mode
Permission denied:
scp -o PreferredAuthentications=publickey file.txt user@server:/path/
Host key verification:
scp -o StrictHostKeyChecking=no file.txt user@server:/path/
Scripting Examples
Automated backup:
#!/bin/bash DATE=$(date +%Y%m%d) scp -r /important/data/ backup@server:/backups/$DATE/
Log collection:
for host in server1 server2 server3; do scp $host:/var/log/app.log logs/$host-app.log done
Deployment script:
scp -r dist/ production@server:/var/www/html/
Progress Monitoring
Verbose output:
scp -v file.txt user@server:/path/
With progress (using pv):
pv file.txt | ssh user@server 'cat > /path/file.txt'
Using rsync for progress:
rsync --progress -e ssh file.txt user@server:/path/
Troubleshooting
- Connection timeouts
- Authentication failures
- Permission issues
- Network interruptions
- Disk space problems
Integration Examples
With find:
find . -name "*.conf" -exec scp {} user@server:/configs/ \;
With tar:
tar czf - directory/ | ssh user@server 'tar xzf - -C /destination/'
Backup script:
scp -r /home/user/ backup@server:/backups/$(date +%Y%m%d)/