SCP (Secure Copy) is a command-line utility in Unix-like operating systems that enables secure and efficient file transfers between a local and remote host or between two remote hosts. It is built on top of the SSH protocol, ensuring data confidentiality and integrity during the transfer process. SCP is widely used for transferring files and directories in a secure manner.
Basic Syntax:
scp [options] source destination
Common Options:
- -r: Recursively copy directories and their contents.
- -p: Preserve modification times, access times, and modes from the original files.
- -P port: Specify a different port for SSH. Default is 22.
- -i identity_file: Use an identity file (private key) for authentication.
Some common usecases
Copying a File from Local to Remote:
scp file.txt user@remotehost:/path/to/destination/
This copies the local file file.txt to the remote server at the specified path.
Copying a File from Remote to Local:
scp user@remotehost:/path/to/source/file.txt /local/destination/
This copies the remote file file.txt to the local machine at the specified path.
Copying a Directory Recursively:
scp -r directory/ user@remotehost:/path/to/destination/
The -r option allows you to copy the entire directory and its contents.
Copying with a Different Port:
scp -P 2222 file.txt user@remotehost:/path/to/destination/
This command specifies a non-default SSH port (2222) for the transfer.
Preserving File Metadata:
scp -p file.txt user@remotehost:/path/to/destination/
The -p option maintains the original modification times and permissions of the file.
Copying Between Two Remote Hosts:
scp user1@remotehost1:/file.txt user2@remotehost2:/path/to/destination/
This copies a file from one remote host to another.
Using Identity File for Authentication:
scp -i ~/.ssh/my_private_key.pem file.txt user@remotehost:/path/to/destination/
The -i option specifies the private key for SSH authentication.
Comparison with Other Tools:
SCP vs. rsync : SCP is suitable for simple one-time transfers, while rsync is more versatile for syncing and incremental transfers.
Conclusion:
The SCP command provides a secure and efficient way to transfer files and directories between local and remote hosts. Its use cases include simple file transfers, recursive directory transfers, specifying custom SSH ports, preserving file metadata, and more. By leveraging the SSH protocol, SCP ensures data security and integrity during the transfer process.
Top comments (0)