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, and 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.
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 protected] ~]# scp [OPTIONS] [SOURCE] [DESTINATION]
- Transfer Local Files To Remote Destination.
For example, We uploading a file from our local computer to a remote location. With SCP, we can accomplish this with a command like:
[[email protected] ~]# scp path/to/local/file.txt [email protected]:path/to/remote/file.txt
After running this above command, a prompt will display for 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 protected] ~]# scp [email protected]: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:
[[email protected] ~]# scp [email protected]:path/to/remote/file.txt [email protected]: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 protected] ~]# scp -r path/to/local/my-folder [email protected]:path/to/remote/my-folder
- Suppressed Mode.
-q flag suppresses the progress meter and non-error messages so your terminal stays clean:
[[email protected] ~]# scp -q path/to/local/file.txt [email protected]: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 protected] ~]# scp -i path/to/local/keypair.pem path/to/local/file.txt [email protected]: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:
[[email protected] ~]# scp -p -q -P 999 -i path/to/local/keypair.pem -r path/to/local/folder [email protected]:path/to/remote/folder
Congratulations! You have successfully learned to use the SCP command. Thanks for using this tutorial for We covered transferring files from the local host to a remote host, from a remote host to the local host, and between two remote hosts on a Linux system.