Skip to content

Understanding SCP Command in Linux, with Examples

The Secure Copy Protocol (SCP) is an essential command for securely transferring files between Linux systems. This comprehensive guide explains what SCP is, how it works, its key capabilities, and provides practical examples to help you master SCP usage.

Brief History of SCP

SCP was created as a secure file transfer alternative to older protocols like FTP and RCP (Remote Copy Protocol). It was designed to leverage the encryption, authentication, and data integrity features of the Secure Shell (SSH) protocol.

SSH was created in 1995 by Tatu Ylönen, who was looking to replace insecure terminal emulation and remote access software like Telnet and Rlogin. It enabled secure communication between systems over unsecured networks.

Building upon SSH, SCP was implemented as a more secure file transfer mechanism compared to FTP and RCP. It has since become a standard for encrypted file transfers in Linux and other UNIX-like operating systems.

What is SCP?

SCP (Secure Copy Protocol) is a command line utility used to securely transfer files and directories between remote Linux/Unix systems.

It uses the SSH protocol underneath to transfer data. Some key aspects:

  • Encrypts data during transfers for security
  • Authenticates using SSH keys or passwords
  • Maintains integrity checks via cryptographic hashes
  • Optional compression for efficient transfers over slow networks

SCP relies entirely on SSH for its secure transport functionality. It does not have built-in security itself. This allows it to securely move files between systems over vulnerable networks.

Key Capabilities of SCP

SCP has several options that make it versatile for managing file transfers:

Transfer files from local to remote system

Send files securely from your machine to another over a network.

Fetch files from remote system to local

Retrieve files securely from a remote server onto your local machine.

Transfer between two remote systems

Move files directly between two servers without having to route through your own machine.

Recursive copying

Recursively copy entire directories by using the -r option.

Preserve file attributes

Preserve timestamps, permissions, ownership details etc. using the -p option.

These capabilities make SCP suitable for file management tasks in IT environments where transferring data and files between systems is routine.

Next, we‘ll explore SCP usage in detail with examples.

Transferring Files from Local System to Remote

The basic SCP syntax to transfer a file from your local machine to a remote system is:

scp local_file username@remote_host:/remote/directory/

Breaking this down:

  • scp: the scp utility that will transfer the file securely
  • local_file: path and name of local file to transfer
  • username: username on the remote host system
  • remote_host: hostname or IP address of remote system
  • /remote/directory/: path to folder on remote system to place the file

For example, to copy file.txt to a remote server at IP 192.168.1.100, with username jdoe, into folder /home/jdoe/documents, run:

scp file.txt [email protected]:/home/jdoe/documents

You‘ll be prompted for the password of the jdoe user account on the destination system.

This securely copies file.txt onto the remote machine under /home/jdoe/documents.

Fetching Files from Remote System to Local

To transfer a file securely from a remote system to your local machine, the SCP syntax is:

scp username@remote_host:/remote/file /local/directory/
  • username: username on the remote system
  • remote_host: hostname or IP address of remote system
  • /remote/file: absolute path to file on remote system
  • /local/directory/: local folder to copy the file into

For example, to fetch the file config.conf from a server storage.acmecorp.com under remote user bob into local folder /home/alice/downloads/, you would run:

scp [email protected]:/etc/config.conf /home/alice/downloads

After entering password for [email protected], this would securely transfer config.conf into /home/alice/downloads locally.

Transferring Between Two Remote Systems

SCP can securely transfer files between two remote servers, without needing to download to your local system first.

The syntax is:

scp user1@remote1:/remote/file user2@remote2:/remote/directory/
  • user1: username on first remote system
  • remote1: hostname/IP of first remote system
  • /remote/file: path to file on first remote system
  • user2: username on second remote system
  • remote2: hostname/IP for second remote system
  • /remote/directory/: directory on second remote system to put the file

For example, to move file.txt between two systems serverA and serverB between user accounts adam and dana:

scp adam@serverA:/home/adam/file.txt dana@serverB:/var/backups

This will securely transfer file.txt from /home/adam on serverA to /var/backups on serverB.

Recursive Copying of Directories

By default, SCP does not copy directories. To recursively copy directories between systems, use the -r option:

scp -r localdir username@remotehost:/remotedir

This will copy localdir from local system to remotedir on the remote system, including all subdirectories.

For example, to recursively copy /home/project directory from local machine to /backups on remote system 192.168.10.50:

scp -r /home/project [email protected]:/backups  

Preserving File Attributes During Transfer

To preserve attributes like timestamps, permissions, ownership when copying files between systems, use the -p option:

scp -p file user@host:/remote/dir/

This will copy file to the remote host, replicating the original file permissions, last modified time, etc. on the transferred copy.

For instance, to copy script.sh to remote system and retain its Unix permissions:

scp -p script.sh admin@webserver:/home/admin/scripts

Now script.sh exists on the remote machine mirroring the original file attributes.

Understanding SCP: Key Takeaways

  • SCP allows secure transfer of files between Linux/Unix machines over SSH
  • Essential for file management and movement across systems
  • Encrypts transfers and authenticates using SSH keys/paswords
  • Can transfer between local->remote, remote->local , remote->remote
  • Recursive copy directories, retain file attributes, compress data
  • Used by sysadmins, developers, tech professionals routinely

With this deep understanding of SCP, you can securely manage your file transfer operations. Consistent practice helps build expertise.

Summary Table of SCP Functions

Function Description
Local to Remote Transfer Copy files from local machine to remote system
Remote to Local Transfer Retrieve files securely from a remote system onto local machine
Remote to Remote Transfer Move files directly between two remote servers
Recursive Copy -r option copies directories recursively
Preserve Attributes -p option retains permissions, ownership, timestamps etc.

Frequently Asked Questions

Q: How does SCP differ from FTP or RCP?

A: SCP uses SSH encryption for all transfers making it much more secured than FTP or RCP. It also leverages SSH authentication.

Q: Where is SCP natively available?

A: SCP comes bundled with SSH implementation in Linux/UNIX based systems. Just install an SSH server, and you can use SCP.

Q: Can SCP transfer whole directories?

A: Yes, use the recursive -r option to securely copy entire directory structures between systems.

Q: Does SCP work on Windows?

A: SCP isn‘t natively available on Windows. But after installing an SSH server on Windows, SCP clients can connect to transfer files.