LinuxCommandLibrary

scp

Securely copy files between computers

TLDR

Copy a local file to a remote host

$ scp [path/to/local_file] [remote_host]:[path/to/remote_file]
copy

Use a specific port when connecting to the remote host
$ scp -P [port] [path/to/local_file] [remote_host]:[path/to/remote_file]
copy

Copy a file from a remote host to a local directory
$ scp [remote_host]:[path/to/remote_file] [path/to/local_directory]
copy

Recursively copy the contents of a directory from a remote host to a local directory
$ scp -r [remote_host]:[path/to/remote_directory] [path/to/local_directory]
copy

Copy a file between two remote hosts transferring through the local host
$ scp -3 [host1]:[path/to/remote_file] [host2]:[path/to/remote_directory]
copy

Use a specific username when connecting to the remote host
$ scp [path/to/local_file] [remote_username]@[remote_host]:[path/to/remote_directory]
copy

Use a specific SSH private key for authentication with the remote host
$ scp -i [~/.ssh/private_key] [path/to/local_file] [remote_host]:[path/to/remote_file]
copy

Use a specific proxy when connecting to the remote host
$ scp -J [proxy_username]@[proxy_host] [path/to/local_file] [remote_host]:[path/to/remote_file]
copy

SYNOPSIS

scp [options] [[user@]host1:]file1 [[user@]host2:]file2

PARAMETERS

-p
    Preserves modification times, access times, and modes from the original file.

-q
    Suppresses progress meter and warning/diagnostic messages.

-r
    Recursively copy entire directories.

-v
    Verbose mode. Causes scp to print debugging messages about its progress.

-c cipher
    Selects the cipher to use for encrypting the data transfer. This option is directly passed to ssh.

-i identity_file
    Selects the file from which the identity (private key) for authentication is read. This option is directly passed to ssh.

-P port
    Specifies the port to connect to on the remote host. This option is directly passed to ssh.

-S program
    Name of program to use for the encrypted connection. The program must understand ssh(1) options.

[[user@]host1:]file1
    Source file specification. It can be a local file or a remote file in the format user@host:path.

[[user@]host2:]file2
    Destination file specification. It can be a local file or a remote file in the format user@host:path.

DESCRIPTION

The scp (Secure Copy) command is a command-line utility that allows users to securely transfer files between a local host and a remote host, or between two remote hosts. It leverages the SSH (Secure Shell) protocol for secure data transfer, ensuring confidentiality and integrity. scp utilizes the same authentication and encryption mechanisms as SSH, protecting against eavesdropping and man-in-the-middle attacks.

It is a crucial tool for system administrators and developers for tasks such as deploying applications to remote servers, backing up data, or sharing files securely. It is simple and provides direct access from the command line, but is now considered legacy in favour of more advanced tools such as rsync, which offers additional features like delta transfers. Despite its age, it remains widely used for its simplicity and security features built on SSH.

CAVEATS

scp relies on the underlying ssh subsystem for authentication and security. It's crucial to ensure that SSH is properly configured and secured. Also scp is deprecated in some environments in favor of sftp and other modern alternatives.

EXAMPLES

Copy a local file to a remote server:
scp localfile.txt user@remotehost:/path/to/destination/

Copy a remote file to the local machine:
scp user@remotehost:/path/to/remotefile.txt /local/destination/

Copy a directory recursively to a remote server:
scp -r localdirectory user@remotehost:/path/to/destination/

SECURITY CONSIDERATIONS

Always verify the fingerprint of the remote host to prevent man-in-the-middle attacks.
Use strong passwords or key-based authentication for SSH.
Ensure that the SSH server is properly configured and secured.

HISTORY

scp was originally developed as part of the OpenBSD project.
It was designed to provide a secure alternative to the insecure rcp command, leveraging the security features of SSH. scp became a standard tool for transferring files securely over networks, especially in Unix-like environments.
Over time, other more advanced tools like rsync and sftp have gained popularity, offering features such as resume functionality and more robust error handling. However, scp remains a widely used command due to its simplicity and wide availability.

SEE ALSO

ssh(1), sftp(1), rsync(1)

Copied to clipboard