As a system administrator or Linux power user, you will regularly need to securely transfer data and backup files between local and remote systems or between two remote systems. The SCP command line utility will assist you in accomplishing this.
In this tutorial, we learn about scp command in Linux with examples.
What is SCP Command?
SCP short for secure copy protocol is a file transfer network protocol that makes it simple and safe to copy files between two remote computers or between a remote computer and a local computer.
SCP employs the SSH (Secure Shell) protocol for authentication and encryption, ensuring that the information transmitted is secure from malicious actors.
Syntax
Before we begin using the SCP (secure copy) command, let's go over the basic syntax.
The syntax for the SCP command is as follows:
scp [options] [src [email protected] IP address]:/[directory or file name] [dst [email protected] IP address]:/[dst directory]
Here is a breakdown of the syntax:
- OPTIONS - parameters you can use with SCP command such as ssh port, the cipher used for encrypting the data transfer, IP version to use, and so on. We’ll discuss the commonly used ones later in the article.
- - username and the system's IP address with the file you want to copy.
- :/[directory or file name] - the file name and directory where the file is located.
- [dst [email protected] IP address] - the destination username and IP address.
- :/[dst directory] - The destination directory to which the file will be copied.
SCP command Options
The SCP command has several command line options that change its behavior. The most commonly used ones are:
Options | Description |
---|---|
-3 | route the traffic through your machine. |
-4 | forces scp to use IPv4 addresses only. |
-6forces scp to use IPv6 addresses only. | |
-A | Allows ssh-agent to be forwarded to a remote system. |
-B | Selects batch mode (prevents asking for passwords or passphrases). |
-C | Enable data compression when transferring files. |
-c | Selects the cipher to be used to encrypt the data transfer. |
-l | Limits the available bandwidth in Kbit/s. |
-O | Use the legacy SCP protocol for file transfers instead of the SFTP protocol. |
-P | specify a port if SSH on the remote server is set to listen on a different port other than the default port 22 |
-p | preserve file attributes such as the modification times, access times, and file mode bits of the original files |
-q | Quiet mode: disables the progress meter as well as warning and diagnostic messages from |
-r | Recursively copy a directory and its subdirectories |
Feel free to refer to the man pages for more options that come with the SCP command.
SCP Command Examples
You now understand what the SCP command is and what it is used for. In the sections that follow, we will go over some practical examples of how to use this tool to transfer files.
1. Copy Data from local machine to remote server
In this example, we'll transfer the file log.txt to a specified remote user's home directory. In this case, our remote user is 'linux'.
scp log.txt [email protected]:/home/linux
You will be prompted to enter the user password of the remote server for the transfer process to begin. It will look like this:
Once the transfer process is started, you will see a progress meter that displays the transmission status.
When the filename is excluded in the destination path, the local file is copied to the remote server with the original name. If you want to save the file to the remote server under a different name you can provide a new filename.
We copied the file log.txt to the remote host user's home directory and renamed it systemlogs.txt.
scp log.txt [email protected]:/home/linux/systemlogs.txt
To recursively copy files or a directory and its subdirectories to the remote server, use the -r option.
Here is an example of copying a directory name logs to the remote system:
scp -r logs [email protected]:/home/linux
2. Copy Data from a remote server to a local host
If you want to copy files from a remote computer to a local host, you can simply achieve that by using the remote location as a source and the local host as the destination.
In this example, we'll transfer the remote file systemlogs.txt from a remote machine with the username Linux to our local host's current working directory which is denoted by the period(.):
scp [email protected]:/home/linux/systemlogs.txt .
It's also possible to recursively copy a directory and its subdirectories from a remote computer by making use of the -r option:
scp -r [email protected]:/home/linux/backups ~/
Executing the command will copy directory backups from the remote directory /home/linux to our local host user's home directory ~/. Notice that when copying files from a remote machine the progress meter will not be shown.
3. Copying Data Between Remote Servers
The advantage of using SCP for file transfers is that it allows you to connect to local systems and remote servers.
Let's say we want to copy the file backup.zip from one remote server to another. The command looks like this:
scp [email protected]:~/backup.zip [email protected]:/home/linux/backups
Before the transfer can begin, you will be prompted to enter the passwords for both remote hosts.
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.
scp -3 [email protected]:~/backup.zip [email protected]:/home/linux/backups
4. SCP with a different Port Number
The -P (upper case) argument can be used to specify a port if SSH on the remote server is set to listen on a different port other than the default port 22:scp -r
scp -r projects [email protected]:~/ -P 4343
5. Copy Data without changing the timestamp
If you want tokeep preserve file attributessuch as the modification times, access times, and file mode bits of the original files use the -p (lower case) option.
Here the source file (original file) logs.txt attributes will not be changed:
scp -p logs.txt [email protected]:~/
Important Key takeaway points
- The SCP command relies on ssh to securely copy files, so it needs a password or secure shell keys (ssh keys) to encrypt files and authenticate on the remote systems.
- You need to have root access or at least write permission on the target system and read permission on the source file to copy files.
- When copying files that have the same name and location on both systems, be cautious because SCP will overwrite files without giving any warning.
- SCP differentiates between local and remote hosts using the colon (:).
Conclusion
In this tutorial, we learned about the SCP command in Linux and how to use it to securely copy files between systems. For more information visit the scp man page or simply type "man scp" from the terminal.
Comments