Open In App

How to Securely Copy Files in Linux | scp Command

Last Updated : 24 Feb, 2025
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

Secure file transfer is a crucial part of Linux systems administration. Whether moving sensitive files between local machines or transferring data between servers, SCP (Secure Copy Protocol) provides a fast, secure, and efficient way to copy files over a network. By utilizing SSH (Secure Shell), SCP ensures that your file transfers are encrypted and protected against eavesdropping.

In this article, we’ll cover everything you need to know about how to securely copy files in Linux using the SCP command. We’ll explain the command syntax, the most commonly used options, practical examples, and troubleshooting tips to ensure you can use SCP to its full potential.

What is the Secure Copy Protocol (SCP)

It’s a command-line tool that allows transfer files securely between hosts over a network built on SSH protocol that encrypts data during the transfer to protect it from potential interception. Whether copying files from your local machine to a remote server or transferring files from one Linux machine to another, SCP ensures that the process is safe and fast.

Syntax of scp Command in Linux

The SCP command is used to copy files between local and remote systems securely. It has a simple syntax:

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

In this syntax:

  • options: These are various options that modify the behavior of the SCP command, such as -i for specifying an identity file, -l for limiting bandwidth, -o for specifying SSH options, -P for specifying a custom SSH port, and -S for specifying a program to use for the encrypted connection.
  • [[user@]host1:]source_file_or_directory: This represents the source file or directory. It can be local or on a remote machine specified by user@host1:.
  • ...: This indicates that you can specify multiple source files or directories.
  • [[user@]host2:]destination: This is the destination where the files or directories will be copied. It can be local or on a remote machine specified by user@host2:.

Example:

To copy a file named file.tx from your local machine to a remote server:

scp file.txt username@192.168.1.2:/home/username/

In this example:

  • file.txt is the local file
  • username@192.168.1.2 is the remote server and user
  • /home/username/ is the destination directory on the remote server.

Commonly Used SCP Commands in Linux

These are some of the most commonly used Options in scp Command in Linux:

options  Description
-P port: Specifies the port to connect on the remote host.
-p  Preserves modification times, access times, and modes from the original file.
-q  Disables the progress meter.
-r  Recursively copy entire directories.
-s  Name of program to use for the encrypted connection. The program must understand ssh(1) options.

Example:

To copy a directory recursively with SCP:

scp -r /path/to/local/directory username@192.168.1.2:/home/username/

How to Securely Copy Files from Local to Remote Machine

Syntax:

scp [file_name]  remoteuser@remotehost:/remote/directory

Here,

  • file_name = The name of the file that needs to be copied.
  • remoteuser =The username of the remote host.
  • remotehost = The IP address or hostname of the remote host.
  • /remote/directory = The directory where the file should be copied on the remote machine.

For example: If we want to copy a file name “test.txt” from local system to a 

  • “remoteuser” = “Jayesh” 
  • “remotehost” = “10.143.90.2”
  • “/remote/directory” = “/home/jayesh”

Syntax:

scp test.txt jayesh@10.143.90.2:/home/jayesh
copied file from local system to remote system

copied file from local system to remote system

To Verify: Use `ls` command in the location we copied file.

File that we have copied

File that we have copied

How to Securely Copy Files From Remote Machine to Local Machine

To copy a file from a remote machine to your local system, you just reverse the source and destination, let’s check out the syntax below:

Syntax:

scp user@remotehost:/home/user/file_name 

Here,

  • “user” = username of remote system.
  • “remotehost” = IP address (or hostname) of remote system.
  • “/home/user/file_name” = path of file that has to be copied.
  • “.” = this means that we are copying that file in current location in local system.

For Example: If we have

  • “user” = jayesh
  • “remotehost” = 10.143.90.2
  • “home/user/file_name” = home/jayesh/test1.txt

Syntax:

scp jayesh@10.143.90.2:/home/jayesh/test1.txt 
copied file from remote system to local system using scp

copied file from remote system to local system using scp

To verify: use dir (in windows cmd)

test1.txt successfully copied

test1.txt successfully copied

-P Option in scp Command

It is used to Securely Copy File to a Remote Machine on a Non-Standard SSH Port and specify the port to connect on the remote host. It is useful when our SSH server is listening on a non-standard port.

Syntax: 

scp -P port source_file user@hostname:destination_file

For Example:

If we want to copy a file “test2.txt” from local machine to a remote machine with IP address “10.143.90.2” on port 2222 , user = “jayesh” and location = “/home/jayesh/”.
By default, the scp uses ssh over port 22 for transferring the files. Changing the port might be necessary if the designated port 22 is not open on the remote host.

Syntax:

scp -P 2222 test2.txt jayesh@10.143.90.2:/home/jayesh/
copying file from local system to remote using -P option in scp

copying file from local system to remote using -P option in scp

To Verify: Use `ls` command in remote system in the location we have copied the file.

test2.txt successfully copied

test2.txt successfully copied

-p Option in scp Command

This option is used when we want the original metadata of the file that has been transferred. Basically, it preserves modification time, access time, and modes from the original file.

Syntax: 

scp -p source_file user@hostname:destination_file

For Example: If we want to copy a file “test3.txt” from local machine to a remote machine with IP address “10.143.90.2”, user = “jayesh” and location = “/home/jayesh/”

Syntax:

scp -p test3.txt jayesh@10.143.90.2:/home/jayesh/
copying file from local system to remote using -p option in scp

copying file from local system to remote using -p option in scp

-q Option in scp Command

It Securely Copy File with Quiet Mode – Disabling Progress Meter .This option hides the progress of the file transfer on the terminal.

Syntax: 

scp -q source_file user@hostname:destination_file

For Example: If we want to copy a file “test4.txt” from local machine to a remote machine with IP address “10.143.90.2”, user = “jayesh” and location = “/home/jayesh/”

Syntax: 

scp -q test4.txt jayesh@10.143.90.2:/home/jayesh/
As we can see there is no progress shown

As we can see there is no progress shown 

To Verify: Use `ls` command in remote system in the location we have copied the file.

test4.txt successfully copied

test4.txt successfully copied

-r Option in scp Command

This option is used when we want to copy an entire directory and its contents. Which basically means copying entire directory recursively.

Syntax: 

scp -r Directory_name user@hostname:destination_file

For Example: If we want to copy a Directory content name “new” from local machine to a remote machine with IP address “10.143.90.2”, user = “jayesh” and location = “/home/jayesh/new1/”

Syntax:

scp -r new jayesh@10.143.90.2:/home/jayesh/new1/
copying entire directory and its file recursively using `-r` in scp

copying entire directory and its file recursively using `-r` in scp

To Verify: Use `ls` command in remote system in the location we have copied the file.

new directory successfully copied.

new directory successfully copied.

How to Improve the Security of SCP File Transfer

You must follow these practices to ensure that you’re performing a secure file transfer using the SCP:

1. Ensure to use a strong and unique SSH key to authenticate file transfers.

2. Always ensure that you’re transferring files to a trusted remote hosts by checking its fingerprints.

3. Ensure that you’re using a non-default SSH port (if possible) to prevent from any attacks.

4. Cross verify that you’re only transferring files to directories with limited access.

Troubleshooting While Copying Files in Linux: SCP Command

While performing this action, there are certain challenges that you might face, let’s address them for better clarity:

1. Permission Denied Error

If such issue persists, ensure that all the permissions to read/write to both source and destination (or directories) are coreect. Here’s how you can check it:

ls -l /path/to/remote/directory

2. Connection Timeout

Ensure that the remote server is onlinr and reachable and in case of custom SSH port, always make sure that you’re using the correct port. You may verify it by using the -P option.

3. Issue While Transferring Files

Check if the source file exists and is accessible, also ensure to have sufficient storage disk space on the remote server so that the files can be stored.

Conclusion

The SCP command is one of the most essential tool for anyone who like working on the Linux Systems. It provides a seamless method of copying files between local and remote machines. By folloiwng the above steps and methods, you can easily transfer files in a secured way, and troubleshoot common SCP issues with ease.

Ensure to follow the safe practice for secrity, use SSH keys for authentication. and ensure that your system’s networking settings are onfigured properly for seamless file transferring.

How to Securely Copy Files in Linux | scp command in Linux – FAQs

What is the scp -r command in Linux?

The scp -r command in Linux is used to copy an entire directory and its contents. This option enables recursive copying of files within the specified directory. Here’s an example:

scp -r username@source_machine:/path/to/source/directory  username@destination_machine:/path/to/destination/

This command recursively copies all files and subdirectories from the source directory to the specified destination.

What is scp command in Linux.?

The scp command in Linux is used to copy files or directories between hosts over a network, and it utilizes the SSH (Secure Shell) protocol for secure file transfer. Here’s an example of copying a file from a local machine to a remote server:

scp /path/to/local/file  username@remote_machine:/path/to/destination/

This command securely transfers the specified file to the remote machine.

What is the reverse scp command in Linux?

There is no reverse scp command in Linux. The scp command is inherently designed for copying files from a source (local or remote) to a destination (local or remote). There is no specific command for copying files in the reverse direction. If you want to copy a file from a remote location to your local system, you still use the regular scp command.

For example:

scp username@remote_machine:/path/to/source/file /path/to/local/destination/

This command copies the specified file from the remote machine to the local destination.

How do I use the scp command to copy files between two Linux machines securely?

To copy files securely between Linux machines, use the following syntax:

scp [options]  [destination]

For example:

scp username@source_machine:/path/to/source/file  username@destination_machine:/path/to/destination/

What are the common options used with scp for secure file transfer?

Some common options used with scp include:

  • -r: Recursively copy entire directories.
  • -P port: Specify the port to connect to on the remote host.
  • -i identity_file: Specify the private key file for authentication.
  • -v: Enable verbose mode for detailed output during the transfer.

How can I copy files from a local machine to a remote server using scp?

To copy files from a local machine to a remote server, use the following syntax:

scp /path/to/local/file username@remote_machine:/path/to/destination/

How do I improve the security of scp file transfers?

To enhance security during scp transfers, consider:

  • Using SSH key authentication for passwordless logins.
  • Specifying a non-default port for added security (-P port option).
  • Restricting access by setting appropriate file permissions on the source and destination directories.

How to specify the SSH port in Linux for scp?

To specify the SSH port in Linux for scp, you use the -P option followed by the port number. The -P flag allows you to define a custom port for the SSH connection, which is useful when the SSH server on the remote machine operates on a non-default port.

Here is an example:

scp -P <port_number> your_file.txt user@remote_server:/path/to/destination/

In this command:

  • Replace `<port_number>` with the actual port number you want to use for the SSH connection.
  • `your_file.txt` is the file you want to copy.
  • `user` is the username on the remote server.
  • `remote_server` is the address or hostname of the remote server.
  • `/path/to/destination/` is the destination directory on the remote server where you want to copy the file.

By using the `-P` option followed by the desired port number, you ensure that `scp` connects to the remote server using the specified port for the secure copy operation.



Next Article

Similar Reads

three90RightbarBannerImg