Whenever you're working with computers or any electronic device that has storage capacity, you might need to distribute or share information and files in various ways.
These might be sound files, images, notes, or any number of text configuration files.
And the information you share will often be private or confidential, and you won't want it to fall into the wrong hands. This means that protecting the info is essential.
When it comes to sharing data in operating systems like Linux, there are multiple commands you can choose from to share information. But today we'll be focusing on the
SCP command. It lets you share files and data securely and easily.
Given the continuous growth in the Linux job market, having essential skills like secuerly transferring data is essential if you're pursing jobs in the field.
Let's begin by understanding what SCP is, and then we'll learn some commands you can use to transfer files.
What are Linux SCP Commands?
SCP stands for Secure Copy Protocol. It encrypts the transmission of files between Linux systems. SCP is a command-line tool that you use to copy or transfer files and directories across Linux systems securely.
For example, if you want to copy files to a Linux system and you are worried that prying eyes on the network might have access to your files, you can easily use SCP commands.
SCP uses encryption over an SSH (Secure Shell) connection, which ensures that even if the data is intercepted it is still protected.
Whenever you're trying to learn a new command, you'll need to understand the basic syntax. This will help you understand how they're arranged and how to write them properly.
scp [OPTIONS] [[user@]src_host:]file [[user@]dest_host:]file
The source can either be the client or server depending on the origin of the file.
With SCP, there are some options you can use while copying the file. These features grant different permissions depending on how they are used.
The most common include:
- P (Caps) - Specifies the port to connect to on the remote host.
- p (lowercase) - Preserves the time-stamp for modification and access.
- r - Recursively copies entire directories.
- q - Quiet mode, doesn't display progress or messages.
- C - Compresses the data during transmission
With SCP commands, you are able to:
- Copy files within the same machine.
- Copy files from local host to remote host and vice versa.
- Copy files between two different remote servers.
Before you use any of these SCP commands, there are a few things to keep in mind.
First, make sure you have SSH installed on both the client and server machines.
Second, you need to know the username and password of the remote system. You will be prompted to enter the password of the remote host, and your file transfer will begin only when you enter the correct password of the remote host.
Let's look at some of these SCP commands in a bit more detail.
Common Linux SCP Commands
Copy a file from a local to remote server with SCP
Let's say you are sending a local file to a remote system over SSH using SCP.
To copy a file from your local machine to a remote host, specify the path to the file as the source path along with the path in the remote host where the file needs to be copied.
Typically, you do not need to specify the location for a file if you are in your /home/user directory. If you want to copy the test.txt file to the server, you can enter the following:
scp test.txt userbravo@destination:/location2
To copy all .txt files into userbravo’s home directory, enter the following:
scp *.txt userbravo@destination_host:/~/
If you only specify the destination directory, SCP will leave the filename as-is.
To change the filename, define a new filename in the destination:
scp test.txt userbravo@destination_host:/user/home/useralphatest.txt
In this example, we copied the test.txt file from the local machine, then saved it as useralphatest.txt in the user directory of the destination system.
Copy a file from remote to local using SCP
Take this scenario. You want to copy files from a remote Linux system your currently logged-in system. All you need to do is to invoke SCP followed by the remote username, @, the IP address or host, colon, and the path to the file.
If the path is not specified, the default path is the remote user’s home directory. Then, define the local path where the file will be stored locally.
A simple scp example to copy a file from remote to the local device would be:
scp <Username>@<IPorHost>:<PathToFile> <LocalFileLocation>
In my case, way I wanted to copy a file named linuxcheatsheet from the remote device with this address 192.168.1.100.
The linuxcheatsheet file is stored on the kali user’s home directory, the user I will authenticate. Therefore after the colon, I don’t need specify the path because it's the default one, which is the home directory, and I just type the filename (“linuxcheatsheet”). Then, I specify the current directory as the local location to store the file by typing a dot.
Copy a file from one remote host to another
You see, the beauty of SCP is that it does not limit you to connecting between local machines only. You can also connect between remote severs, too.
Now, to copy a file from one remote system to another, use the following command:
scp firstname.lastname@example.org:/files/test.txt email@example.com:/files
This will replicate the test.txt file from the /files directory on host1.com to the /files directory on /host2.com.
Copy multiple files with SCP
To copy multiple files from a local machine to another host, all you need to do is specify the file names as the source path.
scp file1.txt file2.txt user@<ip_address_of_user>:/home/user/Desktop
Quick Tips to Help You Use SCP
The SCP command relies on SSH for secure data transfer, meaning it requires a password to authenticate on remote systems.
To be able to copy files, you must have read permissions on the source file and write permission on the target system.
Watch out when copying files with the same name and location, as SCP will overwrite them without warning you.
To be able to distinguish between local and remote locations, use ':'.
Whether you are a support engineer, system admin, or even a growing developer like myself who uses Linux or wants to learn it – it's likely that you will have to transfer files at some point. And knowing these simple SCP commands will come in handy.
In this article, we have covered some of the most common scenarios where you'd want to use SCP and hopefully you have learned something new.
Enjoy Coding ❤