Skip to content
Follow
Ubuntu Linux

A Guide to SCP: Secure File Transfer on Ubuntu

Richard
Written by
Richard
Updated 6 min read
How to Enable or Disable Microsoft Defender Cloud Protection
How to Enable or Disable Microsoft Defender Cloud Protection

SCP (Secure Copy Protocol) is a command-line tool that lets you move files and folders safely between computers over a network. It uses SSH (Secure Shell) to encrypt your data while it travels, so no one can snoop on your transfer.

Advertisement

Moving a project folder to an Ubuntu 22.04 LTS server or pulling files back to your desktop takes just one command. You can run SCP from your terminal, and the OpenSSH client that provides it is normally already installed on Ubuntu. The remote machine does need an SSH server running and reachable, because SCP connects through SSH. If SSH logins to that machine don’t work, SCP won’t work either.

⚡ Quick Answer

Use the `scp` command in your Ubuntu terminal to securely transfer files and directories. To copy a file from local to remote, use `scp /path/to/local/file username@remote:/path/to/destination`. For remote to local, reverse the source and destination paths.

Advertisement

SCP command examples

SCP command examples show you how to structure secure file transfers between computers using the terminal. The basic format is `scp source_host:file destination_host:file`. Each side of the command points to a file you want to move, and one side is usually your local machine.

Advertisement

[options]

[user@]

A local path needs no host name. A remote path has the form user@host:path, with a colon between the host and the path.

[user@]

Advertisement

You can add extra flags to change the port or connection method, depending on your network setup. The most useful ones are covered below. Flags always go right after scp and before the source and destination.

The scp command syntax uses the following format:

💻Code
scp [OPTION] [[user@]SRC_HOST:]file1 [[user@]DEST_HOST:]file2

Advertisement

    • OPTION – scp options that specify cipher, SSH configuration, SSH port, limit, recursive copy, etc. You can combine several options in one command, or leave them out entirely for a simple copy.

Advertisement

    • SRC_HOST:]file1

       – path to the source file. This is the file or directory you are copying from.

Advertisement
    • DEST_HOST:]:file2

       – path to the destination file. This is where the copy will be written.

Advertisement
📝Good to KnowLocal files can be specified using an absolute or relative path, while remote file names must include a user and host specification.

A relative path on the remote side is relative to that user's home directory on the remote machine, not to the folder you are in locally.

⚠️WarningBe careful with the destination.

SCP overwrites an existing file with the same name without asking, so check the target path before you press Enter.

The most commonly used options for the SCP command are:

    • -P - Specifies the remote host ssh port. Use it when the SSH server doesn't listen on the default port 22. The letter is uppercase here, which trips up people used to the ssh command.

    • -p - Preserves file modification and access times. Without it, the copies get the time of the transfer.

    • -q - Use this option to suppress the progress meter and non-error messages. This is handy in scripts and scheduled jobs where you only want to see failures.

    • -C - This option forces scp to compress the data as it is sent to the destination machine. It helps most with text-heavy files on slow links. Files that are already compressed, such as ZIP archives or JPEG images, gain little.

    • -r - This option tells scp you to copy directories recursively. It is required whenever the source is a folder.

Transferring files

Transferring files from your local machine to a remote server requires running the `scp /path/to/local/file username@remote:/path/to/destination` command in your terminal. This tells Ubuntu which file to send and where it needs to land on the destination computer over a secure connection.

Here is what happens when you run it:

  1. Open a terminal on the local machine.
  2. Type the command with your own file path, username, remote host and destination path, then press Enter.
  3. If this is the first time you connect to that host, SSH shows a message that the authenticity of the host can't be established and asks if you want to continue. Check that it is the right server, then type yes and press Enter. The host is then remembered for next time.
  4. Enter the remote user's password when asked. Nothing appears on screen while you type, which is normal. If you use SSH keys, this prompt is skipped.
  5. Watch the progress meter. It shows the file name, percentage, size, speed and time remaining. When it reaches 100% and the prompt returns, the copy is finished.

If you see "Permission denied", either the password or key was rejected, or your user can't write to the destination folder. If you see "No such file or directory", check the path on both sides.

💻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 username@192.168.0.2:/home/username/

To transfer a file from a remote machine to the local machine, use the following command. The source is now the remote path, and the destination is a local path:

💻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. Replace the user name and paths with your own. If you leave out the user name, SCP tries your current local user name, which may not exist on the remote machine.

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

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

Transferring directories

Transferring directories and all their contents to another computer requires adding the `-r` flag to your command. You run `scp -r /path/to/local/directory username@remote:/path/to/destination` in your terminal to copy the entire folder recursively in one go.

If you forget the flag, SCP refuses to copy the folder and reports that it is not a regular file. Add the flag and run the command again. For large folders with many small files, expect the transfer to take longer than the total size suggests, because each file is handled separately.

💻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/ username@192.168.0.2:/home/username/

To transfer a directory from a remote machine to the local machine, use the following command. The folder is copied into the local destination you name:

💻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 username@192.168.0.2:/home/username/example/ /home/localuser/

That should do it! After a transfer, it's worth checking that the files arrived where you expected, especially after a large directory copy. Log in to the destination machine, or look in the local destination folder, and confirm the names and sizes match.

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.

Have a question about this guide?

Answered by AI from this guide. Good questions may be added to this page — please don't include personal details.

Did this guide fix your problem?

Richard

About the Author

Richard

Tech Writer, IT Professional

Richard is a writer at Geek Rewind who turns complex IT tasks into clear, step-by-step guides. He draws on years of hands-on experience in system administration and enterprise IT operations, focusing on Windows, Linux and WordPress: the problems people actually run into, and the fixes that work. His server and WordPress guides come from systems he runs himself. Richard builds and maintains the platform behind Geek Rewind, from its Ubuntu servers and Nginx configuration to its custom WordPress plugins. Many new tutorials start with readers' questions, and he's always glad to answer them in the comments.

Advertisement

No comments yet — be the first to share your thoughts!

Leave a Comment

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