SCP Command - Securely Copy Files Remotely

Learn how to use the SCP command to securely copy files between remote servers and your local machine. Understand basic syntax, port specification, and alias usage.

SCP Command - Secure File Transfer

The scp command is a fundamental utility for securely copying files between hosts on a network. It leverages SSH (Secure Shell) for data transfer, ensuring that your files are encrypted during transit. This makes it a reliable choice for transferring sensitive data or simply for managing files across different servers.

Basic SCP Usage

The most common use case for scp is to copy a file from a remote server to your local machine. The syntax involves specifying the remote address and path, followed by the local destination.

# Securely copies files from remote ADDR's PATH to the current-working-directory.
# By default here, port 22 is used, or whichever port is otherwise configured.
scp ADDR:PATH ./

Using Aliases with SCP

scp supports the use of aliases, which can simplify commands when dealing with frequently accessed remote locations. These are not shell aliases but rather configurations that scp can interpret.

# Using aliases (not Bash aliases) work with scp(1) as well. In this example, -
# the PATH1 of the first remote source defined as ALIAS1 is sent to PATH2 of
# the remote destination defined as ALIAS2.
scp ALIAS1:PATH1 ALIAS2:PATH2

Specifying a Non-Standard Port

If the SSH server on the remote host is not running on the default port 22, you can specify a different port using the -P flag (uppercase P). This is crucial for connecting to servers with custom SSH configurations.

# You can use the `-P` flag -- uppercase, unlike ssh(1) -- to determine the
# PORT, in-case it's non-standard (not 22) or not defined within an alias.
scp -P PORT ADDR:PATH ./

Further Reading

For more advanced usage and options, consult the official scp manual page: