Introduction
In the world of Linux system administration, securely transferring files between machines is a daily task. The SSH protocol provides an encrypted channel that protects information from interception and tampering. Within this ecosystem, the scp (secure copy) command stands out for its simplicity and its ability to copy files and directories via SSH.
What is scp?
scp is a command-line utility that allows copying files between a local host and a remote host, or between two remote hosts, using the same authentication and encryption mechanism as SSH. Unlike tools such as FTP, scp does not require an additional daemon; it only requires SSH access to the destination.
Basic Syntax
The general form of the command is:
scp [options] source destination
Where source and destination can be local paths or specifications in the format user@host:path. If the user is omitted, the current client user is assumed.
Most Common Options
-r: enables recursive mode to copy entire directories.-P port: specifies the SSH port to use (uppercase P to avoid conflict with scp’s-ppreservation option).-C: enables data compression during transfer, useful on low-bandwidth links.-i keyfile: specifies the private key file for authentication when you do not want to use the default key.-v: verbose mode that displays debugging information, ideal for troubleshooting connection issues.
Practical Examples
- Copy a local file to the remote server:
scp /home/local/document.pdf user@server.example.com:/tmp/ - Copy a remote file to the local machine:
scp user@server.example.com:/var/log/syslog ./ - Copy an entire directory recursively:
scp -r /home/local/project user@server.example.com:/home/user/ - Use a port different from the default 22:
scp -P 2222 file.txt user@server.example.com:/home/user/ - Transfer between two remote servers (traffic goes through the client):
scp user@origin.example.com:/data/backup.tar.gz user@destination.example.com:/backup/
Security Tips
- Always verify the host’s authenticity the first time you connect; the SSH client stores the server’s fingerprint in
~/.ssh/known_hosts. - Prefer using public/private key pairs instead of passwords to avoid brute-force attacks.
- Restrict access via
AllowUsersorAllowGroupsin the SSH server configuration. - Monitor authentication logs to detect suspicious attempts.
Conclusion
The scp command is an essential tool for any Linux administrator or advanced user who needs to move files securely and quickly. Its integration with SSH ensures confidentiality and integrity, while its simple syntax allows everything from simple copies to complex transfers between multiple systems. Practicing the examples and options described will enable you to get the most out of this utility and keep your environment under control.
This post is also available in ESPAÑOL.