Skip to content

Secure File Transfer with scp in Linux

Summary

The scp command provides a secure way to copy files and directories between a local and remote system, or between two remote systems.

Introduction

The scp (Secure Copy) command is a powerful tool in Linux for securely transferring files and directories over a network. It utilizes SSH (Secure Shell) for encryption, ensuring that your data remains protected during transmission. This post will cover the basic usage and common flags of scp.

  • scp (Secure Copy):
    • Purpose: Securely copies files and directories between hosts.
    • Basic Usage: scp [options] source destination

Use Cases and Examples

Use Cases and Examples

Copying a local file to a remote server

scp localfile.txt user@remote_host:/remote/directory/
This command copies localfile.txt to the /remote/directory/ on remote_host as the user user.

Copying a remote file to a local machine

scp user@remote_host:/remote/directory/remotefile.txt /local/directory/
This command copies remotefile.txt from the remote server to the /local/directory/ on your local machine.

Copying a local directory to a remote server recursively

scp -r local_directory user@remote_host:/remote/directory/
The -r flag copies the entire local_directory and its contents recursively.

Copying a file from one remote server to another remote server

scp user1@remote_host1:/path/file.txt user2@remote_host2:/path/
This copies file.txt from remote_host1 to remote_host2. You may be prompted for passwords on both remote servers.

Most Used Flags

Flag Description Example
-r Recursive copy (for directories). scp -r local_dir user@remote:/remote/dir/
-P Specifies the SSH port number. scp -P 2222 file.txt user@remote:/remote/dir/
-p Preserves modification times, access times, and modes from the original file. scp -p file.txt user@remote:/remote/dir/
-q Quiet mode (suppresses progress indicators and warning messages). scp -q file.txt user@remote:/remote/dir/
-C Enables compression. scp -C file.txt user@remote:/remote/dir/
-i Specifies the identity file (private key). scp -i /path/to/private_key file.txt user@remote:/remote/dir/


Share on Share on

Comments