What is SCP Command? How to use SCP Command in Linux

SCP (Secure Copy Protocol) is a command used to securely copy files and folders between computers, including transferring data between a local computer and a remote server, or between two remote servers.

What is SCP Command? How to use SCP Command in Linux Picture 1What is SCP Command? How to use SCP Command in Linux Picture 1

To securely copy files from local to remote and vice versa on Linux, you will need to use SCP Command. So what is SCP Command and how to use this command in Linux? Let's find out with  TipsMake  in the following article.

What is SCP Command?

SCP stands for Secure Copy Protocol, so we can understand that SCP Command is a command used to securely copy files and folders between computers, including transferring data between the local machine and a remote server, or between two remote servers. This command uses the SSH (Secure Shell) protocol to encrypt data during transmission, ensuring that sensitive information is not exposed.

SCP Command Syntax

The basic syntax of the SCP command is as follows:

scp [options] [[user@]host1:]source_file_or_directory . [[user@]host2:]destination

In there:

● [options]: Additional options to adjust the behavior of the SCP command.

● [[user@]host1:]: Source server address, can be local or remote.

● source_file_or_directory: The file or directory to copy from the source server.

● [[user@]host2:]: The destination server address where the file will be copied to.

● destination: Path to the destination directory on the destination server.

Some commonly used options with the SCP command include:

What is SCP Command? How to use SCP Command in Linux Picture 2What is SCP Command? How to use SCP Command in Linux Picture 2

What is SCP Command?

Some notes when using SCP Command

The SCP command uses SSH to move data, requiring authentication with an SSH key or password. The colon (:) distinguishes between local and remote locations. To copy files, read permissions are required on the source file and write permissions on the destination system.

Note: SCP will overwrite files with the same name without warning, so be careful. For large files, it is recommended to run SCP in a screen or tmux session to avoid losing connections.

How to use SCP Command in Linux

1. How to securely copy a file from local machine to remote machine

To securely copy a file from the local computer to a remote computer, you use a command like the following:

Using the SCP command to copy files from your local machine to a remote server, such as a VPS, makes it easy to deploy web applications. You can quickly push updated files from your development environment to your server.

For example, to copy the scp.zip file to the remote server with the root user, you use the following command:

scp /users/Hostinger/desktop/scp.zip root@162.168.1.2:/write/post

If you don't have an SSH key pair set up, you'll be prompted for a password and shown a progress bar. SCP uses the default SSH port 22, but if you have a custom port, you can add the -P option:

scp -P 2322 /users/Hostinger/desktop/scp.zip root@162.168.1.2:/writing/article

To rename a transferred file, simply specify the new name in the destination folder:

scp /users/Hostinger/desktop/scp.zip root@162.168.1.2:/writing/article/howtoscp.zip

You can also copy directories recursively using the -r command to transfer multiple files at once:

scp -r /users/Hostinger/desktop root@162.168.1.2:/write/post

Additionally, you can copy multiple files by specifying their names:

scp file1.txt file2.txt file3.txt root@

2. How to safely copy a file from a remote computer to a local computer

To transfer data from a remote system to your local machine using SCP, simply reverse the source and destination locations. This allows you to download and work with the project file offline.

For example, use the following command to pull the scp.zip file from the VPS to the local machine:

scp root@162.168.1.2:/writing/articles/SCP.zip /Users/Hostinger/Desktop

When you run this command, you will be prompted for your SSH password. If you have public key authentication set up, you will not need to enter a password.

3. How to copy files between two remote computers

SCP allows file transfers not only to or from the current server, but also between two remote Linux machines. This improves efficiency, especially when you have multiple projects on different machines, as you don't need to log into each system individually.

For example, to transfer the scp.zip file from root@162.168.1.2 to hostinger@162.168.1.1, you would use the following command:

scp root@162.168.1.2:/write/article/scp.zip hostinger@162.168.1.1:/publish

You will need the SSH password for both accounts. By default, SCP routes data directly between remote machines, but you can route it through your local computer by adding the -3 option:

scp -3 root@162.168.1.2:/write/article/scp.zip hostinger@162.168.1.1:/publish

This option is useful when experiencing network errors between systems, although it can slow down the process and does not show a progress bar.

Conclude

SCP Command is not only a powerful tool for transferring data but also ensures the security of your information during the transmission process. Through the above instructions, hopefully you have mastered how to use SCP Command in Linux to perform file copy operations effectively.

4 ★ | 1 Vote