LinuxCommandLibrary

scp

TLDR

Copy a local file to a remote host

$ scp [path/to/file] [user]@[host]:[path/to/destination]
copy
Copy a file from a remote host to local
$ scp [user]@[host]:[path/to/file] [path/to/destination]
copy
Copy a directory recursively
$ scp -r [path/to/directory] [user]@[host]:[path/to/destination]
copy
Use a specific SSH port
$ scp -P [2222] [path/to/file] [user]@[host]:[path/to/destination]
copy
Use a specific identity file (SSH key)
$ scp -i [~/.ssh/id_rsa] [path/to/file] [user]@[host]:[path/to/destination]
copy
Copy with compression enabled
$ scp -C [path/to/file] [user]@[host]:[path/to/destination]
copy
Preserve file attributes
$ scp -p [path/to/file] [user]@[host]:[path/to/destination]
copy
Copy between two remote hosts via local
$ scp -3 [user1]@[host1]:[file] [user2]@[host2]:[destination]
copy

SYNOPSIS

scp [options] source destination
scp [options] [[user@]host:]file [[user@]host:]file

DESCRIPTION

scp (secure copy) transfers files between hosts over SSH. It uses SSH for authentication and encryption, providing the same security as an SSH session. The colon (:) distinguishes remote paths from local paths.
Files can be copied from local to remote, remote to local, or between two remote hosts. When copying directories, use -r for recursive transfer. The remote path syntax is [user@]host:path, where user defaults to the current username.
Authentication uses SSH keys or passwords, configured through ssh_config or command-line options. Transfer progress is shown unless -q is specified. The command exits 0 on success or >0 on error.

PARAMETERS

-r

Recursively copy directories
-P PORT
Connect to specified port on remote host
-p
Preserve modification times, access times, and modes
-C
Enable compression
-c CIPHER
Select encryption cipher
-i FILE
Identity file (private key) for authentication
-l KBITS
Limit bandwidth in Kbit/s
-o OPTION
Pass options to ssh (ssh_config format)
-F FILE
Use specified ssh config file
-q
Quiet mode; suppress progress meter and warnings
-v
Verbose mode; show debugging messages
-B
Batch mode; disable password prompts
-3
Copy between remote hosts through local host
-4
Force IPv4 only
-6
Force IPv6 only
-S PROGRAM
Use specified program for encrypted connection

CAVEATS

scp uses an older protocol (SCP/RCP) that has security limitations. The OpenSSH project recommends sftp or rsync over SSH for new scripts. Wildcards in remote paths must be quoted to prevent local shell expansion. When copying between two remote hosts without -3, the hosts connect directly, which may fail if they cannot reach each other.

HISTORY

scp originated as part of the SSH protocol suite developed by Tatu Ylönen in 1995 at Helsinki University of Technology. It was designed as a secure replacement for the insecure rcp (remote copy) command. The OpenSSH project, started in 1999, provides the most widely used implementation. While still functional, scp's underlying protocol has known issues, leading to recommendations to use sftp for new deployments.

SEE ALSO

sftp(1), rsync(1), ssh(1), ssh-keygen(1)

Copied to clipboard