---
title: "Securely Copy Files With the SCP Command"
description: "Utilize the SCP Command to securely copy files locally and between remote systems in this ATA Learning tutorial!"
canonical: "https://adamtheautomator.com/scp-command/"
---

# Securely Copy Files With the SCP Command

> Utilize the SCP Command to securely copy files locally and between remote systems in this ATA Learning tutorial!

Source: https://adamtheautomator.com/scp-command/

---

ATA Learning

Tap to hide

[

ATA Learning

](/)

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Search for:  

*   [](https://twitter.com/adbertram)
*   [](https://github.com/Adam-the-Automator)
*   [](https://www.linkedin.com/company/adam-the-automator-llc)
*   [](/feed/)

![Securely Copy Files With the SCP Command](https://adamtheautomator.com/wp-content/uploads/2022/06/Securely-Copy-Files-With-the-SCP-Command.jpg)

# Securely Copy Files With the SCP Command

[![](https://secure.gravatar.com/avatar/2788bb1a3f735603f81eca51d68daec56a9d97e805a10268fb2c20afcc76b81b?s=192&d=mm&r=g)Nicholas Xuan Nguyen](https://adamtheautomator.com/author/nicholas-xuan-nguyen/)28 June 20227 min. read

Categories: [IT Ops](/category/it-ops/)

Tags:[Linux](/tag/linux/)

Table of Contents

*   [Prerequisites](#prerequisites)
*   [Copying a File using the SCP Command](#copying-a-file-using-the-scp-command)
*   [Running Recursive Copy with the SCP Command](#running-recursive-copy-with-the-scp-command)
*   [Filtering File Extensions to Copy](#filtering-file-extensions-to-copy)
*   [Enabling Key-Based Authentication](#enabling-key-based-authentication)
*   [Conclusion](#conclusion)

The SCP (Secure Copy) command is a non-interactive command for securely copying files and directories between two systems. SCP uses the SSH protocol for encryption and authentication, making it a secure way to transfer files between remote servers.

Not a reader? Watch this related video tutorial!

**_Not seeing the video? Make sure your ad blocker is disabled._**

If you’re looking for an awesome guide to using the SCP command, look no further. This tutorial will show you SCP command usage examples, which could help you become a confident SCP user.

Ready? Read on to take your file transfers to the next level!

## Prerequisites

This tutorial will be a hands-on demonstration. If you’d like to follow along, be sure you have the following requirements.

*   You’ll need two Linux computers to act as the copy source and the copy destination on the same or different networks. This tutorial will be using Ubuntu 20.04 computers, as listed in the table below.

<table><tbody><tr><td><strong>Computer Name</strong></td><td><strong>Designation</strong></td></tr><tr><td>ubuntu1</td><td>Source host</td></tr><tr><td>ubuntu2</td><td>Destination host</td></tr></tbody></table>

Related:[How to Install Ubuntu 20.04 \[Step-by-Step\]](https://adamtheautomator.com/install-ubuntu/)

*   If there’s a [firewall](https://adamtheautomator.com/ufw-firewall/) on each or between the two computers, make sure that port 22 is open.

Related:[How To Set Up the UFW Firewall on Linux](https://adamtheautomator.com/ufw-firewall/)

*   This tutorial assumes that you have already opened an SSH session and logged on to both computers.

Related:[Using Pageant Putty Agent to Unleash Your SSH Key Use](https://adamtheautomator.com/pageant-putty/)

## **Copying a File using the SCP Command**

There can be many reasons you’d want to copy files from one system to another. Perhaps for backup purposes, replicate configuration files, or simply want to have a copy of the files. Whatever the reason, the SCP command has your back.

The basic syntax for the SCP command is as follows.

```bash
scp [Option] [user1@]SRC_HOST:]your_file1 [user2@]DEST_HOST:]you_file2
```

Where:

*   `Option` – specifies any options to use, such as cipher or limit. You’ll be using some common options in this tutorial.
*   `user1` – is the username on the source host.
*   `SRC_HOST` – is the hostname or IP address of the copy _source_.
*   `your_file1` – is the path and name of the file that you want to copy.
*   `DEST_HOST` – is the hostname or IP address of the copy _destination_.
*   `user2` – is the username with access to the destination host.
*   `your_file2` – is the path and name of the file that you want to copy from `your_file1`

> _Note: You can specify your file paths using a relative or absolute path on the local system. But the file paths on the remote system must use a username with an absolute path._

Now that you understand the basic syntax, you can start with the file copy exercise using the SCP command.

1\. To start, create a new file called _backup.txt_ in the home directory on _ubuntu1_.

```bash
# Change into the home directory
cd ~
# Create a text file with content
echo "This is my backup file" > backup.txt
# Display the file content to confirm
cat backup.txt
# Show the SHA1 hash 
```

![Creating a sample text file on the source host](https://adamtheautomator.com/wp-content/uploads/2022/06/image-340.png)

Creating a sample text file on the source host

2\. Next, create a new directory named _backup_ on ubuntu2. This directory will be the file copy destination path.

```bash
# Creat the backup directory
mkdir ~/backup && cd ~/backup
# Display the full path
pwd
```

![Creating the file copy destination directory](https://adamtheautomator.com/wp-content/uploads/2022/06/image-341.png)

Creating the file copy destination directory

3\. Now, run the scp command below on ubuntu1 to copy _backup.txt_ to the _backup_ directory on ubuntu2. Make sure to replace the username and path with yours.

```bash
scp backup.txt ata@ubuntu2:/home/ata/backup
```

> _If you want to copy the file to a different name, append the new filename to the destination path. Example: scp backup.txt ata@ubuntu2:/home/ata/backup/backup2.txt_

4\. If now’s the first time you’re connecting to the _ubuntu2_ host, type `yes` at the confirmation prompt and press Enter.

5\. Next, type the user account password on _ubuntu2_ and press Enter. Once you enter the correct password, the file copy will proceed.

![Copying a file with the SCP command](https://adamtheautomator.com/wp-content/uploads/2022/06/image-342.png)

Copying a file with the SCP command

6\. After copying the file, switch to your _ubuntu2_ terminal and run the below command to list the directory contents.

```bash
ls
```

Confirm that the file _backup.txt_ exists and you’ve successfully copied the file.

![Listing the copied file](https://adamtheautomator.com/wp-content/uploads/2022/06/image-343.png)

Listing the copied file

7\. Next, display the copied file’s content to confirm that the file is intact on _ubuntu2_.

```bash
cat backup.txt
```

![Confirming the file](https://adamtheautomator.com/wp-content/uploads/2022/06/image-344.png)

Confirming the file

8\. Lastly, to confirm that the files are identical, get the SHA1 checksum of each file on both machines and compare the values. This step is an extra measure to verify the file’s integrity did not change during the transfer.

```bash
# on ubuntu1 and ubuntu2
sha1sum backup.txt
```

![Comparing the file checksum](https://adamtheautomator.com/wp-content/uploads/2022/06/image-345.png)

Comparing the file checksum

## **Running Recursive Copy with the SCP Command**

In the previous section, you learned to copy individual files from a local system to a remote system using the SCP command. You often need to copy an entire directory with its subdirectories and files in real-life scenarios.

Copying entire directories typically apply to backup scenarios. Suppose your server (ubuntu1) runs a web server whose files are in the _/var/www/_ directory. Below is the sample folder tree structure.

```powershell
/var/www
├── html
│   └── index.html
└── your_domain
    └── index.html
```

You can effortlessly back up the entire folder to a remote destination with the SCP command.

> _In most cases, backup operations run through automation scripts and scheduled tasks or cron jobs. This example demonstrates how you would back up an entire directory manually._

1\. First, create a _web-server-backup_ directory on _ubuntu2_.

```bash
# Create the backup directory on ubuntu2
mkdir ~/web-server-backup
# Display the full backup directory path
realpath ~/web-server-backup
```

![Creating the backup directory on the remote machine](https://adamtheautomator.com/wp-content/uploads/2022/06/image-346.png)

Creating the backup directory on the remote machine

2\. Next, switch back to the _ubuntu1_ terminal and run the SCP command below. The -r switch enables the recursive copy mode, which means that SCP will copy the entire _/var/www/_ from the top folder down to the last file.

```bash
scp -r /var/www/ ata@ubuntu2:/home/ata/web-server-backup
```

![Copying an entire folder recursively](https://adamtheautomator.com/wp-content/uploads/2022/06/image-347.png)

Copying an entire folder recursively

3\. Now, run the tree command on both systems to compare the directory structure. On _ubuntu1_, you’ll display the directory tree for _/var/www_. On ubuntu2, you’ll display the directory tree for the backup directory, which is _/home/ata/web-server-backup_ in this example.

```bash
# on ubuntu1
tree /var/www/

# on ubuntu2
tree /home/ata/web-server-backup
```

![Comparing the source and destination directory tree](https://adamtheautomator.com/wp-content/uploads/2022/06/image-348.png)

Comparing the source and destination directory tree

## **Filtering File Extensions to Copy**

Suppose you have a directory on your local system containing mixed file types with different extensions. You’re supposed to copy all files from that folder to a remote computer using the SCP command as part of your job. You already know how to copy entire directories.

But, the condition is that you must only copy all files with the \*_.txt_ extension. Don’t panic; you’re still on track, and the solution is not far from what you already did in the previous sections. To filter the files to copy, you’ll be using wildcards.

> _Common wildcard characters are the asterisk (\*), which represents zero, one, or more characters, and the question mark (?), which means a single character._

1\. First, create the sample folder and files on ubuntu1 that you will be copying later. Run the command below to create the folder _foo_ and files with \*.txt, \*.doc, and \*.ppt extensions.

```bash
# Create a directory named foo
mkdir foo && cd foo
# Create five *.txt files
touch {1..5}.txt
# Create five *.doc files
touch {1..5}.doc
# Create five *.ppt files
touch {1..5}.ppt
# List all the files in the directory
```

![Creating the sample directory and files](https://adamtheautomator.com/wp-content/uploads/2022/06/image-349.png)

Creating the sample directory and files

2\. Next, switch to your ubuntu2 terminal and create a directory called _bar_. This directory will be your copy destination path.

```bash
# Create the folder
mkdir bar
# Display the new folder's full path
realpath bar
```

![Create the destination directory](https://adamtheautomator.com/wp-content/uploads/2022/06/image-350.png)

Create the destination directory

3\. Switch back to your _ubuntu1_ terminal and run the below SCP command. Notice that this time, instead of specifying a specific folder as the source, your source contains a wildcard that matches any files with a _txt_ extension inside the _foo_ folder.

```bash
scp *.txt ata@ubuntu2:/home/ata/bar/
```

Type in the account password, and the copy should continue. As you can see below, the SCP command only copied the _txt_ files despite the source folder having other file types.

![Copying \*.txt files](https://adamtheautomator.com/wp-content/uploads/2022/06/image-351.png)

Copying \*.txt files

4\. Lastly, switch to the ubuntu2 terminal and list the files inside the _bar_ directory.

```bash
ls -l bar/
```

The result should confirm that only \*.txt files exist inside the directory.

![Listing the destination directory files](https://adamtheautomator.com/wp-content/uploads/2022/06/image-352.png)

Listing the destination directory files

## Enabling Key-Based Authentication

Typing in the destination account password can be tedious and cumbersome. Also, password-based authentication is less secure and not appropriate for automation. What’s the alternative? Enable key-based authentication.

As you know, the SCP command uses SSH under the hood. And SSH gives you the option to authenticate using SSH keys instead of passwords. The steps for setting up SCP with SSH key authentication are as follows.

1\. On _ubuntu1_, generate an SSH key pair by running the ssh-keygen command.

```bash
ssh-keygen -t rsa
```

2\. Press Enter to accept the default path in which to save the key. Finally, press Enter two times not to add a passphrase to the key.

As you can see below, the command created two files. The id\_rsa file is your private key, which will stay on the source computer. Conversely, you should copy the id\_rsa.pub to the destination host.

![Generating an SSH key pair](https://adamtheautomator.com/wp-content/uploads/2022/06/image-353.png)

Generating an SSH key pair

3\. Run the below command to copy your public key to the remote host _ubuntu2_.

```bash
ssh-copy-id ata@ubuntu2
```

Enter the destination account password at the prompt to continue copying the public key.

![Uploading the public key](https://adamtheautomator.com/wp-content/uploads/2022/06/image-354.png)

Uploading the public key

4\. Now that you’ve generated the SSH key pair, run the below command to securely copy the _backup.txt_ file from _ubuntu1_ to _ubuntu2_. The argument -i ~/.ssh/id\_rsa tells the SCP command to authenticate with the SSH key file.

```bash
scp -i ~/.ssh/id_rsa backup.txt ata@ubuntu2:backup3.txt
```

The SCP command no longer prompts for the password, as you can see below.

![Copying files using the SCP command with key-based authentication](https://adamtheautomator.com/wp-content/uploads/2022/06/image-355.png)

Copying files using the SCP command with key-based authentication

That’s it! You’ve gotten rid of that pesky password prompt and made your SSH session even more secure.

## Conclusion

This article taught you how to use the SCP command to copy files between two Linux systems securely. You also learned how to use SCP with SSH keys for authentication. Now that you’ve learned about the SCP command, try it out the next time you need to copy one to bulk files between computers.

Share this article

[Share on X](https://twitter.com/intent/tweet?url=https%3A%2F%2Fadamtheautomator.com%2Fscp-command%2F&text=Securely%20Copy%20Files%20With%20the%20SCP%20Command)[Share on Facebook](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fadamtheautomator.com%2Fscp-command%2F)[Share on LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fadamtheautomator.com%2Fscp-command%2F)

## Related Posts

![](https://adamtheautomator.com/wp-content/uploads/2022/09/Practical-Linux-Unix-Tee-Commands-for-the-Linux-Admin.jpg)

### [Master Unix tee Commands for Real-World Linux Admin Tasks](/unix-tee/)

Simplify Linux output management and streamline your workflow with the “Unix tee” command. This tutorial guides you through its practical applications.

![](https://adamtheautomator.com/wp-content/uploads/2024/03/openldap-4.jpg)

### [How to Install and Configure an OpenLDAP Ubuntu Server](/openldap/)

Unlock the power of OpenLDAP on Ubuntu for centralized user authentication, seamless access control management, and enhanced directory services!

![](https://adamtheautomator.com/wp-content/uploads/2024/03/pipe-command-in-linux-1.jpg)

### [Unleashing the Power of the Pipe Command in Linux](/pipe-command-in-linux/)

Unlock the prowess of the pipe command in Linux to streamline tasks, boost productivity, and simplify complex operations effortlessly!

## Categories

*   [IT Ops](/category/it-ops/)
*   [Cloud](/category/cloud/)
*   [DevOps](/category/devops/)
*   [Home Ops](/category/home-ops/)
*   [Information Security](/category/infosec/)
*   [Software Development](/category/software-development/)

## Site

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Copyright 2026© ATA Learning | [Privacy Policy](/privacy/)
