The Ultimate Guide to Mastering the scp Command on Linux

The Ultimate Guide to Mastering the scp Command on Linux

A comprehensive guide on using the scp command on Linux, enabling fast and secure file transfers Explore various techniques like copying single or multiple files, retrieving files, and even transferring between remote servers Unlock the full potential of SCP with our detailed instructions

Key Takeaways

SCP is a secure and simple way to copy files between Linux computers using SSH. It is easy to use and can be a safe means of transferring files.

SCP has been deprecated since OpenSSH version 8.8, and modern implementations now favor SFTP for file transfers. Nevertheless, the syntax remains unchanged.

Although rsync is the preferred choice for copying files between computers, scp remains a viable option for self-contained network transfers. It is crucial to ensure that SSH is running on all participating computers.

The scp command makes copying files between Linux computers easy and secure. It uses SSH security, but best of all, it is simple. If you can use cp, you can use scp.

What is scp and the Secure Copy Protocol?

To clarify, we need to establish two distinct terms: SCP and scp. The uppercase SCP refers to the Secure Copy Protocol, while the lowercase scp denotes the secure cp program. In essence, SCP represents a specific protocol, whereas scp represents a particular program.

SCP was created to securely copy files between remote Linux computers using SSH. SSH, or secure shell, is a cryptographic network protocol commonly used to access and log in to remote Linux computers. OpenSSH provides the SSH functionality on Linux distributions.

However, SCP is outdated and there are concerns about its use today. Starting from OpenSSH version 8.8, SCP has been marked as deprecated. Modern implementations of SCP now default to using the Secure File Transfer Protocol (SFTP), while still utilizing SSH for secure connections. This transition from SCP to SFTP is seamless and happens without any changes to the SCP syntax.

To check the installed version of OpenSSH on your computer, simply type:

ssh -V

The Ultimate Guide to Mastering the scp Command on Linux

Copying a Single File with scp

Similar to the standard cp command, scp is used to transfer files from the source location to the destination location. In order to copy a file to a remote computer, it is necessary to have knowledge of the IP address or network name of the remote computer. Additionally, one must possess the credentials for a user account that has the necessary write privileges for the designated location where the file is intended to be sent.

To send a file called "sample.txt" to a computer called "fedora-34" on the local network, the syntax is:

scp ./sample.txt dave@fedora-34.local:/home/dave/Downloads/

The Ultimate Guide to Mastering the scp Command on Linux

The command is made up of:

scp: The scp command

./sample.txt: The file we're going to send. This is in the current directory.

dave@: The user account on the remote computer we're going to send the file to.

fedora-34.local: The network name of the remote computer.

The file should be copied to the specified location on the remote computer, which is "/home/dave/Downloads/". Please remember to include the colon ":" to separate the computer name and the path.

After providing the password for the account on the remote computer, the file will be successfully copied.

To change the name of the file on the remote computer, simply include the desired filename in the target path. For instance, to duplicate the file and name it "different-file.txt," utilize the following syntax:

scp ./sample.txt dave@fedora-34.local:/home/dave/Downloads/different-file.txt

The Ultimate Guide to Mastering the scp Command on Linux

Exercise caution while using the scp command as it will silently overwrite any existing files. When copying files, double-check if a file with the same name already exists on the target computer. If so, the existing file will be overwritten and permanently lost.

If the target computer isn't using the default SSH port of 22, you can use the -P (port number) option to provide the appropriate port number.

Retrieving a Single File

To copy a file from a remote server, specify the remote server as the source and the desired local path as the target. Let's copy the file named "development-plan.md" from the remote computer to the current directory on the local computer.

Use the following command:

scp dave@fedora-34.local:/home/dave/Downloads/development-plan.md .

The Ultimate Guide to Mastering the scp Command on Linux

If you add a filename to the local path, the file is copied and given that name.

scp dave@fedora-34.local:/home/dave/Downloads/development-plan.md ./dp-1.md

The Ultimate Guide to Mastering the scp Command on Linux

The file is copied but renamed to our specified filename.

ls -hl *.md

The Ultimate Guide to Mastering the scp Command on Linux

Copying Multiple Files

Multiple files can be easily copied in both directions. You can specify as many source files as needed. In this example, we are duplicating two markdown files and a CSV file.

To execute, use the following command:

scp ./dp-1.md ./dp-2.md ./dp-3.csv dave@fedora-34.local:/home/dave/Downloads/

The Ultimate Guide to Mastering the scp Command on Linux

The three files are copied to the remote computer. You can also use wildcards. This command does exactly the same thing as the last command.

scp ./dp. dave@fedora-34.local:/home/dave/Downloads/

The Ultimate Guide to Mastering the scp Command on Linux

Recursively Copying Directories Over scp

The -r (recursive) option allows you to effortlessly copy entire directory trees in a single command. We have placed two files in a directory named "data" and established a subdirectory named "CSV" within the "data" directory. Subsequently, we have positioned a CSV file within the "data/CSV" subdirectory.

This command copies the files and recreates the directory structure on the remote computer.

scp -r ./data dave@fedora-34.local:/home/dave/Downloads/

The Ultimate Guide to Mastering the scp Command on Linux

Copying Files Between Remote Servers

To copy files from one remote server to another, you can utilize scp with a straightforward syntax. Simply specify the account name and network address of both the source and target servers. This will ensure that the files are successfully copied from the source server to the specified location on the target server.

However, while the syntax itself is uncomplicated, it requires additional consideration to ensure a smooth process. It is crucial that the designated user account on the command line has access to the desired location on the remote server for file copying. Furthermore, this user account must possess the necessary write permissions for that particular location.

To proceed, it is essential to establish SSH access between your local computer, the source computer, and the target servers. Verify that you can successfully log in to the target server from the source server using SSH. Failure to do so will result in scp being unable to establish a connection.

The recommended approach is to set up SSH keys, allowing for authentication without the need for passwords. Relying on passwords can become cumbersome, particularly if multiple user accounts are involved, as it hinders complete automation when utilizing a script.

To transfer files from the "davem" user account on a Manjaro computer to the "dave" account on a Fedora computer, using the scp command from our local Ubuntu computer, follow this syntax:

scp davem@manjaro20-0-1.local:/home/davem/man. dave@fedora-34.local:/home/dave/

The Ultimate Guide to Mastering the scp Command on Linux

We are now back at the command line without any visible indication of the process that took place. Following the principle that no news is good news, scp only provides error reports for remote to remote copying. Upon examining the Fedora computer, we can confirm that the files from the Manjaro computer have been successfully copied and received.

The Ultimate Guide to Mastering the scp Command on Linux

By default, files are directly copied from the source computer to the target computer. However, you have the option to override this by using the -3 (three-way) option.

With the -3 option enabled, files are transferred from the target to the source computer, passing through your local computer. In order for this to occur, seamless SSH access must be established from your local computer to the target computer.

scp -3 davem@manjaro20-0-1.local:/home/davem/man. dave@fedora-34.local:/home/dave/

The Ultimate Guide to Mastering the scp Command on Linux

There is still no indication of any activity, even when transferring the files through your personal computer. However, the true test lies in inspecting the destination computer.

Other SCP Options

The -p option will preserve the file attributes of the transferred files including their original creation, ownership, and access flags. As a result, the transferred files will retain the same metadata as the original files on the source computer.

In case you encounter any error messages, you can try running the command again and utilize the -v flag for verbose mode. This will provide you with detailed information about the transfer attempt, allowing you to easily identify the point of failure in the output.

The -C option compresses files during copying and decompresses them upon receipt. Originally designed for slow modem communications, reducing payload size could save transmission time. However, given the current speed of compressing and decompressing files, the difference in transmission time between compressed and uncompressed files is minimal. Since scp is primarily used for copying files within a LAN, transmission speed is not a significant concern.