scp
- Securely Copy Files
Summary
The scp
(Secure Copy) command is a command-line utility that allows you to securely copy files and directories between a local and a remote host, or between two remote hosts. It leverages the SSH protocol for secure data transfer.
Introduction
scp
provides a secure way to transfer files over a network. It encrypts the data during the transfer, protecting sensitive information from eavesdropping. scp
utilizes SSH for authentication and encryption, making it a reliable choice for transferring files between systems where SSH is enabled. It’s widely used for deploying applications, backing up data, and sharing files between servers and desktops.
Use case and Examples
Copying a local file to a remote server
This command copies thelocalfile.txt
from your local machine to the /path/to/destination/
directory on the remote server remotehost
as user user
. You will be prompted for the user's password on the remote server. Copying a remote file to your local machine
This command copies the file/path/to/remotefile.txt
from the remote server remotehost
as user user
to the /local/destination/
directory on your local machine. You will be prompted for the user's password on the remote server. Copying a directory recursively from local to remote
This command copies the entire directorylocaldirectory
and its contents to the /path/to/destination/
directory on the remote server. The -r
flag enables recursive copying. Copying a file from one remote server to another (via your local machine)
This command copies the file/path/to/file.txt
from remotehost1
(as user1
) to remotehost2
(as user2
), using your local machine as an intermediary. You'll be prompted for the passwords for both user accounts. Specifying a different SSH port
If the remote server uses a non-standard SSH port (e.g., 2222), use the-P
flag to specify the port number. This is crucial when the remote server's SSH service doesn't run on the default port 22. Commonly used flags
Flag | Description | Example |
---|---|---|
-r | Recursive copy (for directories). | scp -r localdirectory user@remotehost:/path/to/destination/ |
-P | Specifies the port to connect to on the remote host. | scp -P 2222 localfile.txt user@remotehost:/path/to/destination/ |
-q | Suppresses the display of the progress meter and diagnostic messages. | scp -q localfile.txt user@remotehost:/path/to/destination/ |
-C | Enables compression during transfer. Useful for slow network connections. | scp -C localfile.txt user@remotehost:/path/to/destination/ |
-v | Verbose mode. Displays detailed information about the transfer process. | scp -v localfile.txt user@remotehost:/path/to/destination/ |
-i | Specifies the identity file (private key) for authentication. | scp -i ~/.ssh/id_rsa localfile.txt user@remotehost:/path/to/destination/ |
-l | Limits the bandwidth used by scp , specified in Kbit/s. | scp -l 800 localfile.txt user@remotehost:/path/to/destination/ |