SCP (Secure Copy Protocol) — Secure File Transfer Between Linux Systems¶
SCP (Secure Copy Protocol) is a command-line utility used to securely transfer files and directories between Linux systems over an SSH (Secure Shell) connection. Since SCP uses SSH for authentication and encryption, all transferred data is protected during transmission. Linux administrators, DevOps engineers, Cloud Architects, Platform Engineers, and Site Reliability Engineers (SREs) use SCP to copy configuration files, application packages, backups, logs, and scripts between servers.
Learning Path¶
Course Progress
What You'll Learn¶
After completing this lesson, you'll be able to:
- Understand SCP
- Transfer files securely
- Copy directories recursively
- Use SSH keys with SCP
- Transfer files between remote systems
- Monitor file transfers
- Apply SCP in production environments
Prerequisites¶
Complete:
- Module 1 – Linux Fundamentals
- Module 2 – Linux Command Line Essentials
- Module 3 – Text Processing
- Module 4 – File Management
- Module 5 – Users and Groups
- Module 6 – Process Management
- Module 7 – Package Management
- Module 8 Lessons 1–11
Why Learn SCP?¶
Imagine:
- Copying application packages to a production server.
- Uploading Kubernetes configuration files.
- Downloading log files for troubleshooting.
- Backing up configuration files.
- Deploying scripts to multiple Linux servers.
SCP provides a simple and secure method for transferring files.
What is SCP?¶
SCP stands for:
It securely copies files between systems using SSH.
Advantages:
- Encrypted communication
- Simple syntax
- SSH authentication
- Cross-platform support
- Secure file transfers
How SCP Works¶
All transferred data is encrypted using SSH.
Basic SCP Syntax¶
Example:
This copies file.txt to the remote server.
Copy File to a Remote Server¶
Copy File from a Remote Server¶
The file is copied to the current local directory.
Copy an Entire Directory¶
Use the recursive option:
Copy Using a Different SSH Port¶
If SSH runs on port 2222:
Note: SCP uses an uppercase
-Pfor the port option.
Use an SSH Private Key¶
Preserve File Attributes¶
Preserve:
- Permissions
- Timestamps
- Modification times
Enable Compression¶
Compress data during transfer.
Useful for slower network connections.
Copy Between Two Remote Servers¶
The local machine coordinates the transfer between the two remote systems.
Display Transfer Progress¶
SCP displays progress by default.
Example:
Common Commands¶
Upload file.
Download file.
Copy directory.
Use SSH key.
Use compression.
Real Production Examples¶
Deploy an application.
Upload Kubernetes configuration.
Download log files.
Backup configuration.
Production Perspective¶
SCP is commonly used for:
- Application deployments
- Configuration backups
- Log collection
- Cloud server administration
- Kubernetes configuration
- Infrastructure automation
- Disaster recovery
Although SCP is simple and secure, rsync is often preferred for large or repeated transfers because it transfers only changed data.
Hands-on Lab¶
Task 1¶
Create a test file.
Task 2¶
Upload the file.
Task 3¶
Download the file.
Task 4¶
Copy a directory.
Task 5¶
Transfer using an SSH key.
Task 6¶
Transfer using compression.
Task 7¶
Transfer using a custom SSH port.
Task 8¶
Verify the copied file.
Command Deep Dive¶
| Command | Purpose | Production Example |
|---|---|---|
scp file user@host:path | Upload file | Application deployment |
scp user@host:file . | Download file | Log collection |
scp -r | Copy directory | Website deployment |
scp -P | Use custom SSH port | Hardened servers |
scp -i | Use SSH private key | Secure authentication |
scp -p | Preserve timestamps and permissions | Configuration backup |
scp -C | Compress transfer | Large file transfer |
Common SCP Errors¶
| Error | Possible Cause |
|---|---|
Permission denied | Incorrect credentials or file permissions |
Connection refused | SSH service unavailable |
No such file or directory | Incorrect source or destination path |
Host key verification failed | SSH host key mismatch |
Connection timed out | Firewall or network issue |
SCP vs FTP vs SFTP¶
| Feature | SCP | SFTP | FTP |
|---|---|---|---|
| Encryption | ✅ | ✅ | ❌ |
| Uses SSH | ✅ | ✅ | ❌ |
| Interactive File Management | ❌ | ✅ | ✅ |
| Secure | ✅ | ✅ | ❌ |
| Production Ready | ✅ | ✅ | ❌ |
SCP vs rsync¶
| Feature | SCP | rsync |
|---|---|---|
| Simple File Copy | ✅ | ✅ |
| Incremental Transfer | ❌ | ✅ |
| Synchronization | ❌ | ✅ |
| Compression | ✅ | ✅ |
| Resume Interrupted Transfers | Limited | ✅ |
| Best For | One-time transfers | Repeated synchronization |
Production Troubleshooting Scenario¶
Scenario
A DevOps engineer attempts to deploy an application.
Error:
Investigation:
Verify SSH access.
Check destination permissions.
The deployment directory is owned by another user.
Correct the ownership or permissions.
Retry:
The deployment completes successfully.
Best Practices¶
- Use SSH key-based authentication.
- Verify destination paths before transferring files.
- Use compression (
-C) for large transfers over slow networks. - Preserve file attributes when copying configuration files.
- Use SCP for one-time transfers and
rsyncfor recurring synchronization tasks. - Verify transferred files after completion.
Common Mistakes¶
❌ Forgetting to use uppercase -P for the SSH port.
✅ Remember to to use uppercase -P for the SSH port.
❌ Using password authentication instead of SSH keys.
✅ Prefer SSH keys rather than using password authentication.
❌ Copying files to incorrect directories.
✅ Avoid this mistake: copying files to incorrect directories.
❌ Assuming SCP can synchronize changed files like rsync.
✅ Verify SCP can synchronize changed files like rsync instead of assuming it.
Interview Questions¶
Beginner¶
- What is SCP?
- Which protocol does SCP use?
- How do you copy a file to a remote server?
- Which option copies directories recursively?
Intermediate¶
- What is the difference between SCP and SFTP?
- Why is uppercase
-Pused for specifying the SSH port? - How do you transfer files using an SSH key?
- What does the
-Coption do?
Architect Level¶
- How would you securely deploy application packages across production servers?
- When would you choose SCP instead of
rsync? - How would you automate secure file transfers in a CI/CD pipeline?
Summary¶
In this lesson, you learned:
- SCP fundamentals
- Secure file transfers
- Directory transfers
- SSH key authentication
- Compression
- File preservation
- Production deployment techniques
SCP provides a secure and straightforward method for transferring files between Linux systems using SSH encryption. It is ideal for one-time file transfers, application deployments, backups, and administrative tasks across production environments.
Key Takeaways¶
- SCP securely transfers files using SSH.
- Use
scp -rto copy directories. - Use
-Pfor custom SSH ports. - Use
-ifor SSH key authentication. - Use
-Cto compress large transfers. - Use SCP for simple transfers and
rsyncfor efficient synchronization.
What's Next?¶
rsync — Efficient File Synchronization and Backups in Linux
You'll explore:
- File synchronization
- Incremental transfers
- Archive mode
- Compression
- Backup automation
- Remote synchronization
- Production deployment strategies
By the end of the lesson, you'll understand why rsync is one of the most powerful tools for backups, deployments, and synchronizing data across Linux systems.