A Step-by-Step Guide to Safely Copy Files on Linux with SCP
Day 15: Safely Transfer Files Using SCP
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.
options | Description |
-P | port: Specifies the port to connect on the remote host. |
-p | Preserves modification times, access times, and modes from the original file. |
-q | Disables the progress meter. |
-r | Recursively copy entire directories. |
-s | Name 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:
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.
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:
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/
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:
Copying a file from local machine to a remote machine:
scp /home/aditya/Desktop/photo.jpg aditya@192.168.1.100:/home/aditya/pictures/
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/
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.