How to use the SCP command in Linux

#

How to use the SCP command in Linux

  • 0 comments Nick Rose
Welcome to our Knowledge Base
Table of Contents
< All Topics
Print

When copying files locally, the cp command does the job quite well. But how do you go about copying files from your local system to a remote host? This is where you leverage the SCP command. Short for Secure Copy, the SCP command copies files and directories from a local system to a remote host over the SSH protocol. It provides the same authentication and encryption as SSH to ensure the privacy of your data transfer and keep snoopers at bay.

The SCP command allows you to copy files or directories from your local PC to a remote server, or vice-versa – copy files and directories from the remote server to your local PC. In this guide, you will learn how to use the SCP command to securely transfer files between two hosts.

Basic Syntax

The SCP command takes the syntax shown.

$ scp [option] /path/to/local/file user@remote_server-IP:/path/to/target/dir/

Command options

The following options are available for use with the SCP command:

-C – Compresses files/directories are they are being transferred to the remote host.

-P – Specifies the ssh port of the remote host (if the default ssh port is not set to port 22).

-p – Enforces the preservation of modification and access times of a file.

-r – Recursively copies directories.

-q – Invoke this option to suppress the progress bar as data is being copied to the remote host.

A few points to take into consideration.

To successfully copy files, you need to have read permissions on the files/directories and write permissions on the remote host’s path.

Additionally, caution should be taken when copying files bearing the same name and having the same destination path as this might overwrite the existing files on the remote host.

How to copy a file/directory from a local PC to the remote host with the scp command

In the example below, I’m copying a zip file called latest.zip from my local Linux system to a remote host james@192.168.2.102 where james is the username and 192.168.2.102 is the IP address. The destination directory on the remote system is /home/james/data.

$ scp latest.zip james@192.168.2.102:/home/james/data

Upon running the command, you will be prompted for the remote user’s password on the remote system and SCP will copy your file on the destination directory.

latest.zip

To copy a directory, use the -r flag as shown. Here, we are copying the directory deb_packages to the home directory of the remote user’s host.

$ scp -r deb_packages james@192.168.2.102:/home/james/

How to copy a file/directory from the remote host to the local system with the scp command

Conversely, you can copy files or directories from the remote system to your local PC using the following syntax.

$ scp [option] user@remote_server-IP:/path/to/target/file
/path/to/local/directory

In the example below, I am copying the file latest.tar.gz from the remote system to the home directory of my local Linux PC.
$ scp james@192.168.2.102:/home/james/latest.tar.gz/home/winnie

latest.tar.gz

To copy a directory from the remote system to the local system, use the -r flag. In the example below, I am copying the deb_packages directory from the remote system to my local system.

$ scp -r james@192.168.2.102:/home/james/deb_packages
/home/winnie

To copy a directory from the remote system to the local system, use the -r flag.

For more command options, visit the man pages as shown.

$ man scp

SCP in Linux

Summary

The SCP command provides a convenient way of transferring files to a remote system and vice-versa in a fast and secure manner while employing encryption provided by the SSH protocol. To make the process more seamless, it’s advisable that you configure passwordless SSH authentication between the local and remote system to prevent being prompted for a password each time your copy or transfer files.

SCP command in Linux

Learn More: How to Install psyBNC on Ubuntu 20.04

Categories