Secure Copy Protocol (SCP) Command is a method of securely moving files between local and remote host. Using the command-line utility, one can even transfer data between two remote hosts. SCP roots from cryptographic network protocol Secure Shell (SSH). Typically, one leverages SSH to operate network services over the unsecured network securely.
What is SCP Command?
In a controversial viewpoint, developers of the OpenBSD Secure Shell (OpenSSH) stated that SCP is outdated and not the best pick for transferring files in modern time.
SCP is still very popular and leveraged by many to securely transfer files between hosts on a network. Commonly, most use the Secure Copy Protocol programs such as Putty, WinSCP, Filezilla, and more. However, the most widely used application is the ‘command line SCP.’ It comes in almost every SSH implementations.
In simple terms, the Secure Copy Protocol facilitates file or directory transfer between two locations. Below are things that people can do with SCP:
- Transfer files from the local machine to a remote system and vice versa.
- It allows file transfer between two remote systems from the local machine.
Most use SCP as it encrypts files and passwords at the time of transferring. Hence, preventing anyone from prying or copying sensitive information by interfering in the network.
SCP Command Syntax
Every protocol has a basic syntax and is best to learn it before diving into using commands to transfer files or directories. The syntax is homogeneous to the scp command in Unix-like operating systems for copying files and directories.
The general SCP command takes the following form:
scp [OPTION] [user@]SRC_HOST:]file1 [user@]DEST_HOST:]file2
Below is the breakdown of the expression:
- [user@]SRC_HOST:]file1 – this indicates the source file.
- [user@]DEST_HOST:]file2 – this stipulates the destination file
- OPTION – it specifies options such as recursive copy, cipher, ssh port, etc.
Note: It is necessary to specify user and host specification for remote files. Also, one must define the absolute or relative path for local files.
The SCP comes with various parameters that allow users to control every feature easily. Among them, the most common are as follows:
- -q – The option will repress the progress meter and non-error messages.
- -r – This will copy files recursively.
- -P – It defines the SSH port of the remote host
- -C – The option compresses data to speed up file transfer
- -p – It safeguards files from being modified or accessed.
- (:) – The SCP uses colons to differentiate between local and remote destinations.
The users must also ensure to obtain an SSH key or password before beginning to transfer files as the command-utility uses SSH for data transfer. The SSH key is used to validate the remote systems. Before initiating file transfer using SCP, users must ensure they have read permission for the source file. Also, one must have write permission consent on the target system.
One of the drawbacks of SCP is that it overwrites a file without giving a warning it shares the same name with another file in the same location. Also, it is best to open SCP in a tmux session when transferring large files.
Transferring Files Using SCP Command
Transferring files with SCP Command is very easy, and below, we will demonstrate ways to transfer files with examples. We will also provide several examples that will make it simple for anyone to use the SCP parameters.
Copying the Local File to a Remote System
Copying a local file to remote is very and can be achieved by executing the following SCP command:
$ scp trial.txt [email protected]:/remote/directory
Let’s breakdown the above command.
In the command:
- Filename – trial.txt is the file that a user wants to copy to the remote system.
- Remote server – remote_mike is the name of the remote server
1. The command ends with the path of the directory. In case, one does not specify the path; the file will be copied to the home directory of the remote system. The file will begin to transfer once a user enters the password. Below is what will be the outcome of the code:
[email protected]'s password: trial.txt 100% 0 0.0KB/s 00:00
2. In case, one removes the files from the location; then it will copy the file with the original name. If one changes the file name then they need to mention the new name in the command which is probe.txt So the new command will look like this:
$ scp trial.txt [email protected]:/remote/directory/probe.txt
3. A user has to use the -P parameter to define the port in case of SSH on the remote system is listening to a different port than the default.
$ scp -P 2322 trial.txt [email protected]:/remote/directory
4. In case, a user wants to copy the entire directory rather than they have to use the -r parameter for copying files recursively. Below is the command using -r option to copy a directory from local to a remote system:
$ scp -r /local/directory [email protected]:/remote/directory
Copy the remote file to the local system
Copying a remote file to a local system is as easy as the other way round. The only difference is that a remote location becomes the source and the local system now becomes the receiver. So run the following command to copy a file that is named trial.txt from the remote system:
$ scp [email protected]:/remote/trial.txt /local/directory
The command is for the remote server that has the IP address 10.10.0.5. Also, if there is a password on the remote system, then it will prompt the user to enter the password.
Copy Files Between Two Remote Systems Using SCP Command
One of the best advantages of using SCP command-line utility is that it does not require any login to a server to transfer files between two remote systems.
Below is the command to transfer files between the two remote servers:
$ scp [email protected]:/experiment/experiment.txt [email protected]:/experiment
For example, the breakdown of the above command is
- File to transfer – experiment/experiment.txt
- Remote system – host10.com
- Other remote machines -host20.com
- Directory – /experiment.
The user will have to enter the password for both remote accounts to transfer the file.
In case, one wants to direct the traffic via this system from which the command is executed than use the -3 parameter. Below is the command to route the traffic to the system:
$ scp -3 [email protected]:/experiment/experiment.txt [email protected]:/experiment
Using -C Parameter
Generally, one uses the -C parameter to compress files and make file transfer quicker. The parameter is convenient and will compress any file on the go. Interestingly, the file compression only takes place in the network, so when the destination system receives the file, they will get the original size and not the compressed version. But one must note that already compressed files such as .zip, .rar, etc. will not see any difference using the -C parameter.
- Username – mike@putty
- Destination_folder – /Documents
- File – trial.log
- Destination_host – [email protected](IP address)
mike@putty ~/Documents $ scp -Cpv trial.log [email protected]:.
The above SCP commands demonstrate an easy way to transfer files and directories from local to remote and vice versa. It also covers the way to transfer files between two remote systems. To link to Linux server, one can choose to set up SSH key-based authentication. Also, users are not required to input any passwords.