SCP (Secure Copy Protocol) is a network protocol that lets Linux (Unix) systems on a network safely transfer files and directories. Use the safer alternative to the cp (copy) command, the scp command line utility, to send data.
By encrypting the files and the passwords, SCP secures your data while copying over an SSH (Secure Shell) connection. As a result, the data is still encrypted even if the communication is intercepted.
Make use of SCP command when:
- Copy files from a local host to a remote host.
- Copy files from a remote host to a local host.
- Copy files between two remote servers.
The scp
command will be used to demonstrate how to utilize it in this tutorial using real-world scenarios and in-depth discussions of the most popular scp
options.
Table of Contents
Prerequisites
- Logging into the server’s secure shell
- Both the client and the server to have root access.
- On the server system, a secure shell login
Syntax of SCP Command
The scp
command’s syntax is as follows:
scp [option] [user_name@source_host:path/to/source/file] [user_name@target_host:target/path]
If you don’t:
The host’s (or target’s) user_name; otherwise, the operation uses the current user by default.
The program searches for (or copies) the file locally using the path/to/source (or target) from the command.
Always specify the user and host details when working with remote files.
Use a user account on the source system that has read access to the file(s) you wish to copy. Additionally, you must use a user account with write access to the directory on the target system where the file(s) will be saved.
Note: The destination location is not verified before writing by the scp
command. Unnoticed overwriting of identical files in the destination will occur.
SCP Command Options
To modify and speed the operation, you can add a variety of scp command arguments to the command. Following the scp
command, options are added as attributes.
Each choice has a one-character, short form and a longer, more detailed equivalent.
Short Form Long Form Description -r
--recursive
Enable recursive copying for directories -P
--port
Specify the port for SSH connection -i
--identity
Specify the private key file for authentication -p
--preserve
Preserve file attributes during transfer -v
--verbose
Enable verbose output -q
--quiet
Disable progress bar
Before you start
The scp
command uses ssh to transfer data, therefore it needs to authenticate on the remote systems with an ssh key or password.
SCP distinguishes between local and remote sites using the colon (:).
You must have at least read permissions on the source file and write permission on the target system in order to copy files.
SCP will overwrite data without giving you a chance to stop it, so take care when copying files that have the same name and location on both systems.
SCP Command – Usage Examples
The scp
Command copies a local file to a remote system.
The following command should be used to copy a file from a local system to a remote one:
scp file.txt [email protected]:/remote/directory
The name of the file we want to copy is file.txt
, the user on the remote server is remote_username, and the server’s IP address is 10.10.0.2.
The path to the directory you wish to copy the file to is in the /remote/directory
field. The file will be copied to the remote user’s home directory if a remote directory is not specified.
The transfer process will begin after you are prompted to provide the user password.
output
[email protected]'s password:
file.txt 100% 0 0.0KB/s 00:00
When the filename is left out of the destination location, the file is copied with its original name.You must give the new file name if you wish to save the file with a different name:
scp file.txt [email protected]:/remote/directory/newfilename.txt
If the SSH service on the remote host is listening on a port other than the default port 22
, you can easily specify the desired port using the -P
argument. By doing so, you ensure that the scp
command establishes a connection on the correct port.
scp -P 2322 file.txt [email protected]:/remote/directory
The command to copy a directory is quite similar to the command used to copy files. The -r
flag for recursive is the only difference that has to be made.
The -r
option can be used to copy a directory from a local to a remote system:
scp -r ~/dir [email protected]:/remote/directory
SCP Command – Copy Remote File to Local System
Use a remote location as the source and a local location as the destination to copy a file from a remote system to a local one.
For example, run the following command to copy a file named file.txt from a remote server with the IP 10.10.0.2
scp [email protected]:/remote/file.txt /local/directory
You will be prompted for the user password if you haven’t set up a password less SSH login on the remote machine.
SCP Command – Copy File Between 2 Remote Systems
When transferring files between remote machines using scp
, unlike rsync
, you don’t need to log onto a server beforehand.
The file /files/file.txt
from remote host host1.com
will be copied to remote host host2.com's
directory /files
by the issuing the below command.
scp [email protected]:/files/file.txt [email protected]:/files
The login information for both remote accounts will be requested from you. Direct data transfers will take place between two remote hosts.
Use the -3
option to send traffic through the device where the command is being issued:
scp -3 [email protected]:/files/file.txt [email protected]:/files
Also read: How to Change Directory in Linux Using cd Command.
SCP Command on Linux – FAQs
What is SCP in Linux?
A secure SSH connection is used to transfer data between local and remote systems using the Linux command-line utility known as SCP (Secure Copy).
Can I use SCP to copy directories and their contents?
Recursive copying is supported by SCP, allowing you to move folders and all of their contents. scp -r source_directory> user>@remote_host>:destination_path>
uses the -r
or --recursive
option.
Can I specify a different SSH port for SCP connections?
Yes, by using the -P
or --port
option and the port number as the source and destination, you can choose a different SSH port.
Is verbose output possible during SCP transfers?
Yes, scp -v source> destination>
displays information about the SCP transfer process in detailed verbose output when the -v
or --verbose
option is used.
How can I keep file attributes during the SCP transfer?
During the file transfer, the -p
or --preserve
option retains file properties, such as timestamps, permissions, and ownership: scp -p source> destination>
.
Conclusion
A strong and safe tool for transferring files between local and remote systems is Linux’s SCP (Secure Copy Protocol) command. SCP preserves the security and integrity of data while it is in transit by employing the SSH protocol. I hope you now have a better understanding of how to safely copy data across computers using the scp
command. Please feel free to share your thoughts and feedback in the comment section below.