scp — Secure Copy — is one of the simplest ways to transfer files and directories between Linux machines over an SSH connection.
If you work with Linux servers, AWS, Azure, Docker, Kubernetes, Hadoop, DevOps, SRE, or system administration, scp is a command you will use frequently.
For example:
scp backup.tar.gz user@192.168.1.20:/home/user/
This means:
Take
backup.tar.gzfrom my current machine and securely copy it to/home/user/on192.168.1.20.
Modern OpenSSH scp uses the SFTP protocol over an SSH connection by default, while retaining the familiar scp command syntax. It uses the same SSH authentication and security model as an SSH login.
1. What is SCP?
SCP stands for:
Secure Copy Protocol / Secure Copy
The scp command allows you to copy:
- Local → Remote
- Remote → Local
- Directory → Remote
- Remote → Directory
- Remote → Remote
The important concept is that SSH is used for authentication and encrypted communication.
Think of it this way:
Your Linux Machine | | SSH encrypted connection | v Remote Linux Server
Instead of:
FTP | +---- username/password +---- potentially insecure depending on configuration
you can use:
SCP | +---- SSH authentication +---- encrypted connection +---- secure file transfer
2. Why Do We Use SCP?
Imagine you have this situation:
Your Laptop 10.10.10.5
and your server is:
Production Server 10.10.10.20
You created:
application.tar.gz
on your laptop and need to put it on the server.
Without SCP, you might need:
- FTP
- SFTP client
- cloud storage
- USB
- web upload
- shared network storage
With SCP:
scp application.tar.gz user@10.10.10.20:/opt/apps/
Done.
3. Basic SCP Syntax
The most important syntax to remember is:
scp SOURCE DESTINATION
For a remote system:
[user@]host:path
Therefore:
scp file.txt user@server:/home/user/
Break it down:
scp │ ├── file.txt │ └── source │ └── user@server:/home/user/ │ │ │ └── destination path └── remote user and server
4. The Four Most Important SCP Patterns
Memorize these four.
Pattern 1 — Local → Remote
scp file.txt user@server:/home/user/
Pattern 2 — Remote → Local
scp user@server:/home/user/file.txt .
The . means:
Current directory
Pattern 3 — Local Directory → Remote
scp -r myfolder user@server:/home/user/
-r means recursive.
Pattern 4 — Remote → Local Directory
scp -r user@server:/home/user/myfolder .
These four commands cover a huge percentage of everyday SCP usage.
5. First: Test SSH
Before using SCP, make sure SSH works.
Try:
ssh user@192.168.1.20
If you can log in successfully:
user@192.168.1.20's password:
then SCP will generally work using the same connection/authentication configuration.
Test:
ssh user@192.168.1.20
Then:
scp test.txt user@192.168.1.20:/home/user/
6. Installing SCP
On Ubuntu/Debian:
sudo apt update sudo apt install openssh-client
Check:
scp -V
or:
scp -h
On RHEL/Rocky/AlmaLinux/Fedora:
sudo dnf install openssh-clients
On many Linux distributions, scp is already installed with the OpenSSH client package.
7. Scenario 1: Upload a File to a Server
Suppose you have:
~/Downloads/application.tar.gz
and want to upload it to:
/opt/app/
on:
192.168.1.100
Run:
scp ~/Downloads/application.tar.gz admin@192.168.1.100:/opt/app/
You'll see something similar to:
application.tar.gz 100% 250MB 25.4MB/s 00:09
8. Scenario 2: Download a File From a Server
Suppose the server contains:
/var/log/application.log
Download it to your current directory:
scp admin@192.168.1.100:/var/log/application.log .
Result:
application.log
will appear in your current directory.
9. Scenario 3: Download a Server Backup
Suppose your server has:
/backup/database.sql.gz
Run:
scp admin@server:/backup/database.sql.gz ~/Downloads/
This is a very common administrator workflow:
Production Server | | SCP v Administrator Laptop | v ~/Downloads/
10. Scenario 4: Copy an Entire Directory
Suppose you have:
website/ ├── index.html ├── css/ ├── js/ └── images/
Use:
scp -r website user@server:/var/www/
The -r option tells SCP to recursively copy directories.
The remote server will contain:
/var/www/website/ ├── index.html ├── css/ ├── js/ └── images/
11. Scenario 5: Download an Entire Directory
Remote:
/opt/application/ ├── config/ ├── logs/ ├── scripts/ └── data/
Download:
scp -r user@server:/opt/application ~/backup/
You get:
~/backup/application/
12. Scenario 6: Copy Multiple Files
You can specify multiple source files:
scp file1.txt file2.txt file3.txt user@server:/home/user/
For example:
scp app.conf nginx.conf docker-compose.yml admin@server:/tmp/
13. Scenario 7: Use Wildcards
Suppose you have:
app.log app.log.1 app.log.2 app.log.3
You might use:
scp user@server:/var/log/app.log* .
However, wildcard handling deserves care because it can differ depending on the SCP/SFTP mode and remote shell behavior. Modern SCP uses SFTP by default, while -O explicitly forces the legacy SCP protocol.
For predictable scripting, be explicit where possible.
14. Scenario 8: Copy Using an IP Address
Instead of hostname:
scp file.txt user@server:/tmp/
you can use:
scp file.txt user@192.168.1.50:/tmp/
Very common in lab environments.
15. Scenario 9: Copy Using a Hostname
If DNS or /etc/hosts is configured:
scp backup.tar.gz root@server01:/backup/
instead of:
scp backup.tar.gz root@192.168.1.50:/backup/
This becomes particularly useful when managing many servers.
16. Scenario 10: Use a Non-Standard SSH Port
SSH normally uses:
22
Suppose SSH is running on:
2222
Use:
scp -P 2222 file.txt user@server:/tmp/
Important
It is:
-P
uppercase P.
Not:
-p
-P specifies the SSH port, while -p preserves file modification/access times and mode bits.
17. -P vs -p
This is one of the classic SCP mistakes.
Specify SSH port
scp -P 2222 file.txt user@server:/tmp/
Preserve file attributes
scp -p file.txt user@server:/tmp/
You can combine them:
scp -P 2222 -p file.txt user@server:/tmp/
18. Scenario 11: Use an SSH Private Key
Suppose your private key is:
~/.ssh/myserver.pem
Use:
scp -i ~/.ssh/myserver.pem file.txt ubuntu@192.168.1.100:/home/ubuntu/
This is extremely common with cloud servers.
For example:
scp -i aws-prod.pem application.tar.gz ubuntu@54.123.45.67:/tmp/
The -i option selects the identity/private key used for public-key authentication.
19. AWS EC2 Example
Imagine:
Local laptop | | SSH key | v AWS EC2 54.10.20.30
Upload:
scp -i myserver.pem app.tar.gz ubuntu@54.10.20.30:/home/ubuntu/
Download:
scp -i myserver.pem ubuntu@54.10.20.30:/var/log/app.log .
Copy directory:
scp -i myserver.pem -r ./application ubuntu@54.10.20.30:/home/ubuntu/
20. Scenario 12: Upload to a Root-Owned Directory
Suppose you want:
/etc/myapp/
but you're connecting as:
ubuntu
This may fail:
scp config.yml ubuntu@server:/etc/myapp/
because ubuntu doesn't have permission to write there.
A common approach is:
scp config.yml ubuntu@server:/tmp/
Then:
ssh ubuntu@server
and:
sudo mv /tmp/config.yml /etc/myapp/
Why?
Because SCP doesn't magically become root just because the destination directory is root-owned.
21. Scenario 13: Deploy a Configuration File
Suppose you're managing:
nginx.conf
You can upload it:
scp nginx.conf admin@web01:/tmp/
Then:
ssh admin@web01
and:
sudo cp /tmp/nginx.conf /etc/nginx/nginx.conf
Validate:
sudo nginx -t
Then:
sudo systemctl reload nginx
This creates a simple deployment workflow:
Developer | | scp v /tmp/nginx.conf | | sudo v /etc/nginx/nginx.conf | v nginx reload
22. Scenario 14: Copy Application Logs From Production
Suppose you are troubleshooting an application.
Server:
prod01
Log:
/var/log/myapp/application.log
Download:
scp admin@prod01:/var/log/myapp/application.log .
If the file requires elevated privileges, first copy it somewhere accessible:
ssh admin@prod01
sudo cp /var/log/myapp/application.log /tmp/ sudo chown admin:admin /tmp/application.log
Then:
scp admin@prod01:/tmp/application.log .
This is often safer than routinely using direct root SSH access.
23. Scenario 15: Copy Docker Configuration
Suppose you have:
docker-compose.yml .env nginx.conf
Upload:
scp docker-compose.yml .env nginx.conf deploy@server:/opt/myapp/
Then:
ssh deploy@server
and:
cd /opt/myapp docker compose up -d
This is a simple manual deployment pipeline.
24. Scenario 16: Transfer a Large Backup
Suppose:
database-backup.sql.gz
is 50 GB.
You can use:
scp database-backup.sql.gz backup@server:/backup/
But for very large files or unreliable networks, SCP may not be the best tool.
Consider:
rsync
because it can efficiently resume/synchronize data.
For example:
rsync -avP database-backup.sql.gz backup@server:/backup/
A useful rule:
One-time/simple copy ↓ SCP Repeated synchronization / huge datasets ↓ rsync
25. Scenario 17: Limit SCP Bandwidth
Suppose you're copying a large file over a production network and don't want SCP consuming all available bandwidth.
Use:
scp -l 5000 largefile.iso user@server:/backup/
-l specifies the bandwidth limit in Kbit/s.
For example:
5000 Kbit/s ≈ 5 Mbit/s
This can be useful when transferring large backups during business hours.
26. Scenario 18: Compress Data During Transfer
You can use:
scp -C file.txt user@server:/tmp/
-C enables SSH compression.
This can be useful for:
- text files
- logs
- source code
- JSON
- CSV
- configuration files
It may provide little or no benefit for already-compressed data such as:
.zip .gz .jpg .mp4 .iso
For example:
scp -C application.log user@server:/tmp/
27. Scenario 19: Preserve File Attributes
Suppose:
script.sh
has:
-rwxr-xr-x
You want to preserve its timestamps and mode bits.
Use:
scp -p script.sh user@server:/opt/scripts/
The -p option preserves modification time, access time, and file mode bits.
28. Scenario 20: Copy Between Two Remote Servers
Suppose:
Server A 10.0.0.10 Server B 10.0.0.20
You want:
Server A:/backup/data.tar.gz ↓ Server B:/backup/
One form is:
scp userA@10.0.0.10:/backup/data.tar.gz userB@10.0.0.20:/backup/
Be careful with this operation because authentication and network reachability between the hosts matter.
Modern SCP also has -R, which changes how a remote-to-remote copy is performed: the connection is made to the origin host and SCP is executed there, requiring that origin host to authenticate to the destination without requiring a password.
29. Scenario 21: Jump Through a Bastion Server
This is extremely useful in enterprise environments.
Imagine:
Your Laptop | v Bastion / Jump Server 10.10.10.10 | v Private Server 10.20.20.20
The private server cannot be accessed directly from your laptop.
Use:
scp -J bastion@10.10.10.10 file.txt app@10.20.20.20:/tmp/
The -J option uses an SSH jump host/ProxyJump mechanism.
This is particularly useful for:
- AWS private subnets
- Azure VNets
- Kubernetes infrastructure
- enterprise data centers
- production networks
30. Real Enterprise Example
Imagine:
Laptop | | SSH v Bastion | | SSH v Production Server | +--- /opt/application +--- /var/log +--- /backup
You need to upload:
hotfix.tar.gz
Command:
scp -J admin@bastion.company.com \ hotfix.tar.gz \ deploy@prod01.company.com:/tmp/
Then:
ssh -J admin@bastion.company.com deploy@prod01.company.com
and:
sudo tar -xzf /tmp/hotfix.tar.gz -C /opt/application/
31. Scenario 22: Use SSH Config Instead of Long Commands
Instead of:
scp -i ~/.ssh/company.pem \ -P 2222 \ -J bastion@10.10.10.10 \ application.tar.gz \ deploy@10.20.20.20:/tmp/
you can configure:
~/.ssh/config
Example:
Host prod HostName 10.20.20.20 User deploy IdentityFile ~/.ssh/company.pem Port 2222 ProxyJump bastion Host bastion HostName 10.10.10.10 User admin
Now:
scp application.tar.gz prod:/tmp/
Much easier.
You can also:
ssh prod
The same SSH configuration is usable by SCP because SCP passes SSH-related configuration through to SSH.
32. Scenario 23: Copy a File From Server to Server Through Your Laptop
Suppose:
Server A | | Laptop | | Server B
The data can pass through your local machine.
For example:
scp userA@serverA:/data/file.tar.gz userB@serverB:/backup/
Depending on the SCP implementation/options and protocol behavior, remote-to-remote transfers can be handled differently.
When you specifically need the transfer to go through the local machine, the -3 option is available in implementations supporting it:
scp -3 userA@serverA:/data/file.tar.gz userB@serverB:/backup/
This is useful when:
- Server A can't directly reach Server B
- Your laptop can reach both
- You need to troubleshoot the transfer path
33. Scenario 24: SCP With IPv4
If IPv4 is required:
scp -4 file.txt user@server:/tmp/
IPv6:
scp -6 file.txt user@server:/tmp/
The current OpenSSH SCP supports IPv4/IPv6 selection.
34. Scenario 25: Troubleshoot a Failed SCP Connection
Suppose:
scp file.txt user@server:/tmp/
returns:
Permission denied
or:
Connection refused
or:
Connection timed out
Use verbose mode:
scp -v file.txt user@server:/tmp/
For even more SSH diagnostics:
scp -vvv file.txt user@server:/tmp/
-v enables verbose diagnostic output, which is particularly useful for authentication, connection, and configuration problems.
35. Troubleshooting: Connection Refused
If you see:
ssh: connect to host 192.168.1.100 port 22: Connection refused
Check on the server:
sudo systemctl status ssh
or on some distributions:
sudo systemctl status sshd
Check whether port 22 is listening:
sudo ss -tlnp | grep :22
From the client:
nc -zv 192.168.1.100 22
Potential causes:
SSH service stopped ↓ Wrong IP ↓ Wrong port ↓ Firewall ↓ Security group ↓ Network routing
36. Troubleshooting: Permission Denied
Example:
scp: dest open "/etc/app/config.yml": Permission denied
Your SSH user probably doesn't have write permission.
Check:
ssh user@server
Then:
ls -ld /etc/app
Instead of trying to force SCP, upload to a writable location:
scp config.yml user@server:/tmp/
Then:
ssh user@server
sudo mv /tmp/config.yml /etc/app/
37. Troubleshooting: SSH Key Permission
You may see:
WARNING: UNPROTECTED PRIVATE KEY FILE!
Fix:
chmod 600 ~/.ssh/mykey
Then:
scp -i ~/.ssh/mykey file.txt user@server:/tmp/
Private SSH keys should not be broadly readable.
38. Troubleshooting: Host Key Warning
You might see:
WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!
Do not blindly remove the key.
First determine whether the server was legitimately rebuilt/replaced or whether you're connecting to a different machine.
Check:
ssh-keygen -F server
If the host change is legitimate, update the relevant known_hosts entry using the appropriate ssh-keygen workflow.
This protects against connecting to an unexpected host.
39. SCP and File Permissions
Suppose you copy:
script.sh
with:
chmod +x script.sh
Normal SCP behavior does not necessarily mean every ownership/permission characteristic you expect will be preserved.
If preserving mode and timestamps matters:
scp -p script.sh user@server:/opt/scripts/
For ownership such as:
root:root
you generally need appropriate privileges on the destination.
For example:
scp script.sh admin@server:/tmp/ ssh admin@server sudo chown root:root /tmp/script.sh sudo mv /tmp/script.sh /opt/scripts/
40. SCP Does Not Create Magic Permissions
Suppose:
scp config.yml user@server:/etc/myapp/
fails.
Changing the command to:
scp -p config.yml user@server:/etc/myapp/
does not solve a permission problem.
-p means preserve attributes.
It doesn't mean:
"Please copy as root."
41. SCP With Spaces in File Names
Suppose:
My Backup.tar.gz
You can use:
scp "My Backup.tar.gz" user@server:/backup/
or:
scp My\ Backup.tar.gz user@server:/backup/
Quoting is generally easier to read.
42. SCP and Paths With Spaces on the Remote Server
Suppose remote destination is:
/home/user/My Backups/
Use appropriate quoting/escaping.
For example:
scp backup.tar.gz user@server:"/home/user/My Backups/"
When shell interpretation and remote paths become complicated, using SFTP can sometimes be clearer.
43. SCP With a File Starting With -
Suppose a file is named:
-test.txt
Commands that begin with - can be interpreted as options.
Use:
scp -- -test.txt user@server:/tmp/
The -- convention tells the command that following arguments should be treated as operands rather than options.
44. SCP Return Code
SCP returns:
0
when the operation succeeds and a nonzero value when an error occurs.
This is useful in scripts.
Example:
scp backup.tar.gz backup@server:/backup/ if [ $? -eq 0 ]; then echo "Backup transfer successful" else echo "Backup transfer failed" fi
A cleaner shell pattern is:
if scp backup.tar.gz backup@server:/backup/; then echo "Backup transfer successful" else echo "Backup transfer failed" fi
45. SCP in a Backup Script
Example:
#!/bin/bash BACKUP="/backup/database.sql.gz" SERVER="backup@192.168.1.50" DEST="/data/backups/" if scp "$BACKUP" "$SERVER:$DEST"; then echo "Backup successfully transferred" else echo "Backup transfer failed" exit 1 fi
This can be integrated into:
- cron
- systemd timers
- backup scripts
- CI/CD pipelines
- disaster-recovery workflows
46. SCP + Date-Based Backups
Suppose you generate:
database-2026-09-25.sql.gz
You could:
DATE=$(date +%F) scp "database-${DATE}.sql.gz" \ backup@server:/backup/database/
Result:
/backup/database/database-2026-09-25.sql.gz
47. SCP in a Hadoop Environment
Imagine a Hadoop cluster:
master01 worker01 worker02 worker03
You have:
hdfs-site.xml
and need to distribute it.
For a small lab:
scp hdfs-site.xml hadoop@worker01:/tmp/ scp hdfs-site.xml hadoop@worker02:/tmp/ scp hdfs-site.xml hadoop@worker03:/tmp/
For many machines, however, use automation tools such as:
Ansible
rather than manually maintaining dozens of SCP commands.
For example:
SCP ↓ Good for quick/manual transfer Ansible ↓ Good for repeatable fleet-wide configuration
48. SCP in Kubernetes Troubleshooting
If you need to move a file between your workstation and a Kubernetes pod, you will normally use:
kubectl cp
rather than SCP.
For example:
kubectl cp ./debug.log namespace/pod:/tmp/debug.log
But if the Kubernetes node itself is accessible over SSH, SCP can be useful for transferring files to/from the node:
scp debug.tar.gz admin@k8s-node01:/tmp/
So remember:
Linux host ↔ Linux host ↓ scp Local machine ↔ Kubernetes pod ↓ kubectl cp
49. SCP in Docker Environments
Suppose Docker is running on a remote server.
You want to send:
app.tar.gz
to the host:
scp app.tar.gz admin@docker01:/tmp/
Then:
ssh admin@docker01
and:
docker cp /tmp/app.tar.gz container_name:/app/
Again:
Laptop | SCP ↓ Docker Host | docker cp ↓ Container
50. SCP With Cloud Servers
A typical cloud workflow:
Developer Laptop | | SCP ↓ Cloud VM | +--- Application +--- Logs +--- Configuration +--- Backups
Example:
scp -i production.pem \ release.tar.gz \ ubuntu@10.20.30.40:/tmp/
Then:
ssh -i production.pem ubuntu@10.20.30.40
and deploy.
51. SCP vs SFTP vs rsync
These commands are related but serve different purposes.
| Tool | Best For |
|---|---|
scp | Simple file/directory copies |
sftp | Interactive file management |
rsync | Synchronization and incremental transfers |
ssh | Remote commands/login |
tar + SSH | Streaming archives |
kubectl cp | Kubernetes pod transfers |
SCP
scp file.txt server:/tmp/
Simple.
SFTP
sftp user@server
Then:
put file.txt get backup.tar.gz ls cd /backup
rsync
rsync -avP ./website/ user@server:/var/www/website/
Better suited to repeated synchronization.
52. SCP vs rsync: Real Example
Suppose you have a 100 GB website/data directory.
First transfer:
scp -r website user@server:/var/www/
Later, only 500 MB changes.
SCP will generally copy the selected files again.
With:
rsync -avP website/ user@server:/var/www/website/
rsync can synchronize changes much more efficiently.
Therefore:
One-time transfer ↓ SCP Continuous synchronization ↓ rsync
53. SCP vs SFTP
Use SCP when you know exactly what you want:
scp report.pdf user@server:/reports/
Use SFTP when you want to interact with the remote filesystem:
sftp user@server
Then:
ls cd reports put report.pdf get result.csv mkdir archive
54. Modern SCP: Important Technical Detail
A common misconception is:
"SCP always uses the old SCP protocol."
That is no longer correct for modern OpenSSH.
Since OpenSSH 9.0, scp uses the SFTP protocol by default. The legacy SCP protocol can be explicitly selected using:
-O
For example:
scp -O file.txt user@server:/tmp/
The legacy mode may still be necessary for certain older servers or compatibility cases.
This distinction is important when troubleshooting behavior involving:
- wildcard expansion
-
~paths - older SSH servers
- unusual filenames
- legacy implementations
55. When Would You Use -O?
Normally:
scp file.txt user@server:/tmp/
uses modern SFTP-backed SCP behavior.
If you have an old SSH server where compatibility requires the legacy SCP protocol:
scp -O file.txt user@server:/tmp/
Don't use -O simply because you see it in an old tutorial.
Use it when you have a specific compatibility reason.
56. Common SCP Options Cheat Sheet
| Option | Meaning |
|---|---|
-r | Recursive directory copy |
-p | Preserve timestamps/mode bits |
-P | SSH port |
-i | SSH private key |
-C | Compression |
-l | Bandwidth limit in Kbit/s |
-v | Verbose diagnostics |
-q | Quiet mode |
-4 | IPv4 |
-6 | IPv6 |
-J | Jump host |
-F | Alternative SSH config |
-o | SSH option |
-O | Force legacy SCP protocol |
-R | Remote-to-remote transfer via origin host |
These options are documented in the current OpenSSH SCP manual.
57. The Most Useful SCP Commands to Memorize
Upload file
scp file.txt user@server:/tmp/
Download file
scp user@server:/tmp/file.txt .
Upload directory
scp -r mydir user@server:/tmp/
Download directory
scp -r user@server:/tmp/mydir .
Specify port
scp -P 2222 file.txt user@server:/tmp/
Specify SSH key
scp -i ~/.ssh/id_ed25519 file.txt user@server:/tmp/
Preserve attributes
scp -p file.txt user@server:/tmp/
Compression
scp -C file.txt user@server:/tmp/
Verbose troubleshooting
scp -v file.txt user@server:/tmp/
Jump host
scp -J user@bastion file.txt user@private-server:/tmp/
Limit bandwidth
scp -l 5000 largefile user@server:/tmp/
58. A Practical SCP Lab
You can practice everything on two Linux machines.
Suppose:
Machine A 192.168.1.10 Machine B 192.168.1.20
Create a test file:
echo "Hello SCP" > test.txt
Upload:
scp test.txt user@192.168.1.20:/tmp/
SSH into the server:
ssh user@192.168.1.20
Check:
cat /tmp/test.txt
You should see:
Hello SCP
Now download it:
scp user@192.168.1.20:/tmp/test.txt ./downloaded.txt
Check:
cat downloaded.txt
59. Practice Directory Transfer
Create:
mkdir -p scp-demo/{config,logs,scripts}
Create files:
echo "application configuration" > scp-demo/config/app.conf echo "application log" > scp-demo/logs/app.log echo '#!/bin/bash' > scp-demo/scripts/start.sh
Copy:
scp -r scp-demo user@192.168.1.20:/tmp/
On the server:
find /tmp/scp-demo -type f
You should see:
/tmp/scp-demo/config/app.conf /tmp/scp-demo/logs/app.log /tmp/scp-demo/scripts/start.sh
60. A Real-World DevOps Scenario
Imagine you're deploying an application manually.
Your project:
myapp/ ├── app.py ├── requirements.txt ├── config.yaml ├── templates/ └── static/
Create an archive:
tar -czf myapp.tar.gz myapp/
Upload:
scp myapp.tar.gz deploy@prod01:/tmp/
Connect:
ssh deploy@prod01
Extract:
tar -xzf /tmp/myapp.tar.gz -C /opt/
Restart:
sudo systemctl restart myapp
Complete flow:
Developer | | tar v myapp.tar.gz | | SCP v Production Server | | tar v /opt/myapp | | systemctl v Application
This is a very common pattern for small/manual deployments.
61. SCP Security Best Practices
1. Prefer SSH keys
Instead of repeatedly using passwords:
scp -i ~/.ssh/id_ed25519 file.txt user@server:/tmp/
2. Protect private keys
chmod 600 ~/.ssh/id_ed25519
3. Don't blindly disable host-key checking
Avoid casually using:
-o StrictHostKeyChecking=no
especially in production.
It weakens an important SSH trust check.
4. Don't expose SSH unnecessarily
If a server is Internet-facing:
Internet | v SSH | Firewall | Server
Use appropriate:
- firewall rules
- security groups
- VPN
- bastion hosts
- key-based authentication
- least-privilege accounts
5. Avoid direct root SSH where possible
Prefer:
deploy user | v sudo | v privileged operation
rather than routinely logging in as root.
62. SCP in Automation
SCP can be integrated into:
Bash
scp "$FILE" "$SERVER:$DEST"
Python
import subprocess subprocess.run([ "scp", "backup.tar.gz", "backup@server:/backup/" ], check=True)
CI/CD
For example:
Git ↓ Build ↓ Test ↓ Package ↓ SCP ↓ Server ↓ Deploy
For larger production environments, dedicated deployment tools are generally preferable to building an entire deployment system around SCP.
63. A Better Production Pattern
Instead of:
scp app.tar.gz root@production:/opt/app/
consider:
Developer | v CI/CD | +-- Build +-- Test +-- Security scan +-- Artifact | v Deployment system | v Production
SCP remains useful for:
- emergency transfers
- debugging
- manual operations
- administrative tasks
- one-off artifacts
- small environments
64. SCP Troubleshooting Decision Tree
When SCP fails:
SCP failed | +---- Can SSH connect? | | | +-- NO → troubleshoot SSH/network | +---- YES | +---- Permission denied? | | | +-- Check destination permissions | +---- No such file? | | | +-- Check source/destination path | +---- Connection timeout? | | | +-- Check firewall/network/port | +---- Key problem? | | | +-- Check -i and key permissions | +---- Strange wildcard/path behavior? | +-- Check modern SFTP behavior +-- Consider -O for legacy compatibility
65. The 10 SCP Scenarios You Should Practice
If you're learning Linux administration, practice these:
Scenario 1
Upload a file:
scp file.txt user@server:/tmp/
Scenario 2
Download a file:
scp user@server:/tmp/file.txt .
Scenario 3
Copy a directory:
scp -r project user@server:/opt/
Scenario 4
Use a different SSH port:
scp -P 2222 file.txt user@server:/tmp/
Scenario 5
Use an SSH key:
scp -i ~/.ssh/id_ed25519 file.txt user@server:/tmp/
Scenario 6
Preserve attributes:
scp -p script.sh user@server:/opt/scripts/
Scenario 7
Use a jump server:
scp -J bastion@jumpserver file.txt user@private-server:/tmp/
Scenario 8
Debug connection:
scp -vvv file.txt user@server:/tmp/
Scenario 9
Limit bandwidth:
scp -l 5000 backup.tar.gz user@server:/backup/
Scenario 10
Copy server logs:
scp user@server:/var/log/application.log .
Master these and you'll be comfortable with most everyday SCP operations.
66. SCP Mental Model
The easiest way to remember SCP is:
scp SOURCE DESTINATION
Ask yourself:
Where is the source? Where should it go?
For local → remote:
LOCAL | | SCP v REMOTE
scp file.txt user@server:/tmp/
For remote → local:
REMOTE | | SCP v LOCAL
scp user@server:/tmp/file.txt .
For directory:
Add -r
For SSH key:
Add -i
For SSH port:
Add -P
For jump server:
Add -J
For troubleshooting:
Add -v
That's the core of SCP.
67. SCP Cheat Sheet
# Local → Remote scp file.txt user@server:/tmp/ # Remote → Local scp user@server:/tmp/file.txt . # Directory → Remote scp -r directory user@server:/tmp/ # Remote directory → Local scp -r user@server:/tmp/directory . # Multiple files scp file1 file2 file3 user@server:/tmp/ # Custom SSH port scp -P 2222 file.txt user@server:/tmp/ # SSH key scp -i ~/.ssh/id_ed25519 file.txt user@server:/tmp/ # Preserve attributes scp -p file.txt user@server:/tmp/ # Compression scp -C file.txt user@server:/tmp/ # Bandwidth limit scp -l 5000 file.iso user@server:/tmp/ # Verbose debugging scp -v file.txt user@server:/tmp/ # Jump host scp -J user@bastion file.txt user@private-server:/tmp/ # IPv4 scp -4 file.txt user@server:/tmp/ # IPv6 scp -6 file.txt user@server:/tmp/ # Legacy SCP protocol scp -O file.txt user@server:/tmp/
68. Final Takeaway
Think of SCP as:
SSH + simple file transfer
The fundamental command is:
scp SOURCE DESTINATION
Once you understand the remote path format:
user@server:/path/
everything else becomes an option around that basic concept.
The most important commands to remember are:
scp file user@server:/path/
scp user@server:/path/file .
scp -r directory user@server:/path/
scp -i key.pem file user@server:/path/
scp -P 2222 file user@server:/path/
scp -J bastion@server file user@private-server:/path/
scp -v file user@server:/path/
And remember the practical distinction:
SCP → quick/simple file transfer SFTP → interactive remote file management rsync → repeated synchronization/large data SSH → remote shell/commands
Modern OpenSSH has also changed an important implementation detail: SCP uses SFTP by default since OpenSSH 9.0, while -O forces the legacy SCP protocol when compatibility requires it.
No comments:
Post a Comment
Thank you for Commenting Will reply soon ......