A Step-by-Step Guide to Safely Copy Files on Linux with SCP

Aditya GadhaveAditya Gadhave
3 min read
  • scp (secure copy) command in Linux system is used to copy file(s) between servers in a secure way.

  • The SCP command or secure copy allows the secure transferring of files between the local host and the remote host or between two remote hosts

  • . It uses the same authentication and security as it is used in the Secure Shell (SSH) protocol.

  • SCP is known for its simplicity, security, and pre-installed availability.

SCP Command Syntax:

The basic syntax for SCP is:

scp [options] source_file username@remote_host:/destination_path
  • source_file: The file or directory you want to copy.

  • username@remote_host: The SSH login information of the remote machine.

  • /destination_path: The path where the file should be copied on the remote machine.

Options in scp command in Linux

Most commonly used Options in scp Command in Linux.

optionsDescription
-Pport: Specifies the port to connect on the remote host.
-pPreserves modification times, access times, and modes from the original file.
-qDisables the progress meter.
-rRecursively copy entire directories.
-sName of program to use for the encrypted connection. The program must understand ssh(1) options.

How to Securely Copy a File from a Local Machine to a Remote Machine in CentOS:

  1. Copy a Single File from Local to Remote:

    To copy a file from your local machine to a remote server, use the following command:

     scp /path/to/local/file username@remote_host:/path/to/remote/directory
    

    Example:

     scp /home/aditya/Documents/report.txt aditya@192.168.1.100:/home/aditya/backup/
    

    This will copy the file report.txt from the local machine’s /home/aditya/Documents/ directory to the remote machine's /home/aditya/backup/ directory.

  2. Copy a Directory from Local to Remote:

    Use the -r option to recursively copy an entire directory.

     scp -r /path/to/local/directory username@remote_host:/path/to/remote/directory
    

    Example:

     scp -r /home/aditya/Projects aditya@192.168.1.100:/home/aditya/backup/
    

    This will copy the entire Projects directory from the local machine to the remote machine.

Additional SCP Commands and Options:

  1. Specify SSH Port:

    If the remote server is running SSH on a port other than the default (port 22), you can specify the port using the -P option:

     scp -P port_number /path/to/local/file username@remote_host:/path/to/remote/directory
    

    Example:

     scp -P 2222 /home/aditya/Documents/report.txt aditya@192.168.1.100:/home/aditya/backup/
    
  2. Copy Files from Remote to Local:

    To copy a file from a remote server to your local machine, reverse the source and destination paths:

     scp username@remote_host:/path/to/remote/file /path/to/local/directory
    

    Example:

     scp aditya@192.168.1.100:/home/aditya/backup/report.txt /home/aditya/Documents/
    

Example SCP Use Cases:

  1. Copying a file from local machine to a remote machine:

     scp /home/aditya/Desktop/photo.jpg aditya@192.168.1.100:/home/aditya/pictures/
    
  2. Copying a file from a remote machine to the local machine:

     scp aditya@192.168.1.100:/home/aditya/pictures/photo.jpg /home/aditya/Desktop/
    
  3. Copying an entire directory:

     scp -r /home/aditya/Projects/ aditya@192.168.1.100:/home/aditya/backup/
    

Advantages Of SCP:

  • Secure, encrypted file transfers over SSH.

  • Simple to use with minimal configuration.

  • Cross-platform support and easy for small file transfers.

  • Useful for copying both files and directories.

Disadvantages Of SCP:

  • Does not support transfer resume.

  • No progress bar or advanced transfer features.

  • Slower for large file transfers due to encryption overhead.

  • Lacks verification of transferred file integrity.

Uses Of SCP:

  • Secure file transfer between local and remote systems.

  • Automated file backups and sharing.

  • Copying configuration files or application data across servers.

  • Automating deployment processes in scripts.

Conclusion:

SCP is a simple and secure tool for transferring files between local and remote machines using the SSH protocol. It is highly efficient for both single files and entire directories, and with options like bandwidth limitation, port specification, and key-based authentication, it provides a flexible way to manage file transfers in a secure manner.

0
Subscribe to my newsletter

Read articles from Aditya Gadhave directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Aditya Gadhave
Aditya Gadhave

👋 Hello! I'm Aditya Gadhave, an enthusiastic Computer Engineering Undergraduate Student. My passion for technology has led me on an exciting journey where I'm honing my skills and making meaningful contributions.