In this tutorial, we will show you how to use the scp command on Linux. For those of you who didn’t know, SCP (Secure Copy) is a command-line tool in Linux and Unix-like systems that are used to transfer files and directories across the systems securely over the network. It uses the Secure Shell SFTP subsystem for data transfer, uses the same authentication, and provides the same security as Secure Shell. Scp will ask for passwords or passphrases if they are needed for authentication. By default, the SCP command is included in Linux and Mac, so you don’t need to download anything using those OS.
This article assumes you have at least basic knowledge of Linux, know how to use the shell, and most importantly, you host your site on your own VPS. The installation is quite simple and assumes you are running in the root account, if not you may need to add ‘
sudo‘ to the commands to get root privileges. I will show you the step-by-step use SCP commands on Linux with practice examples.
- A server running one of the following operating systems: Ubuntu based or RHEL based.
- It’s recommended that you use a fresh OS install to prevent any potential issues.
- SSH access to the server (or just open Terminal if you’re on a desktop).
non-root sudo useror access to the
root user. We recommend acting as a
non-root sudo user, however, as you can harm your system if you’re not careful when acting as the root.
Use SCP Command on Linux
SCP (secure copy) is a command-line utility that allows you to securely copy files and directories between two locations.
- Basic Syntax of SCP Command.
By default, the SCP protocol operates on a port
22 unless overridden by a command-line option. All
scp commands follow the form:
[email@example.com ~]# scp [OPTIONS] [SOURCE] [DESTINATION]
- Transfer Local Files To Remote Destination.
For example, We upload a file from our local computer to a remote location. With SCP, we can accomplish this with a command like:
[firstname.lastname@example.org ~]# scp path/to/local/file.txt user@remote-host:path/to/remote/file.txt
After running this above command, a prompt will display the password corresponding to the remote host’s user account. Once the password is entered, the file will be copied.
- Transfer Remote File to a Local Destination.
Now we can copy a file from a remote computer to our local machine:
[email@example.com ~]# scp user@remote-host:path/to/remote/file.txt path/to/local/file.txt
- Transfer Remote File to Remote Destination.
Run the following command format is used to transfer a file between two remote hosts:
[firstname.lastname@example.org ~]# scp user1@remote-host1:path/to/remote/file.txt user2@remote-host2:path/to/remote/file.txt
- Copying Directories.
-r the flag can be used to recursively copy a folder and its contents instead of a single file:
[email@example.com ~]# scp -r path/to/local/my-folder user@remote-host:path/to/remote/my-folder
- Suppressed Mode.
-q flag suppresses the progress meter and non-error messages so your terminal stays clean:
[firstname.lastname@example.org ~]# scp -q path/to/local/file.txt user@remote-host:path/to/remote/file.txt
- Authentication Keypair File.
-i the flag can be used to authenticate the connection using a cryptographic key pair stored in a file instead of a username and password:
[email@example.com ~]# scp -i path/to/local/keypair.pem path/to/local/file.txt user@remote-host:path/to/remote/file.txt
- Using Multiple SCP Options.
This is an example that implements multiple flags to copy a folder from a remote host to our local machine using a keypair file for authentication on port 999 while preserving file properties and suppressing output:
[firstname.lastname@example.org ~]# scp -p -q -P 999 -i path/to/local/keypair.pem -r path/to/local/folder user@remote-host:path/to/remote/folder
Congratulations! You have successfully learned to use the SCP command. Thanks for using this tutorial We covered transferring files from the local host to a remote host, from a remote host to the localhost, and between two remote hosts on a Linux system.