How to Securely Copy Files in Linux | scp Command
Secure file transfer is a crucial part of Linux systems administration. Whether moving sensitive files between local machines or transferring data between servers, or you need to move backup files to a remote server, fetch logs from a hosted machine, or sync directories across multiple systems, scp
command is useful,
.
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)
SCP (Secure Copy Protocol) allows users to securely transfer files between systems. Because it uses SSH, all transferred data is encrypted, making it a safe choice for transmitting sensitive files.
It's a command-line tool that allows transferring files securely between hosts over a network built on the SSH proabout how to securely copy files in Linux using the SCP command. We'll explaitocol 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.
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 byuser@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 byuser@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
.webp)
To Verify: Use `ls` command in the location we copied file.

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

To verify: use dir (in windows cmd)

-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/

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

-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/

-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/

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

-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/

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

SCP vs SFTP vs Rsync
Whether it’s quick file transfers, synchronizing folders, or browsing remote directories, tools like SCP, SFTP, and Rsync each have their strengths.
Feature/Aspect | SCP (Secure Copy) | SFTP (SSH File Transfer Protocol) | Rsync (Remote Sync) |
---|---|---|---|
Use Case | Best for quick and secure one-time file transfers | Ideal for browsing files interactively on remote servers | Perfect for syncing directories and handling large datasets |
Speed | Fast, especially for single files | Slower than SCP due to session management overhead | Very fast with large or repeated transfers (uses delta copy) |
Encryption | Yes – uses SSH | Yes – uses SSH | Yes – uses SSH or can work over RSH |
Supports Resume | Not supported | Supported | Supported |
Interactive File Browsing | Not available | Supported | Not available |
Directory Synchronization | Not supported | Limited support | Fully supported |
Compression Support | Available with -C flag | Not built-in | Available with -z option |
Availability | Available on all Linux distributions | Available on all Linux distributions | Pre-installed on most Linux systems |
Command Syntax | scp file user@remote:/path | sftp user@remote then use put or get commands | rsync -avz file user@remote:/path |
Ease of Use | Simple for one-time tasks | Easier for interactive use | More complex but highly powerful |
Real-Time Sync | Not supported | Not supported | Supported |
Resource Usage | Moderate | Moderate | Low, with efficient data usage |
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:
- Ensure to use a strong and unique SSH key to authenticate file transfers.
- Always ensure that you're transferring files to a trusted remote hosts by checking its fingerprints.
- Ensure that you're using a non-default SSH port (if possible) to prevent from any attacks.
- 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:
transferring
2. 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.
3. 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.
Conclusion
scp is possibly the easiest yet most effective secure file transfer tool for Linux. It is constructed atop SSH and encrypts all credentials and data, making it safer from cyber attacks and interception of files. Coping files from a laptop to a distant server, creating backups of project directories, or handling files between cloud instances, SCP efficiently and securely gets the task completed.
Unlike graphical file transfer programs, scp provides you complete control from the command line so that you can automate operations, tailor transfer behavior, and safely manage files without third-party utilities. Its straightforward syntax, availability system-wide, and support for SSH keys make it perfect for everyday file handling.