The Linux scp command: copy files by SSH

Introduction

In the world of Linux system management, transferring files safely between machines is a daily task. The SSH protocol provides an encrypted channel that protects information from interceptions and manipulation. Within this ecosystem, the commandscp(secure copy) stands out for its simplicity and its ability to copy files and directories using SSH.

What's scp?

scp is a command line utility that allows you to copy 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 is enough to have SSH access to the destination.

Basic syntax

The general form of the command is:

scp [opciones] origen destino

Whereorigenanddestinomay be local routes or format specificationsusuario@host:ruta. If the user is omitted, the current user of the client is assumed.

Most commonly used options

  • -r: activates the recursive mode to copy complete directories.
  • -P puerto: indicates the SSH port to use (capital P to avoid conflict with the scp option of-pof preservation).
  • -C: enables data compression during transfer, useful in limited bandwidth links.
  • -i archivo_clave: specifies the private key file for authentication when you do not want to use the default key.
  • -v: Vertical mode that shows debugging information, ideal for solving connection problems.

Practical examples

  • Copy a local file to the remote server:
    scp /home/local/document.pdf usuario@servidor.example.com:/tmp/
  • Copy a remote file to the local computer:
    scp usuario@servidor.example.com:/var/log/syslog ./
  • Copy a full directory recursively:
    scp -r /home/local/proyecto usuario@servidor.example.com:/home/usuario/
  • Use a port other than the default 22:
    scp -P 2222 archivo.txt usuario@servidor.example.com:/home/usuario/
  • Transfer between two remote servers (traffic passes through the customer):
    scp usuario@origen.example.com:/datos/respaldo.tar.gz usuario@destino.example.com:/backup/

Security tips

  • Always check the authenticity of the host the first time it is connected; the SSH client stores the server's print on~/.ssh/known_hosts.
  • Prefer the use of public / private keys rather than passwords to avoid brute force attacks.
  • Restrict access byAllowUsersorAllowGroupsin the SSH server configuration.
  • Monitor authentication records to detect suspicious attempts.

Conclusion

The commandscpis an essential tool for any advanced Linux administrator or user who needs to move files safely and quickly. Its integration with SSH guarantees confidentiality and integrity, while its simple syntax allows to make from simple copies to complex transfers between multiple systems. Practice the examples and options described above will allow you to make the most of this utility and keep your environment under control.

This work is under aCreative Commons License Attribution 4.0 International for Francesc Roig francesc @ vivaldi.net.

EnglishenEnglishEnglish