How to Use the SCP Command in Linux
Introduction
scp
stands for Secure Copy Protocol. The scp
command in Linux lets you securely transfer files between hosts using an encrypted SSH (Secure Shell) connection. Unlike regular copy commands such as cp
, scp
also ensures data integrity during transfers.
This article explains how to use the scp
command to effectively transfer files and directories in Linux.
Prerequisites
Before you begin:
- Deploy two Ubuntu 24.04 instances on Vultr and enable the
limited user login
option to use as yourlocal
andremote
workstations respectively. - Access both instances using SSH.
- Update the instances.
The scp
Command Syntax
The following is a basic scp
command syntax:
scp [options] [source] [destination]
In the above scp
command:
[options]
: Accepts optional flags to modify the command's behavior[source]
: Specifies the file or directory to copy.[destination]
: Sets the target location where you want to place the files.
Use Most Common scp
Command Options
The following are the most common scp
command options:
Option | Description |
---|---|
-P |
Specifies the port to connect on the remote host. |
-p |
Preserves the original file's modification times, access times, and modes. |
-r |
Recursively copies entire directories. |
-q |
Quiet mode; suppresses non-error messages. |
-C |
Enables compression. |
-l |
Limits the bandwidth used during the transfer. |
-v |
Verbose mode; displays detailed debugging messages. |
-i |
Sets the private key location to use with a public key for authentication |
Create Sample Directories and Files
Follow the steps below to create sample directories and files to test the scp
command in Linux.
Create three sample
.txt
files on your local server.console$ touch file1.txt file2.txt file3.txt
Create a sample
dir
directory on your local server.console$ mkdir dir
Create three sample
.txt
files on the remote server.console$ touch file4.txt file5.txt file6.txt
Create
dir1
anddir2
sample directories on the remote server.console$ mkdir dir1 dir2
Transfer Files and Directories Using SCP
Follow the steps below to transfer files and directories using the scp
command in Linux.
Copy the
file1.txt
file from your local server to the remote server.console$ scp file1.txt linuxuser2@remote_server:/home/linuxuser2
The above command securely copies
file1.txt
from your local server to the remote server user's home directory. By default, the SCP command copies files to the user's home directory unless you specify another directory.Output:
Enter
yes
to accept the remote server's fingerprint key when prompted.Copy
file4.txt
from the remote server to your local server.console$ scp linuxuser2@remote_server:/home/linuxuser2/file4.txt /home/linuxuser1/
Output:
Copy the
dir
directory from the local server to the remote server using the recursive-r
option.console$ scp -r dir linuxuser2@remote_server:/home/linuxuser2/
The above command uses the
-r
option to recursively copy thedir
directory from the local server to thelinuxuser2
home directory on your remote server including all files and subdirectories.Output:
Copy the
dir1
directory from the remote server to your local server user's home directory.console$ scp -r linuxuser2@remote_server:/home/linuxuser2/dir1 /home/linuxuser1/
Output:
You can explore the find command in Linux to quickly locate files on your system.
Enable Passwordless Authentication Using an SSH Key
The SCP command uses password authentication by default to copy and transfer files. SSH key-based authentication enables passwordless authentication when transferring files using the SCP command. Follow the steps below to generate a new SSH key to use with the SCP command.
Generate a new SSH key pair on your local server.
console$ ssh-keygen -t rsa -b 4096
Press Enter to accept the default private key file location and set an optional passphrase when prompted.
Output:
Copy your public key to the remote server.
console$ ssh-copy-id linuxuser2@remote_server
Enter your remote user password when prompted to copy the public key.
Output:
Access the remote server using SSH and verify that you're not prompted to enter a password.
console$ ssh linuxuser2@remote_server
Output:
Transfer Files Using SCP with SSH Key Authentication
Follow the steps below to transfer files using the scp
command with SSH key authentication.
Copy
file2.txt
from your local server to thedir2
directory on the remote server.console$ scp file2.txt linuxuser2@remote_server:/home/linuxuser2/dir2/
The above command securely copies the
file2.txt
file from your local server todir2
on the remote server.Output:
Copy the
dir2
directory from the remote server to your local server.console$ scp -r linuxuser2@remote_server:/home/linuxuser2/dir2 /home/linuxuser1
Output:
Use the SCP Command in Linux with Advanced Options
Copy
file3.txt
to thedir2
directory on the remote server and preserve the original file attributes using the-p
option.console$ scp -p file3.txt linuxuser2@remote_server:/home/linuxuser2
The above command uses the
-p
option to copyfile3.txt
to thedir2
directory while keeping the original file attributes including the original timestamps and file permissions.Output:
Copy
file1.txt
to the remote server and enable compression using the-C
option.console$ scp -C file1.txt linuxuser2@remote_server:/home/linuxuser2/dir2/
The above command copies
file1.txt
to thedir2
directory and uses the-C
option to compress data during the transfer to speed up the process.Output:
Copy
file2.txt
and limit the bandwidth in kilobits per second (Kbps) using the-l
option.console$ scp -l 500 file2.txt linuxuser2@remote_server:/home/linuxuser2
The above command uses the
-l
option to limit the transfer speed to500
Kbps and avoid overusing network resources while transferring the file to the remote server.Output:
Display detailed debugging messages in verbose mode using the
-v
option.console$ scp -v file3.txt linuxuser2@remote_server:/home/linuxuser2
Output:
Specify a private key for authentication using the
-i
option. Replace/path/to/private_key
with your actual private key path.console$ scp -i /path/to/private_key file1.txt user@remote_server:/home/linuxuser2
The above command uses the
-i
option to specify a private key file for SSH authentication. This is useful when using multiple SSH keys and requires a specific key to establish a connection.Output:
Run the following command in your user home directory to view the default SSH private key location.
console$ ls -l /.ssh/
Output:
Copy
file3.txt
to the/dir2
remote server directory using a specific SSH port to transfer the files.console$ scp -P 22 file3.txt linuxuser2@remote_server:/home/linuxuser2
The above command uses the
-P
option to transfer files using the default SSH port22
. This is important when the SSH daemon runs on a custom port on the remote server.Output:
Transfer archived files using
tar
and thescp
commands. For example, compress thedir
directory and transfer the archived file to the remote server.console$ tar czf - dir | ssh linuxuser2@remote_server 'tar xzf - -C /home/linuxuser2/dir1'
The above command compresses the
dir
directory and copies the data over SSH to the remote server. Thetar xzf - -C
command extracts the files into thedir1
directory in the user home directory to enable advanced compression and efficient file transfers.Output:
Conclusion
You have used the scp
command in Linux, its syntax, options, and practical examples. Additionally, you've set up SSH key-based authentication, allowing you to transfer files and directories more efficiently without a password. The scp
command allows you to securely and efficiently transfer files between hosts by combining various command options in Linux.