How to Securely Copy Files Using SCP in Ubuntu

This guide provides a straightforward tutorial on using the Secure Copy Protocol (SCP) to securely transfer files and directories from an Ubuntu Linux client to a remote SSH server. You will learn the essential command syntax, how to copy single files or entire folders, and how to specify custom SSH ports.

Prerequisites

Before you begin, ensure you have: * An Ubuntu client machine with network access. * The IP address or domain name of the remote SSH server. * Valid login credentials (username and password or SSH key) for the remote server.

Basic SCP Command Syntax

The standard syntax for copying a local file to a remote server using SCP is as follows:

scp /path/to/local/file username@remote_host:/path/to/remote/destination

Step 1: Copying a Single File

To copy a single file from your local Ubuntu machine to a remote directory, open your terminal and run the following command. Replace the placeholder values with your actual details:

scp report.pdf ubuntu@192.168.1.50:/home/ubuntu/documents/

In this example: * report.pdf is the file on your local machine. * ubuntu is the username on the remote server. * 192.168.1.50 is the IP address of the remote server. * /home/ubuntu/documents/ is the path on the remote server where the file will be saved.

Upon running the command, you will be prompted to enter the remote user’s password (or your SSH key passphrase) to complete the transfer.

Step 2: Copying an Entire Directory (Folder)

To copy an entire directory and all of its contents, you must include the recursive flag -r in your command:

scp -r /home/user/projects ubuntu@192.168.1.50:/home/ubuntu/backup/

This command securely uploads the local projects folder and its contents to the remote /home/ubuntu/backup/ directory.

Step 3: Copying Files Using a Non-Standard SSH Port

If your remote SSH server is configured to use a custom port instead of the default port 22, you must specify the port number using the uppercase -P option:

scp -P 2222 report.pdf ubuntu@192.168.1.50:/home/ubuntu/documents/

Note: Always ensure the -P option is placed before the source file path in the command.