Javatpoint Logo
Javatpoint Logo

SCP Command in Linux/Unix with Examples

Introduction to SCP Command

SCP stands for Secure Copy Protocol. In the Linux system, the SCP command copies files between many servers securely. It permits secure file transferring between the remote host and the local host or two remote hosts. It applies the same security and authentication as it's used in the SSH (Secure Shell) protocol. The SCP command is known for its pre-installed availability, security, and simplicity.

Secure copy protocol is a command-line utility that permits the user to copy directories and files securely between two different locations, usually between Linux or Unix systems. The protocol guarantees the file transmission is encrypted to avoid anyone with wrong intentions from stealing sensitive information. In other words, we can say that the SCP command is a substitute for the copy (cp) command.

Before we start

For data transfer, the SCP command depends on ssh, so it needs a password or ssh key to authenticate over the remote systems. On the target system and source file, we must have at least write permission and read permission, respectively, to copy files. The colon is how the SCP command distinguishes between remote and local locations. We need to be careful while copying files that have the same location and name on both systems; the SCP command will overwrite those files without any warning. It's suggested to execute the SCP command in a tmux or screen session when sending large files.

Options of the SCP Command

We can add several SCP commands options to any command to speed up and customize the process. Options are included as aspects right after scp.All options have a single character, a short form, and a descriptive, longer equivalent.

Option Description
-1 Protocol 1 will be used.
-2 Protocol 2 will be used.
-4 IPv4 addresses will only be used.
-6 IPv6 addresses will only be used.
-B Executes in the batch mode, deactivating every query for user input.
-b (buffer_size) Describe the buffer size utilized for data transfer. By default, it uses -32768 bytes if not described.
-c cipher choose the cipher for the process of data encryption. SCP applies the default 'AnyStdCipher' if not specified. SCP, by default, utilizes 'AES-128' for file encryption. If we wish to modify the cipher, we need to use the option, i.e., -c, pursued by cipher name.
-C Enable compression. Compression will be activated, and transfer speed will be enhanced while copying with this option. It will automatically activate compression on the source and decompression on the target.
-D (debug_level) Debug level setting (1, 2, 3, or 99)
-d Only when the target directory exists already, it copies the file.
-F file For SSH, describe a replacement configuration file. It uses distinct ssh_config files in situations while copying where we need to utilize district networks to link to Linux systems. We need to allot another SSH configuration file per user in such conditions.
-h Display the command options list.
-i file Indicate the file through which to read the status for public key authentication. Here, i stands for the private key or identify file. Key-based authentication is generally selected in Linux environments. Hence, we can particularly mention the identity file or private key file with the -i option.
-o ssh_option Arranged options in the ssh_configure format to SSH.
-l limit Restrict the bandwidth (define the limit in Kbit/s)
-p This option is used to preserve file permissions, access time, and modifications while copying.
-P port Define the port to which to link. SCP applies port 22 if not defined. We need to particularly mention the number of the port with the -P option if the destination host port number is not the same as the 22 default port number.
-q Execute SCP in quiet mode. This option will turn off the process meter and will not display the transfer process, diagnostic, or warning message of ssh on the terminal window.
-Q Deactivate showing file transfer statistics.
-S program Apply a specified function for encryption connection.
-r This option copies the directories and files recursively. For example, if we wish to copy the whole folder (with contents within the folder) on a destination machine, we need to execute the -r option.
-v Activate verbose mode, which fixes the debug level at 2. It will display the SCP command execution progress step-by-step on the terminal window. It is useful in debugging.
-u Remove the source file when the copy is completed.

Syntax of SCP Command

Before using the SCP command, let's begin with the common syntax.

The syntax of the SCP command takes the below form:

Where,

  • OPTION: It specifies the SCP options like recursive copy, limit, ssh port, ssh configuration, cipher, etc.
  • [[email protected]]SRC_HOST: ]file1: It is the source file.
  • [[email protected]]DEST_HOST: ]file2: It is the destination file.

Local files must be specified using a relative or absolute path, while the names of the remote file must include a host and user specification.

Examples of SCP

Copy directories and files between two different systems using SCP

Copy a local file using the SCP command

We need to execute the below command to copy any file from a local system to a remote system:

Where empty.txt is the file name we wish to copy, remote_username is the remote server user, and 10.10.0.2 is the IP address of the server. The /remote/directory is the directory path we wish to copy our file to. If we do not specify any remote directory, our file will copy to the home directory of the remote user. We will be asked to type our password, and the copy process will begin.

  • Editing the filename through the destination point copies the file with the actual name. We need to enter a new filename if we wish to save our file with a different name.
  • We can enter the port with the -P option if SSH is listening on any port except the default port 22 on the remote host:
  • To copy any directory, the command is much like as coping file. The difference is that we need to enter the -r option for recursive. We will use the option, i.e., -r, to copy any directory from a local system to a remote system:

Copy a remote file to the local system with the scp command

We will use the remote position as a source and the local position as the destination to copy any remote file to the local system. For instance, to copy a file called empty.txt from any remote server along with the 10.10.0.2 IP address, execute the below command:

We will be promoted to enter our user password if we have not set the passwordless SSH login on the remote machine.

Copy any file between two different remote systems with the scp command

When using the scp command, we do not need to login to any of the servers to send files from one remote system to another, unlike rsync. The below command will copy the /empties/empty.txt file from the ;remote1.com ;remote host to the /empties directory on the ;remote2.com ;remote host:

We will be asked to enter our passwords for remote accounts. Our data will be directly transferred between these remote hosts.

We will use the -3 option for routing the traffic from the machine over which the command is permitted:







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA