How to Use SCP Command in Linux With Examples

How to Use SCP Command in Linux With Examples

Transferring files between two systems can be a challenging task, especially when you need to do it securely. Fortunately, the SCP (Secure Copy) command provides an easy and secure way to transfer files between systems over a network. In this article, we will cover everything you need to know about using the SCP command in Linux to securely transfer files between systems.


Important Notes Before You Begin

Before you can begin using the SCP command, there are a few things you need to keep in mind.

SSH Authentication

As mentioned earlier, the SCP command relies on SSH for data transfer. This means that you will need to have an SSH key or password to authenticate with the remote system. If you do not have an SSH key set up, you can easily generate one using the ssh-keygen command.

Local and Remote Locations

The SCP command uses a colon (:) to distinguish between local and remote locations. For example, user@remote:/path/to/destination/ specifies a remote location, while /path/to/source/file.txt specifies a local location.

File Permissions

To be able to copy files, you must have at least read permissions on the source file and write permission on the target system. If you do not have the necessary permissions, you will receive an error message when attempting to copy the file.

Overwriting Files

When copying files that share the same name and location on both systems, SCP will overwrite the destination file without warning. To avoid losing any data, it’s important to be careful when copying files that have the same name and location on both systems.

Transferring Large Files

When transferring large files, it is recommended to run the SCP command inside a screen or tmux session. This will allow you to detach the session and continue the transfer even if your SSH connection is interrupted.


Basic Syntax of SCP Command in Linux

The basic syntax of SCP command is as follows:

scp [options] [source] [destination]

Options:

SCP command provides several options that can be used to customize the transfer process. Here are some commonly used options:

  • -r: Recursively copy entire directories
  • -P: Specify a different port number for SSH connection
  • -v: Verbose output (useful for debugging)
  • -C : Force scp to compresses the data as it is sent to the destination machine.
  • -i: Specify identity file (private key) for SSH authentication
  • -p : Preserves files modification and access times.
  • -q : Suppress the progress meter and non-error messages.

Source and Destination:

The source and destination parameters specify the location of the file(s) you want to transfer. The format of the source and destination parameters is as follows:

[user@]host:[path]

How to Copy a Local File to a Remote System with the SCP Command in Linux

To copy a file from your local system to a remote system, use the following syntax:

scp /path/to/local/file.txt user@remote:/path/to/destination/

Here, /path/to/local/file.txt is the path of the file you want to copy, user is the username of the user on the remote system, remote is the hostname or IP address of the remote system, and /path/to/destination/ is the path of the destination directory on the remote system.

When you’re copying a file using the SCP command, you have the option to specify a new filename for the file on the destination system. To do this, you simply include the new filename in the destination path when you run the SCP command.

For example, if you want to copy “file.txt” to the remote system at “/remote/directory/” but you want to give it a new name, you can use a command like this:

scp file.txt remote_username@10.10.0.2:/remote/directory/newfilename.txt

This will copy “file.txt” to the remote system at “/remote/directory/” but it will be renamed to “newfilename.txt“.

If the remote system you’re copying to is using a non-standard SSH port (i.e. a port other than 22), you’ll need to specify the port number using the -P argument.

For example, if your SSH port is 2322, you can use a command like this:

scp -P 2322 file.txt remote_username@10.10.0.2:/remote/directory

This will copy “file.txt” to the remote system at “/remote/directory/” using SSH port 2322.

To copy an entire directory and all its contents, you’ll need to use the -r flag for recursive. This tells SCP to copy not just the directory itself, but also all the files and subdirectories contained within it.

For example, if you want to copy the directory “/local/directory” and all its contents to the remote system at “/remote/directory“, you can use a command like this:

scp -r /local/directory remote_username@10.10.0.2:/remote/directory

This will copy the entire directory “/local/directory” and all its contents to the remote system at “/remote/directory“.


How to Copy a Remote File to a Local System Using the SCP Command in Linux

To copy a file from a remote system to your local system, use the following syntax:

scp user@remote:/path/to/remote/file.txt /path/to/local/destination/

Here, user is the username of the user on the remote system, remote is the hostname or IP address of the remote system, /path/to/remote/file.txt is the path of the file you want to copy, and /path/to/local/destination/ is the path of the destination directory on your local system.

For example, let’s say you want to copy a file named “backup.zip” from a remote system with IP address 10.0.0.5, and the file is located in the directory “/home/user/backups“. You want to copy it to the local system’s directory “/home/localuser/downloads“.

You can use the following command to copy the file:

scp user@10.0.0.5:/home/user/backups/backup.zip /home/localuser/downloads

If the file already exists in the local system’s directory, SCP will overwrite it without warning, so be careful when using this command.


How to Copy a File Between Two Remote Systems using the SCP Command in Linux

To copy a file between two remote systems, use the following syntax:

scp user1@remote1:/path/to/source/file.txt user2@remote2:/path/to/destination/

Here, user1 is the username of the user on the first remote system (remote1), /path/to/source/file.txt is the path of the source file on the first remote system, user2 is the username of the user on the second remote system (remote2), and /path/to/destination/ is the path of the destination directory on the second remote system.

For example, if you want to copy a file named “file.txt” from a remote system at IP address 10.0.0.2, using the username “user1“, to another remote system at IP address 10.0.0.3, using the username “user2“:

scp user1@10.0.0.2:/home/user1/file.txt user2@10.0.0.2:/home/user2/

This will copy the file “file.txt” from the home directory of the user “user1” on the first remote system to the home directory of the user “user2” on the second remote system.


Conclusion

SCP command is a useful tool for securely transferring files between two systems over a network. In this article, we covered the basic syntax of SCP command and provided examples to help you understand how to use it. With the help of SCP command, you can easily transfer files between Unix-based systems with ease and security.

Got questions or suggestions on the article? Don’t hesitate to leave a comment below. Your feedback is valuable to us and helps improve the content quality. And don’t forget to share this article with others who might find it useful. Looking forward to hearing from you!

If our tutorials helped you, please consider buying us a coffee. We appreciate your support!

Thank you for your support.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top