scp, The Quick and Basic Guide

by | Jun 4, 2024

SCP (Secure Copy Protocol) is a command-line tool in Linux that allows you to securely transfer files and directories between computers over a network. Here’s a quick and basic guide on how to use SCP.

1. Basic Syntax

The basic syntax for using SCP is:

scp [options] source_file destination_file

Where ‘source_file’ is the path to the file you want to copy and ‘destination_file’ is the path where you want to copy the file.

2. Copying a File from Local to Remote

To copy a file from your local machine to a remote server:

scp /home/user/file.txt user@192.168.1.10:/home/user/
3. Copying a File from Remote to Local To copy a file from a remote server to your local machine:
scp user@192.168.1.10:/home/user/file.txt /home/user/

4. Copying a Directory Recursively

To copy an entire directory and its contents, use the -r option:

scp -r /home/user/myfolder user@192.168.1.10:/home/user/
5. Remote-to-Remote Transfer To transfer files between two remote servers, you typically need to execute an SCP command on one of the remote servers. However, this can be done via your local machine using the following syntax:
scp username1@remote_host1:/path/to/remote/file username2@remote_host2:/path/to/remote/directory

Alternatively, you can also use the -3 option with SCP on your local machine to allow the local machine to serve as an intermediary:

scp -3 username1@remote_host1:/path/to/remote/file username2@remote_host2:/path/to/remote/directory
Transfer a Directory from One Remote Server to Another Remote Server:
scp -3 -r user1@192.168.1.10:/home/user1/myfolder user2@192.168.1.20:/home/user2/

Conclusion:

SCP is a straightforward and secure way to transfer files between machines over a network. With these basic commands, you can easily copy files and directories to and from remote servers. Happy copying!