Posted on
Getting Started

How to Securely Copy Files with `scp`

Author
  • User
    Linux Bash
    Posts by this author
    Posts by this author

How to Securely Copy Files with scp on Linux Systems

File transfers are a routine part of handling systems, particularly when you're managing resources and data across different machines. For Linux users, security during file transfers is imperative to ensure data integrity and confidentiality. One common and powerful tool used in Linux to perform secure file copying is scp (Secure Copy). This tool utilizes SSH (Secure Shell) to transfer files between hosts on a network securely.

In this blog post, we'll dive into how to use scp, and touch upon installation where necessary, across various Linux distributions using different package managers like apt for Debian/Ubuntu, dnf for Fedora/RHEL, and zypper for openSUSE.

What is scp?

scp is a command-line utility in Linux and Unix systems that allows users to securely copy files and directories between two locations over a network. Built on SSH, it encrypts both the data being transferred and the authentication credentials ensuring security at every step.

Installing scp

Generally, scp comes pre-installed with most Linux distributions as part of the OpenSSH packages. However, if it’s not installed on your system, you can easily install it using your distribution’s package manager.

For Debian/Ubuntu:

  1. First, update your package list:

    sudo apt update
    
  2. Install the OpenSSH-client package:

    sudo apt install openssh-client
    

For Fedora/RHEL/CentOS:

  1. First, check if scp is installed:

    scp --version
    
  2. If not, install it using:

    sudo dnf install openssh-clients
    

For openSUSE:

  1. Open a terminal and type:

    zypper se openssh
    
  2. Install the OpenSSH client using:

    sudo zypper install openssh
    

Using scp to Securely Copy Files

Basic syntax of scp is as follows:

scp [OPTION] [user@]SRC_HOST:]file1 [user@]DEST_HOST:]file2

Examples

1. Copying a local file to a remote server:

Suppose you want to copy a file named example.txt from your local machine to a remote machine with the IP address 192.168.1.100 and username remoteuser. The command will be:

scp example.txt remoteuser@192.168.1.100:/home/remoteuser/

You will be prompted to enter the user's password on the remote machine unless you have set up a password-less SSH login.

2. Copying a file from a remote server to the local machine:

If you need to copy example.txt from a remote server back to your local machine:

scp remoteuser@192.168.1.100:/home/remoteuser/example.txt ./local_directory/

Advanced scp Usage

  • Copying directories recursively: To copy entire directories, use the -r option:

    scp -r local_folder user@remote:/path/
    
  • Using a specific SSH key: If you're using a specific SSH key for your connection, use the -i option:

    scp -i /path/to/private_key file.txt user@remote:/path/
    
  • Limiting bandwidth usage: Useful for not saturating your network bandwidth while transferring large files, you can limit the bandwidth used by scp with the -l (limit) option, where speed is given in Kbit/s:

    scp -l 300 file.txt user@remote:/path/
    

Conclusion

The scp command is a secure, versatile, and reliable method for copying files across networked computers, leveraging SSH's security. Whether transferring logs, backups, or migration data, scp simplifies secure file sharing across different systems. By knowing how to use various options effectively, Linux admins can ensure their workflows are both efficient and secure.

Remember, while scp is mighty, always ensure proper permissions and security practices are adhered to avoid unauthorized access to sensitive files.