scp command in Linux Explained [with Examples]

Written by: Bobbin Zachariah   |   Last updated: August 23, 2022

The scp short for "secure copy protocol" is a command-line tool in Linux for securely transferring files between a local and a remote host. It uses SSH (Secure Shell) protocol for authentication and encryption, ensuring that the information transmitted is secure from malicious actors.

Syntax

The basic syntax for the SCP command is as follows:

scp [options] [src] [dst]

where:

  • source: The file or directory you want to copy. This can be a local file or a file on a remote system. If it's a remote file, you should specify it with the format [user@]host:file, where [user] is the remote username, [host] is the remote host (hostname or IP address), and [file] is the path to the file on the remote system.
  • destination: The location where you want to copy the source file or directory. Similar to the source, this can be a local or remote destination. For a remote destination, use the format [user@]host:file, where [user] is the remote username, [host] is the remote host (hostname or IP address), and [file] is the path to the destination on the remote system.

Commonly used options:

  • -P port: Specifies the remote SSH port to connect to.
  • -r: Recursively copies directories and their contents.
  • -i identity_file: Specifies a custom SSH private key for authentication. Useful for connecting without needing a password.
  • -C: Enables data compression, reducing network bandwidth usage during file transfer.
  • -p: Preserves file attributes like modification times, access times, and file permissions during copying.

Examples

1. Copy a local file to remote server

Let's say you have a file named logs.txt on your local machine, and you want to copy it to a remote server with the IP address 192.168.1.100 under the home directory of user linux. Use the following command:

We'll transfer the file log.txt to the specified remote user's home directory from the current directory of local machine, which is 'linux' in this case. You will be prompted to enter the remote server's user password to initiate the transfer process. Once started, a progress meter will display the transmission status.

scp log.txt [email protected]:/home/linux
scp example of copying a local file to remote

You will be prompted to enter the remote server's user password to initiate the transfer process. Once started, a progress meter will display the transmission status. Once completed, you will have a copy logs.txt to the home directory of linux on the remote server.

Note: When you exclude the filename from the destination path, the local file is copied to the remote server with its original name.

For instance, the following command copies a file named log.txt to the remote host's user directory and renames it as systemlogs.txt:

scp log.txt [email protected]:/home/linux/systemlogs.txt
scp example of copying a local file to remote and rename the filename

If you have a custom SSH private key for authentication, specify it with the -i option:

scp -i /path/to/private/key.pem myfile.txt username@remote_server:/path/to/destination/

Copy a local directory to a remote server recursively

Alternatively, if you have to copy an entire directory use -r option.

Example:

scp -r logs [email protected]:/home/linux
scp example of copying a directory to remote recursively using -r option

This will copy the entire logs directory and its contents to the home directory of linux on the remote server.

You may do the other way around as well by copying the remote directory to the local machine:

scp -r [email protected]:/home/linux/backups ~/
copying a remote directory to local

2. Copy a remote file to local machine

Suppose you want to copy a file named systemlogs.txt from a remote server with the IP address 192.168.204.59. You want to save it in your local directory.

scp [email protected]:/home/linux/systemlogs.txt .
scp command example to copy remote file to local

The dot . represents the current directory on your local machine. This command will copy the remote file to your current local directory.

3. Copy a file between two remote servers

Let's say you want to copy the file backup.zip from one remote server to another. The command looks like this:

Imagine you want to copy a file named backup.zip from one remote server to another. Let's say you want to copy from 192.168.204.59 to 192.168.204.51. Use this command:

scp [email protected]:~/backup.zip [email protected]:/home/linux/backups
using scp to copy file between two remote servers

Before the transfer can begin, you will be prompted to enter the passwords for both remote hosts.

enter password for two remote servers

Normally, the file will be transferred directly from the first remote server to the second. If you want to route the traffic through your machine, use the -3 option.

4. Specify a Different SSH Port

Suppose the SSH remote server uses a non-default port, you can specify it using the -P option.

The following command uses SSH port 4343 to copy the local directory to the remote server:

scp -r projects [email protected]:~/ -P 4343
scp to connect remote server with specific port number

This command connects to the remote server on port 4343.

5. Copy file without changing timestamp

If you want to preserve file attributes such as the modification times, access times, and file mode bits of the original files use the -p (lowercase) option.

Here the source file (original file) logs.txt attributes will not be changed:

scp -p logs.txt [email protected]:~/
scp example copying a file to remote without changing its timestamp

About The Author

Bobbin Zachariah

Bobbin Zachariah

Bobbin Zachariah is an experienced Linux engineer who has been supporting infrastructure for many companies. He specializes in Shell scripting, AWS Cloud, JavaScript, and Nodejs. He has qualified Master’s degree in computer science. He holds Red Hat Certified Engineer (RHCE) certification and RedHat Enable Sysadmin.

SHARE

Comments

Please add comments below to provide the author your ideas, appreciation and feedback.

Leave a Reply

Leave a Comment