scp

networkingLinux/Unix
The scp command is one of the most frequently used commands in Linux/Unix-like operating systems. scp The scp (Secure Copy) command is used to securely transfer files between hosts on a network. It uses SSH for data transfer and provides the same authentication and security as SSH. SCP is ideal for safely copying files across untrusted networks.

Quick Reference

Command Name:

scp

Category:

networking

Platform:

Linux/Unix

Basic Usage:

scp [options] [arguments]

Common Use Cases

  • 1

    Secure file transfer

    Transfer files securely between systems using SSH encryption

  • 2

    Remote backups

    Copy important files or backups to remote servers

  • 3

    Deployment

    Deploy code, configurations, and assets to production servers

  • 4

    Multi-server copying

    Copy files between two remote systems without downloading locally

Syntax

scp [OPTION] [[user@]src_host:]file1 ... [[user@]dst_host:]file2

Options

Option Description
-1 Use protocol version 1 (deprecated)
-2 Use protocol version 2 (default and recommended)
-4 Force use of IPv4 addresses
-6 Force use of IPv6 addresses
-B Batch mode (prevents asking for passwords or passphrases)
-C Enable compression
-c cipher Select the cipher to use for encrypting the data transfer
-F ssh_config Specify an alternative SSH configuration file
-i identity_file Specify the file from which the identity (private key) is read
-l limit Limit bandwidth in Kbit/s
-o ssh_option Pass options to ssh in the format used in ssh_config
-P port Specify the port to connect to on the remote host
-p Preserve modification times, access times, and modes from the original file
-q Quiet mode - disable progress meter and warning/diagnostic messages
-r Recursively copy entire directories
-S program Specify the path to the ssh program
-v Verbose mode - display debugging messages

Examples

How to Use These Examples

The examples below show common ways to use the scp command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

#

Basic Examples:

scp file.txt user@remote_host:/path/to/destination/

Copy a local file to a remote host.

scp user@remote_host:/path/to/file.txt local_directory/

Copy a remote file to the local system.

scp -r local_directory/ user@remote_host:/path/to/destination/

Copy an entire directory recursively to a remote host.

Advanced Examples:

scp -P 2222 file.txt user@remote_host:/path/to/destination/

Copy a file to a remote host using a non-standard SSH port (2222).

scp -C file.txt user@remote_host:/path/to/destination/

Enable compression during the transfer to speed up file transfers.

scp user1@host1:/path/file.txt user2@host2:/path/

Copy a file from one remote host to another remote host.

scp -i ~/.ssh/private_key file.txt user@remote_host:/path/to/destination/

Use a specific identity file (private key) for authentication.

scp -v file.txt user@remote_host:/path/to/destination/

Enable verbose mode to see detailed transfer information.

scp -l 1000 file.txt user@remote_host:/path/to/destination/

Limit the bandwidth used during transfer to 1000 Kbit/s.

scp -p file.txt user@remote_host:/path/to/destination/

Preserve file modification and access times, and modes (permissions).

Try It Yourself

Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

Understanding Syntax

Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

Notes

Key Points:

  • SCP (Secure Copy) utilizes SSH protocol for secure file transfers
  • It provides the same authentication and security as the SSH protocol
  • SCP can copy files between two remote hosts (not just local-to-remote or remote-to-local)
  • The recursive option (-r) is required when copying directories
  • SSH key-based authentication can be used with SCP via the -i option
  • Compression can improve transfer speed for text files but may slow down transfers of already compressed files

Common Use Cases:

  • Securely transferring configuration files between servers
  • Deploying web applications to remote servers
  • Backing up important files to a remote location
  • Sharing files between systems across the internet
  • Transferring log files for analysis
  • Moving database dumps between development and production environments

Authentication Methods:

  • Password authentication (requires interactive password entry unless using ssh-agent)
  • Public key authentication (more secure and can be automated)
  • Host-based authentication (less commonly used)
  • GSSAPI-based authentication (for Kerberos environments)

Security Considerations:

  • SCP encrypts both file data and passwords during transmission
  • Use SSH keys instead of passwords for more secure automated transfers
  • Consider using ssh-agent for managing SSH key authentication
  • Keep your SSH client and server software updated to patch security vulnerabilities
  • Use a non-standard SSH port in environments with increased security requirements
  • Disable SSH protocol version 1 (it's deprecated and less secure)

Limitations:

  • SCP lacks rsync's advanced features like delta transfers and synchronization
  • It doesn't show a progress bar by default (though -v can provide some progress information)
  • Resuming interrupted transfers is not supported
  • SCP is gradually being phased out in favor of sftp in some environments

Related Commands:

  • ssh - Secure shell protocol for remote login
  • sftp - Secure file transfer protocol over SSH
  • rsync - Fast, versatile file copying tool with more features than scp
  • cp - Copy files locally
  • sshfs - Mount remote filesystems using SSH

Common Use Cases

Secure file transfer

Transfer files securely between systems using SSH encryption

Remote backups

Copy important files or backups to remote servers

Deployment

Deploy code, configurations, and assets to production servers

Multi-server copying

Copy files between two remote systems without downloading locally

System migration

Securely transfer user data when migrating to new systems

Related Commands

rsync

rsync

View command

cp

cp

View command

sftp

sftp

View command

ssh

ssh

View command

tar

tar

View command

nc

nc

View command

curl

curl

View command

Learn By Doing

The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the scp command works in different scenarios.

$ scp
View All Commands