Ubuntu Linux

A Guide to SCP: Secure File Transfer on Ubuntu

Richard
Written by
Richard
Jan 17, 2024 Updated Mar 19, 2026 5 min read
A Guide to SCP: Secure File Transfer on Ubuntu

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:

💻Code
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:

💻Code
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:

💻Code
scp example.txt [email protected]:/home/username/

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

💻Code
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:

💻Code
scp [email protected]:/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:

💻Code
scp -P 2322 example.txt [email protected]:/home/username/newName.txt

Transferring directories

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

💻Code
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:

💻Code
scp -r example/ [email protected]:/home/username/

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

💻Code
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:

💻Code
scp -r [email protected]:/home/username/example/ /home/localuser/

That should do it!

Conclusion:

  • Using the SSH protocol, SCP (secure copy) is an essential command-line utility for securely transferring files and directories between networked devices.
  • It provides a secure way to transfer files over a network and is particularly useful for protecting sensitive information on insecure networks.
  • Understanding the SCP command’s basic syntax and commonly used options is crucial for efficient usage.
  • The examples demonstrate transferring files and directories between local and remote machines using SCP on Ubuntu Linux.

Frequently Asked Questions

What is SCP and how does it work on Ubuntu?

SCP (secure copy) is a command-line utility that securely transfers files and directories between networked devices using the SSH protocol. It provides both authentication and encryption, making it a safe way to transfer files over insecure networks while protecting sensitive information.

How do I copy a file from my local Ubuntu machine to a remote server?

Use the command: scp /path/to/local/file username@remote_ip:/path/to/destination. For example, scp example.txt [email protected]:/home/username/ will transfer the file to the remote machine at the specified location.

What does the -r option do in the SCP command?

The -r option tells SCP to copy directories recursively, meaning it will copy the entire directory and all its contents. This is essential when you need to transfer folders rather than individual files.

How do I transfer files using a non-standard SSH port with SCP?

Use the -P option (capital P) followed by the port number. For example: scp -P 2322 example.txt [email protected]:/home/username/ will transfer the file using SSH port 2322 instead of the default port 22.

Can I download a file from a remote server to my local Ubuntu machine using SCP?

Yes, use the command: scp username@remote_ip:/path/to/file /path/to/local/destination. For example, scp [email protected]:/home/username/example.txt /home/localuser/ will download the file from the remote server to your local machine.

Was this guide helpful?

Richard

About the Author

Richard

Tech Writer, IT Professional

Richard, a writer for Geek Rewind, is a tech enthusiast who loves breaking down complex IT topics into simple, easy-to-understand ideas. With years of hands-on experience in system administration and enterprise IT operations, he’s developed a knack for offering practical tips and solutions. Richard aims to make technology more accessible and actionable. He's deeply committed to the Geek Rewind community, always ready to answer questions and engage in discussions.

2463 articles → Twitter

📚 Related Tutorials

Download Bing Wallpaper for Ubuntu Linux: A Quick Guide
Ubuntu Linux Download Bing Wallpaper for Ubuntu Linux: A Quick Guide
How to Install VirtualBox on Ubuntu 24.04
Ubuntu Linux How to Install VirtualBox on Ubuntu 24.04
How to Customize Mouse Pointer Color in Ubuntu
Ubuntu Linux How to Customize Mouse Pointer Color in Ubuntu
Mount External Storage as Read-Only in Ubuntu
Ubuntu Linux Mount External Storage as Read-Only in Ubuntu

Leave a Reply

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