How to Transfer Files Using SCP Command in Linux

How to Transfer Files Using SCP Command in Linux

7 mins read886 Views Comment
Updated on Jun 21, 2023 17:27 IST

Learn how to transfer files between Linux machines using the SCP command. Our step-by-step guide will teach you how to securely copy files using SSH, set permissions, and troubleshoot common errors. Start transferring files with confidence today!

2023_04_Feature-Image-Templates-8.jpg

The Secure Copy Protocol (SCP) in Linux is a network protocol that allows the secure transfer of files between a local host and a remote host or between two remote hosts. It uses SSH (Secure Shell) protocol for authentication and data encryption, providing a secure and efficient way to transfer files. SCP is a command-line tool widely used in Linux and Unix systems for file transfer over a network.

Table of Content

What is SCP Command?

The SCP command in Linux is a command-line tool used for secure file transfer between a local host and a remote host or between two remote hosts. It uses the SSH protocol for authentication and encryption and allows users to copy files and directories from one host to another over a network.

Syntax of SCP Command

The SCP command syntax includes the source and destination paths and the user credentials and host information for the remote host. The SCP command is available in most Linux and Unix distributions and is a reliable and secure method for transferring files over a network.

A generic form of the SCP command is given below:

 
scp [OPTIONS] [user@]SOURCE_HOST:]file_1 [user@]DESTINATION_HOST:]file_2
Copy code

Here,

  • scp:- It initializes the command and establishes a secure shell to ensure secure connectivity.
  • OPTIONS:- It is responsible for setting up the necessary permissions depending upon the requirement, such as:
    • P:- Used to specify the port for connecting with the remote server.
    • p:- Used to store timestamps of different events during the transfer.
    • r:- Recursively copy all directories.
    • q:- Used to silently copy files.
    • C:- Used for data compression during the file transfer.
  •  SOURCE_HOST:- Server or client that hosts the file to be transferred.
  • DESTINATION_HOST:- Server or client where the file is to be copied.

Prerequisite of Using SCP Commands

Before you make use of the SCP commands, make sure you have the following things in place:

  1. SSH needs to be installed on both the host and the destination server or machine.
  2. Root access to both the client and server machines.
Learn Basic Linux Commands (With Syntax)
Learn Basic Linux Commands (With Syntax)
"Linux commands" refer to the instructions or inputs that users provide to perform specific operations in the Linux operating system. Linux, being a command-line-oriented operating system, relies heavily on these...read more
Difference between Linux and Unix
Difference between Linux and Unix
Linux and Unix are operating systems similar to Windows, Ubuntu, and Apple macOS. Operating Systems contain and manage all the programs and applications that run on the desktop or mobile....read more
Difference Between Linux and Windows
Difference Between Linux and Windows
The main difference between Linux and Windows is that Linux is an open-source operating system that allows its users access to the source code. In contrast, a windows operating system...read more
Recommended online courses

Best-suited Linux courses for you

Learn Linux with these high-rated online courses

– / –
200 hours
2 K
20 hours
– / –
3 months
– / –
6 months
– / –
3 months
Free
8 hours
10 K
60 hours
– / –
3 months

How does SCP Command Work?

The SCP (Secure Copy) command in Linux works by establishing a secure SSH (Secure Shell) connection between the local and remote hosts and transferring files over the encrypted connection.

When you run the SCP command, it authenticates your credentials (username and password) on the remote host using the SSH protocol. It then creates a secure tunnel over the network and copies the files to or from the remote host.

It also supports several advanced features, such as compression and remote file copy, making it a popular choice for file transfer between local and remote hosts.

Now let’s take a deep dive into the SCP commands.

Copy Local File(s) to a Remote System

To copy a file from a local host to a remote server in Linux using the SCP command, follow the below steps:

  • Open the terminal on the local host.
  • Use the scp command with the following syntax:

scp /path/to/local/file username@remote_host:/path/to/remote/directory

Here, replace “/path/to/local/file” with the path of the file you want to copy from the local host, “username” with the username of the remote host, “remote_host” with the IP address or hostname of the remote server, and “/path/to/remote/directory” with the path of the directory where you want to copy the file on the remote server.

This will copy the local files to the remote server.

Copy Remote File(s) to a Local System

To copy a file from a remote system to a local system using SCP command in Linux, follow the below steps:

  • Open the terminal on the local system.
  • Use the scp command with the following syntax:

scp username@remote_host:/path/to/remote/file /path/to/local/directory

Here, replace “username” with the username of the remote host, “remote_host” with the IP address or hostname of the remote server, “/path/to/remote/file” with the path of the file you want to copy from the remote system, and “/path/to/local/directory” with the path of the directory where you want to copy the file on the local system.

This will copy files from a remote server to a local system.

Copy a File Between Two Remote Systems 

To copy a file between two remote systems using the scp command in Linux, follow the below steps:

  • Open the terminal on your local system.
  • Use the scp command with the following syntax:

scp username1@remote_host1:/path/to/remote/file username2@remote_host2:/path/to/remote/directory

Here replace “username1” with the username of the first remote host, “remote_host1” with the IP address or hostname of the first remote server, “/path/to/remote/file” with the path of the file you want to copy from the first remote system, “username2” with the username of the second remote host, “remote_host2” with the IP address or hostname of the second remote server, and “/path/to/remote/directory” with the path of the directory where you want to copy the file on the second remote system.

This will copy files between two remote hosts.

Note: Make sure that both remote systems are connected to the same network and the SSH service is running on both remote servers.

Compress Files while Copying using SCP Command

To use SCP compression while copying files, you can use the “-C” option with the SCP command. 

The “-C” option tells SCP to compress the data during transfer, which can improve transfer speeds and reduce network bandwidth usage. 

Here is the syntax to use SCP compression:

 
scp -C <source_file> <destination>
Copy code

Here, replace <source_file> with the file you want to copy and <destination> with the location where you want to copy the file.

Limitation of SCP Command

Although the SCP command is a powerful and secure tool for file transfer in Linux, it has some limitations, including:

  1. Limited functionality: SCP has limited functionality compared to other file transfer protocols, such as FTP or SFTP. It does not have features like resume, directory synchronization, or remote file editing.
  2. Poor performance on high-latency networks: SCP does not perform well on high-latency networks because it uses a single connection for file transfer. This can lead to slow transfer speeds and timeouts.
  3. No file browsing capability: SCP does not allow the browsing of files on the remote system. Users need to know the exact path of the file they want to transfer.
  4. No progress indicator: SCP does not provide a progress indicator during file transfer, making it difficult to estimate transfer time.
  5. No file integrity checks: SCP does not perform file integrity checks during transfer, which can lead to file corruption if errors occur.

Conclusion

The SCP command securely transfers files between computers on the same network or over the Internet, making it an invaluable tool for secure data transfer. It is a secure alternative to FTP and works well for transferring large or confidential files.
Users need a basic understanding of command line syntax and security protocols for authentication to use SCP. However, once understood, SCP is an excellent tool for securely exchanging files between computers. By following the steps outlined in this article, users can use SCP to transfer files securely between systems.

FAQs

What is SCP command in Linux, and how is it used to transfer files between machines?

SCP (Secure Copy) is a command-line tool in Linux used for secure file transfer between machines. It uses SSH (Secure Shell) protocol to transfer files and is similar to the CP (Copy) command in Linux.

Is SCP a secure method of transferring files in Linux, and how does it compare to other file transfer protocols?

Yes, SCP is a secure method of transferring files in Linux, as it encrypts the files during transfer. It is more secure than other protocols like FTP, which transfer files in plain text.

Can SCP be used to transfer files between a Linux machine and a Windows machine?

Yes, SCP can be used to transfer files between a Linux machine and a Windows machine, but a Windows machine requires an SCP client to receive the files. Programs like WinSCP and PuTTY can be used as SCP clients.

What are some common errors that can occur when using SCP, and how can I troubleshoot them?

Common errors that can occur when using SCP include incorrect file paths, file permission errors, and authentication errors. To troubleshoot these errors, check the file paths, ensure that you have the necessary permissions to access the files, and verify that your authentication credentials are correct.

How do I use SCP to transfer files to a remote server without entering my password each time?

You can use SCP to transfer files to a remote server without entering your password each time by setting up SSH keys. This involves generating a public and private key on your local machine and adding the public key to the remote server's authorized keys file. This will allow you to authenticate without entering your password.

About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio