scp — remote file copy
💬 "scp command"
scp is file copying that operates over ssh. It is the simplest way to move one or two files.
For bulk or incremental transfers, rsync is better; scp is used for simple transfers.
- Concept: Uses ssh authentication as-is to copy files.
- Left argument: Source path. If remote,
user@host:/path. - Right argument: Destination path.
- Direction: Both local→remote and remote→local are possible.
Frequently used options
| Option | Description |
|---|---|
| -P <port> | Specifies the SSH port (uppercase P) |
| -i <key> | Specifies the key to use |
| -r | Copies directories recursively |
| -C | Compresses during transfer |
Three essential command combinations
scp -i ~/.ssh/deploy app.tgz deploy@10.0.0.9:/tmp/— Send the release file to the serverscp -P 22022 deploy@10.0.0.9:/var/log/app/error.log ./— Fetch the server log to the local machinescp -r -i ~/.ssh/deploy ./conf deploy@10.0.0.9:/tmp/conf— Transfer the entire configuration directory
💬 "Tell me common real-world mistakes and how to avoid them"
Here are common situations on live production servers, in order.
Step 1 · Transfer
Send the file to the target server.
scp -i ~/.ssh/deploy app-20261018.tgz deploy@10.0.0.9:/tmp/
Step 2 · Verification
Compare the size and checksum.
ssh deploy@10.0.0.9 'md5sum /tmp/app-20261018.tgz' && md5sum app-20261018.tgz
Step 3 · Apply
After verification, proceed with the deployment procedure.
ssh deploy@10.0.0.9 'tar xzf /tmp/app-20261018.tgz -C /opt/app/releases/20261018'
How this is used in practice
1. Transferring release files
Situation — Sending artifacts from the build server to the production server.
scp -i ~/.ssh/deploy dist/app.tgz deploy@10.0.0.9:/tmp/
Result — The file is transferred to /tmp and progress is displayed.
Point — Comparing checksums after transfer catches corrupted transfers immediately.
2. Retrieving incident logs
Situation — You need to analyze production server logs locally.
scp -i ~/.ssh/deploy deploy@10.0.0.9:/var/log/app/error.log /tmp/srv01-error.log
Result — The logs are copied locally so you can analyze them with convenient tools.
Point — For large logs, compress them before downloading or grep only the section you need.
Things to watch out for
- The port option is uppercase -P (ssh uses lowercase -p).
- scp does not support resuming or incremental transfer, so it is inefficient for large files.
- By default, scp does not fully preserve permissions and ownership.
- Do not overwrite production paths directly; download to /tmp and verify.
Actual execution screen
This is the result of transferring the release file and verifying its checksum.
!scp command execution result: file transfer and checksum
Live production server
Source (HTML): https://games.aidebatehub.com/programs/linux/remote/scp.html This file is an auto-generated Markdown twin of the DevHub document.
AI Knowledge Hub
Visitor Comments