A guide to securely transfer files with the SCP command on Ubuntu Linux

|

|

This article instructs on using SCP (secure copy) command for securely transferring files and directories on Ubuntu Linux through the SSH protocol. It provides SCP syntax, options for preservation, compression, and recursion, and examples of copying to and from remote machines, including how to specify ports and save files with different names.

This article explains using the SCP (secure copy) on Ubuntu Linux.

SCP (secure copy) is a command-line utility that securely copies files and directories between two networked devices. It transfers files over a network from one computer to another using the SSH protocol.

It uses the SSH protocol for authentication and encryption, making it a secure way to transfer files over a network.

When you want to transfer files securely over an insecure network, it’s recommended that you use SCP to protect sensitive information.

Below are some examples of how to use the SCP command on Ubuntu Linux.

SCP command examples

Before using the scp command, you should understand its basic syntax.

The scp command syntax uses the following format:

scp [OPTION] [user@]SRC_HOST:]file1 [user@]DEST_HOST:]file2
  • OPTION – scp options that specify cipher, SSH configuration, SSH port, limit, recursive copy, etc.
  • [user@]SRC_HOST:]file1 – path to the source file.
  • [user@]DEST_HOST:]:file2 – path to the destination file.

Local files can be specified using an absolute or relative path, while remote file names must include a user and host specification.

The most commonly used options for the SCP command are:

  • -P – Specifies the remote host ssh port.
  • -p – Preserves file modification and access times.
  • -q – Use this option to suppress the progress meter and non-error messages.
  • -C – This option forces scp to compress the data as it is sent to the destination machine.
  • -r – This option tells scp you to copy directories recursively.

Transferring files

To transfer a file from the local machine to a remote machine, use the following command:

scp [options] /path/to/local/file username@remote:/path/to/destination

For example, to transfer a file named `example.txt` from the local machine to a remote machine with the IP address `192.168.0.2`, use the following command:

scp example.txt username@192.168.0.2:/home/username/

To transfer a file from a remote machine to the local machine, use the following command:

scp [options] username@remote:/path/to/file /path/to/destination

For example, to transfer a file named `example.txt` from a remote machine with the IP address `192.168.0.2` to the local machine, use the following command:

scp username@192.168.0.2:/home/username/example.txt /home/localuser/

If you want to save the file under a different name, specify the new file name at the destination. If SSH on the remote host is listening on a port other than the default 22, then you can specify the port using the -P argument:

scp -P 2322 example.txt username@192.168.0.2:/home/username/newName.txt

Transferring directories

To transfer a directory from the local machine to a remote machine, use the following command:

scp [options] -r /path/to/local/directory username@remote:/path/to/destination

For example, to transfer a directory named `example` from the local machine to a remote machine with the IP address `192.168.0.2`, use the following command:

scp -r example/ username@192.168.0.2:/home/username/

To transfer a directory from a remote machine to the local machine, use the following command:

scp [options] -r username@remote:/path/to/directory /path/to/destination

For example, to transfer a directory named `example` from a remote machine with the IP address `192.168.0.2` to the local machine, use the following command:

scp -r username@192.168.0.2:/home/username/example/ /home/localuser/

That should do it!

Conclusion:

This post showed you how to use the SCP command on Ubuntu Linux. Please use the comments form below if you find errors or have something to add.

Like this:



Leave a Reply

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.