We often need to move files from a local machine to a remote server or vice versa, especially when managing or deploying code on servers. One efficient way to do this is through Secure Copy Protocol (SCP), a tool that’s built into the SSH (Secure Shell) protocol. This guide will walk you through how to use SCP on Linux platforms like Ubuntu, Linux Mint, and Debian, among others.
SSH, or Secure Shell, is a protocol that allows secure access to remote computers.
SSH, an acronym for Secure Shell, is a protocol used to access remote computers securely. Built into this SSH
ecosystem is the Secure Copy Protocol (
SCP), an effective tool for transferring files between local and remote
computers. Its syntax closely resembles the ‘cp’ (copy) command, but it’s geared towards remote file transfer.
Other file transfer applications, such as
rsync, also harness the power of SSH to secure their file
transfers. These applications open up the possibility of securely copying files from local to remote servers, and vice versa.
SCP command in Linux is a utility for transferring files between remote computers. It’s included by default in
most Linux and Unix distributions, including Linux Ubuntu, Linux Mint, Linux Debian, Arch Linux, etc., and is part
of the OpenSSH packages.
SCP is your secure solution for transferring files between a remote location and a host, or from your local
computer to a remote server, or even between two remote locations.
The difference between the
cp command and the
scp command is that the latter requires you to specify the remote
host’s DNS name or IP address and provide username credentials. Here’s how to use SCP for copying files from your
local machine to a remote server, and from a remote server to your local machine.
Copying a single file from your local computer to a remote computer using SCP involves the following syntax:
# From current folder you don't need to specify the file path scp myfile.txt username@remoteserver:/remote/folder/ # From any folder from your local computer, write the full local path to the file scp /full/path/to/myfile.txt username@remoteserver:/remote/folder/
Let’s break down these two examples:
scp myfile.txt username@remoteserver:/remote/folder/
In this first example, we’re using SCP to transfer a file called
myfile.txt from the current working directory on
our local machine to a folder on the remote server.
myfile.txtis the name of the file we want to transfer.
usernameis the username for your account on the remote server.
remoteserveris the DNS name or IP address of the remote server.
/remote/folder/is the path of the directory on the remote server where you want to transfer the file.
By running this command,
myfile.txt will be securely copied from your local machine to the specified folder on the remote server.
scp /full/path/to/myfile.txt username@remoteserver:/remote/folder/
In the second example, we’re doing the same thing but the file we want to transfer isn’t in our current directory. Instead, it’s located elsewhere on our local machine, and we need to specify its full path.
/full/path/to/myfile.txtis the absolute path to the file on your local machine. It provides complete information about the file’s location starting from the root directory.
So, when you run this command, again,
myfile.txt will be securely transferred from the specified path on your local
machine to the designated folder on the remote server.
Both of these SCP commands offer a simple yet powerful way to perform secure file transfer from a local machine to a remote server using SSH, irrespective of where your file is stored locally.
If the target folder on remote host (
/remote/folder/) is not specified, it will copy the file to the remote user’s home directory.
The user specified with
username need to exist and have write permission to
/remote/folder/ in the remote system.
You can also copy a file from a remote server to your local computer using SCP. The syntax is similar, only that you invert the order of the local and remote information:
# From remote to current local folder scp username@remoteserver:/remote/folder/remotefile.txt localfile.txt # From remote to local but specifying the local folder target to save the file scp username@remoteserver:/remote/folder/remotefile.txt /path/to/local/folder/localfile.txt
. (dot) as the copy target (in our example replacing
.), will copy the remote file to
the current working directory using the original filename, in this case:
You can copy multiple files from your local machine to a remote server, or vice versa. You can either specify each file as a parameter, or use wildcards:
# Specify each file scp myfile.txt /local/folder/myfile2.txt username@remoteserver:/remote/folder/ # Use a wildcard to copy several files from remote scp username@remoteserver:/remote/folder/* .
To copy recursively files and folders from local to a remote server, you need to use the option
This is particularly useful when setting up a project structure on a remote server:
scp -r * username@remoteserver:/remote/folder/
If you don’t want to type the remote user password when are copying files, you can use a
.cer file to inform you secure credential to
scp ssh connection.
Note that you still need to write the username on the command:
scp -i ~/.ssh/my-certificate.cer my-local-file.html [email protected]:/www/my-project/
SCP command in Linux is one of the most efficient and secure methods for transferring files between local and
remote computers connected over a network. Whether you need to SCP from local to remote or perform SSH file copy
operations, this guide has you covered.
Always remember to verify your SCP operations to ensure the correct files have been transferred.
Other Linux commands for file manipulation you should know are:
cp: To copy files and directories in the operating system - read the tutorial
mv: To move files and directories.
rsync: To synchronize files and directories, often used for backups.
dd: Used for low-level copying and backup.