The Wayback Machine - https://web.archive.org/web/20241117193733/https://www.geeksforgeeks.org/scp-command-in-linux-with-examples/
Open In App

How to Securely Copy Files in Linux | scp Command

Last Updated : 12 Jul, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

scp (secure copy) command in Linux system is used to copy file(s) between servers in a secure way. The SCP command or secure copy allows the secure transferring of files between the local host and the remote host or between two remote hosts. It uses the same authentication and security as it is used in the Secure Shell (SSH) protocol. SCP is known for its simplicity, security, and pre-installed availability.

Syntax of scp Command in Linux

The Basc Syntax of scp command in Linux is as follows:

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

Options in scp command in Linux

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.

Practical Example to Securly Copy a File in Linux

How to Securely copy a file from a local machine to a remote machine in Linux

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 a file from remote machine to our local machine

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

Conclusion 

In this article we discussed the scp command in Linux which is a secure and straightforward way to copy files between computers over a network. It uses the Secure Shell (SSH) protocol for safety and is known for its simplicity. The article explained the basic syntax, common options, and provided examples for copying files, preserving metadata, and transferring entire directories securely. It also answered common questions, such as how to copy directories and files in reverse, and shared tips for improving security during transfers. Overall, scp is a handy tool for managing file transfers in a secure and efficient manner on Linux systems.



Previous Article
Next Article

Similar Reads

Tools to Securely Delete Files from Linux
Every time you delete a file from your Linux system using the shift + delete or rm command, it doesn't actually permanently and securely delete the file from the hard disk. When you delete a file with the rm command, the file system just frees up the appropriate inode but the contents of the old file are still in that space until it is overwritten
3 min read
Ways to Permanently and Securely Delete ‘Files and Directories’ in Linux
Linux is an open-source operating system that is loved by tech geeks. It is a versatile operating system that can be used on thousands of different hardware configurations. The most interesting feature of Linux is its command line interface. This feature allows you to perform multiple operations within the operating system with just a few commands.
7 min read
How to Copy Files and Directories in Linux | cp Command
In the world of Linux computers, a common and important job is copying files. The key tool for this task is the "cp" command. In this simple guide, we'll explore how to copy a file in Linux, looking at the basic steps, different choices you can make, and giving clear examples. We'll also take a peek behind the scenes to understand how the "cp" comm
8 min read
How to Download a File from a Server with SSH / SCP?
Secure Copy Protocol (SCP) is a secure file transfer protocol that allows you to transfer files between computers over a secure connection. It is based on the Secure Shell (SSH) protocol and is commonly used to transfer files between servers and local computers. One of the main benefits of using SCP is that it encrypts the data being transferred, e
3 min read
How To Solve Scp Permission Denied Error
SCP (Secure Copy Protocol) is a convenient way to transfer files securely between a local host and a remote server. However, encountering a "Permission Denied" error during an SCP transfer can be frustrating, especially when you have the necessary permissions. In this article, we'll explore the various causes of SCP permission denied errors and pro
4 min read
How to Extract and Copy Files from ISO Image in Linux?
The term ISO was derived from the ISO 9660 file system, which is commonly used by optical media. An ISO image is a full copy of everything contained on a physical optical disc, such as a CD, DVD, or Blu-ray disc, including the file system. There is no compression and they are a sector-by-sector copy of the disc. ISO images are designed to allow you
3 min read
How To Copy Command Output To Linux Clipboard Directly
To copy commands outputs directly to Linux Clipboard we will be using a Program called xclip. xclip is a program that allows us to clip-> copy/crop ->cut and external reference or a block to a specific area. xclip reads text from standard inputs or files and make it available to other application for passing an X section. It reads from all fi
4 min read
Linux - Installing locate Command to Find Files
In this article, we will see how to install locate command to find files in Linux. locate is a command-line interface tool used for finding files by name in Linux systems. This works more efficiently than other commands. It uses one or more databases by updatedb. To check whether locate utility is already installed in your system. Open up your term
2 min read
How to Compress Files in Linux | Tar Command
File compression is a fundamental task in managing and transferring data efficiently on a Linux system. The Tar command, short for Tape Archive, is a powerful tool that allows users to create compressed and archived files. In this comprehensive guide, we will explore the various options and examples of using the Tar command to compress files on a L
11 min read
How to Compare Files Line by Line in Linux | diff Command
In the world of Linux, managing and comparing files is a common task for system administrators and developers alike. The ability to compare files line by line is crucial for identifying differences, debugging code, and ensuring the integrity of data. One powerful tool that facilitates this process is the diff command. In this article, we will explo
9 min read
How to sort lines in text files in Linux | sort Command
SORT command is used to sort a file, arranging the records in a particular order. By default, the sort command sorts file assuming the contents are ASCII. Using options in the sort command can also be used to sort numerically.  SORT command sorts the contents of a text file, line by line.sort is a standard command-line program that prints the lines
7 min read
How to Compress and Extract Files Using the tar Command on Linux
Tar archives are special files that bundle multiple files and directories into a single file, making it easier to store, share, and manage. While tar archives themselves are not compressed, they can be combined with various compression utilities to reduce their size. The tar command is highly versatile, allowing users to create, modify, and extract
3 min read
How to List Open Files in Linux | lsof Command
In the world of Linux, understanding and managing open files is crucial for system administrators and users alike. The Linux operating system provides a powerful utility called lsof (List Open Files) that allows users to gain insights into the files currently open on their system. In this article, we will delve into the intricacies of the lsof comm
7 min read
How to Display Command History in Linux | history Command
The command-line interface in Linux provides powerful tools for users, and mastering command history is essential for efficient navigation and retrieval of previously executed commands. The history command is a valuable utility that allows users to view and search through their command history. In this comprehensive guide, we will explore the vario
4 min read
Ccat – Colorize Cat Command Output command in Linux with Examples
ccat is a Linux and macOS cat command that improved its precursor: the syntax highlighted file contents can be displayed with its help. Therefore, in the case of source code and configuration files, it is extremely helpful. It can highlight over 10 languages, like JavaScript, Java, Ruby, Python, Go, C, and JSON. Syntaxccat [OPTIONS] [FILE...]Basic
3 min read
Exodus - Copy Linux Binaries From One Linux System
Exodus is an easy-to-implement program, that is used for bundling dependencies, and also helps to effectively copy Linux ELF binaries from one to another machine securely, and this is very handy when you don't have root access, also if there are issues in the availability of the packages in the Linux distribution, Commonly Server oriented distribut
3 min read
How to copy a file's content from Linux terminal?
This article shows the alternative method to copy the content of the file onto the clipboard, via the Linux terminal. In OSX, the commands pbcopy and pbpaste are available by default. Thus, to copy a file onto the clipboard via OSX terminal, type: pbcopy < 'path of the file' Since, in Ubuntu, pbcopy and pbpaste commands are not availab
2 min read
How to Copy a File to Multiple Directories in Linux
In this article, we will discuss how to copy a file to multiple directories in Linux. xargs, find, tee, shell loop, and parallel are the commands that can help you to copy a File to Multiple Directories. Suppose we have a file with the name "xyz.txt" and we want to copy it into 3 directories with the name dir1, dir2, and dir3 then we use the follow
5 min read
Copy and Create Destination Directory if it Does Not Exist in Linux
A directory is a file system location for storing and organizing files. A Linux system has many directories, and users can create their own directories. Copying and creating a directory are useful operations for Linux users. To create a new directory, use the command: mkdir new_directory. The 'm' stands for 'modify.' The name of the new directory m
6 min read
How to Copy a Directory in Linux
There is this famous quote for Linux by Jamie Zawinski that goes like this "Linux is only free if your time has no value." Certainly, that's true as people start using Linux they have to learn more about it, and when encountering it we have to solve it by ourselves, and that way we learn more about Linux. In this article, we are going to learn how
5 min read
Encrypting and Decrypting the Files Using GnuPG in Linux
GnuPG is an encryption module that uses OpenPGP at its core. PGP stands for Pretty Good Privacy. It is an encryption program that provides authentication and cryptographic privacy for data communication. In the age where data is the new oil, the modern age thieves won't intrude in through doors, windows or roofs, but instead, via wires and electric
3 min read
Encrypting Files Using vim editor in Linux
Vim, the popular text editor comes with a built-in feature to encrypt files with a password. Vim uses algorithms like Blowfish to encrypt files. It is faster and convenient than some other utilities available to do so. To get started, all you need is a system with a full version of vim installed. Some Linux distributions are shipped with a minimal
3 min read
How to Recover Deleted Files Using Foremost in Linux?
Foremost is a digital forensic application that is used to recover lost or deleted files. Foremost can recover the files for hard disk, memory card, pen drive, and another mode of memory devices easily. It can also work on the image files that are being generated by any other Application. It is a free command-line tool that is pre-installed in Kali
3 min read
Monitor Linux Daemon, Filesystem, CPU, Network and Files Using Monit
Monit is a utility tool for monitoring & managing processes, files, directories, and filesystems on a Unix System. A superpower or a super ability to start a process if it's not running, restart a process if it isn't responding, and stop a process if it uses high resources, that's what Monit(or) does in layman's terms. Monitoring is basically t
3 min read
How to Find and Remove Duplicate Files on Linux?
Most of us have a habit of downloading many types of stuff (songs, files, etc) from the internet and that is why we may often find we have downloaded the same mp3 files, PDF files, and other extensions. Your disk spaces are unnecessarily wasted by Duplicate files and if you want the same files on a different location you can always set up a soft li
4 min read
Finding Files With SUID and SGID Permissions in Linux
SUID(Set-user Identification) and SGID(Set-group identification) are two special permissions that can be set on executable files, and These permissions allow the file being executed to be executed with the privileges of the owner or the group. SUID: It is special file permission for executable files. This enables other users to run the file with th
3 min read
How to View Configuration Files Without Comments in Linux?
We came across an extremely long configuration file with hundreds of thousands of comments, and all we wanted to do was filter out the important content or settings. As a result, we'll look at various methods for viewing configuration files in Linux without comments. The grep command can be used to do this. The following command will delete lines b
2 min read
Renaming All Files and Directory Names to Lowercase in Linux
Here we will see how to rename all files and directory names to lowercase in Linux. We can easily do it with some basic commands in Linux. The below command can be used to view the contents of your directory. $ find [directory_name] -depth Here 'priyanshu' directory which has some sub-files/folders, has all files starting from uppercase letters: $
1 min read
How to Find Recently Modified Files in Linux?
Here we are going to see how to find recent or today’s modified files in Linux. Locating files with a particular name is one of the problems Linux user's faces, it would be easier when you actually know the filename. let's assume that you have created a file in your home folder and you have forgotten the name of the file which also contains some mo
2 min read
How to Find Files Modified in Last N Number of Days in Linux?
Sometimes, we want to find the files which we created or modified in the last N number of days. Sorting the files on the basis of date helps in this case, but that's the traditional way of doing the task and is not efficient. This article is all about searching such files using the find command. Method 1: Using -mtime(modification time) attribute o
3 min read
three90RightbarBannerImg