How to use the SCP command in Linux?

Introducing the SCP command in Linux, SCP is a powerful tool for securely copying files between different locations. Whether you need to transfer files locally or over a network, SCP comes to the rescue. In this comprehensive guide, we’ll delve into the syntax, common use cases, and practical examples of the SCP command. By the end, you’ll be equipped with the knowledge to effortlessly manage file transfers in the Linux environment.

Keep in mind that the SCP command uses SSH to transfer files, so the remote machine must have an SSH server running, and you must have permission to log in to the remote machine using SSH.

What is SCP and how does it work?

SCP, short for Secure Copy Protocol, is a command-line tool in Linux used for secure and efficient file transfer between different locations. It operates over SSH (Secure Shell) and provides a secure channel for data transmission. SCP ensures the confidentiality and integrity of files during the transfer process, making it ideal for sensitive data.

Key features and advantages of using SCP for file transfer.

  1. Security: SCP uses SSH encryption to protect data during transit, safeguarding it from unauthorized access or tampering.
  2. Simplicity: SCP has a straightforward syntax, making it easy to use for both novice and experienced users. Its commands are similar to the familiar ‘cp’ command, simplifying the learning curve.
  3. Cross-platform compatibility: SCP is widely supported across various operating systems, including Linux, macOS, and Windows with the help of SSH clients like PuTTY or WinSCP.
  4. Remote file transfer: SCP allows you to copy files between local and remote machines seamlessly, regardless of their locations. It enables efficient management of files stored on different servers.
  5. Directory transfer: With the ‘-r’ flag, SCP can recursively copy entire directories and their contents, providing a convenient way to transfer multiple files at once.

Here’s the basic syntax of the SCP command:

scp [options] [source] [destination]

source is the file or directory you want to copy.
destination is the location where you want to copy the file or directory.

Common use cases of scp command.

There are various use cases of the SCP command which mainly include transferring files to a remote server, pulling files from the remote server etc.

Transferring Files and directories to a Remote Server

The syntax for transferring files and directory to a remote server

scp [options] [source_file] [username@remote_host:destination_directory]

[options] with any desired flags or options (e.g., for custom SSH port or verbose output).
[source_file] with the path and filename of the file you want to copy.
[username] with your username on the remote server.
[remote_host] with the IP address or hostname of the remote server.
[destination_directory] with the path to the directory on the remote server where you want to place the file.

Scp command to copy a file named backup.tar.gz from the present working directory to another servers /home directory. The server destination server IP address is 192.168.1.100 and with default ssh port 22.

root@ubundu:~# scp backup.tar.gz [email protected]:/home

The server destination server with custom SSH port 11208.

root@ubundu:~# scp -P11208 backup.tar.gz [email protected]:/home

Let’s say you want to copy the entire contents of the documents directory from your local server to the remote server having the IP address 192.923.223.You can use the following command.
Modify the directory name and path as you wish in this command

root@ubundu:~# scp -r documents [email protected]:/home

Scp command to compress the files on the go. -C flag is used as it compresses the files please note that it will compress the files over the network it will return to their original size after reaching the destination.

root@ubundu:~#scp -C documents [email protected]:/home

The scp command used to view the details of the file transfer .-p flag is used to print the estimated time and the connection speed.-V command is used to print the debug information.
It can help to debug connection, authentication, and configuration problems

root@ubundu:~# scp -pv documents [email protected]:/home

Copying Files from the Remote Server

We can also pull files from a remote server to our local server using the SCP command

scp [options] [username@remote_host:source_file] [destination_directory]

Let’s say you want to copy the backup.tar.gz file from the home directory of a remote server having IP address 192.168.1.100 to the /home/download directory of your local server/system you can use the following command

root@ubundu:~# scp [email protected]:/home/backup.tar.gz /home/download

You can also transfer the entire directory from the remote server to the local server using the -r flag.

SCP to copy files directly between two remote servers.

Unlike rsync in SCP, you don’t need to log into one of the servers to transfer files between them, If you have both the root password of the servers you can initiate the transfer of files and folders between 2 servers
The following command will copy the backup file from remote host1 to the destination directory downloads of host2.

root@ubundu:~# scp [email protected]: backup.tar.gz [email protected]:/downloads
You will be prompted to enter the password of both servers

Summary:
While working with Linux servers we often need to transfer files and folders between systems.SCP is the most widely used command in order to transfer files and folders between Linux servers. We have covered the basic use cases of the SCP command with examples.

Similar blogs:

How to use the Rsync command in Linux.

Leave a Reply

Your email address will not be published. Required fields are marked *