A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/linux-unix/scp-command-in-linux-with-examples/ below:

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:

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:

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,

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

Syntax:

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

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

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,

For Example: If we have

Syntax:

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

To verify: use dir (in windows cmd)

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

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

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

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

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

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

new directory successfully copied. 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:

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.



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4