Copying Files via SSH
SSH (Secure Shell) is a widely used protocol for securely accessing and managing remote servers over an encrypted connection. One of the most common tasks when working with SSH is copying files between your local machine and a remote server or between two remote servers.
In this article, we’ll explore different ways to copy files via SSH, including using SCP (Secure Copy Protocol), rsync, and other methods.
Why Use SSH for File Transfers?
Using SSH to transfer files offers several advantages:
- Security: SSH encrypts both the command and the data during transmission, ensuring that sensitive information is protected.
- Efficiency: SSH is fast and reliable, allowing you to transfer files directly between systems without needing to log in separately.
- Automation: SSH can be integrated into scripts, enabling automated file transfers between servers or local machines.
1. Copying Files with SCP (Secure Copy Protocol)
SCP is one of the most popular tools for copying files securely via SSH. It allows you to transfer files from a local machine to a remote server, from a remote server to your local machine, or between two remote servers.
Basic SCP Syntax:
Example 1: Copy a File from Local Machine to Remote Server
In this example:
- /path/to/local/file is the path to the file on your local machine.
- username@remote_host is the SSH user and hostname or IP address of the remote server.
- /path/to/remote/destination is the path where the file will be stored on the remote server.
Example 2: Copy a File from Remote Server to Local Machine
In this case, the file is transferred from the remote server to the local machine.
Example 3: Copy a Directory Recursively
To copy an entire directory, use the -r (recursive) option:
This will transfer the directory and all of its contents to the remote server.
Useful SCP Options:
- -P [port]: Specify the SSH port if it’s different from the default (22).
- -C: Enable compression during transfer for faster file copying.
- -i [identity_file]: Use a specific SSH private key for authentication.
2. Copying Files with rsync
rsync is another powerful tool for file transfer over SSH. It’s often preferred for large or repetitive file transfers because it only copies the changes between source and destination, saving time and bandwidth.
Basic rsync Syntax:
Example 1: Copy a File from Local Machine to Remote Server
Example 2: Copy a Directory with rsync
Useful rsync Options:
- -a: Archive mode, which preserves symbolic links, permissions, timestamps, and file ownership.
- -v: Verbose mode to display progress during transfer.
- -z: Enable compression to speed up the transfer.
- –delete: Remove files in the destination that no longer exist in the source.
- -e “ssh -p [port]”: Specify a custom SSH port.
3. Copying Files Between Two Remote Servers
Using SSH, you can also copy files directly between two remote servers. Both SCP and rsync support this.
Example 1: Copy Files Between Two Remote Servers with SCP
Example 2: Copy Files Between Two Remote Servers with rsync
This is particularly useful for transferring files between servers without having to download them to your local machine first.
4. Copying Files with SFTP (SSH File Transfer Protocol)
SFTP is another method for securely transferring files over SSH. It’s similar to FTP but uses SSH to provide encryption. SFTP is typically used in interactive sessions but can also be automated.
Using SFTP:
To start an SFTP session, run the following command:
Once inside the SFTP prompt, you can use commands such as:
- put /path/to/local/file /path/to/remote/destination: Upload a file from your local machine to the remote server.
- get /path/to/remote/file /path/to/local/destination: Download a file from the remote server to your local machine.
- mput and mget: To upload or download multiple files at once.
To exit the SFTP session, type exit or quit.
5. Automating File Transfers with SSH Keys
For repeated file transfers, it’s a good idea to set up SSH key-based authentication so you don’t need to enter your password every time.
Step 1: Generate SSH Key Pair (If You Don’t Have One)
On your local machine, run:
This will generate a public and private key pair.
Step 2: Copy Public Key to Remote Server
Use the following command to copy your public key to the remote server:
Now, you can log in and transfer files without needing to enter a password, making automation easier.
Conclusion
Copying files via SSH is a secure and efficient way to transfer data between your local machine and a remote server or between two remote servers. Whether you’re using SCP, rsync, or SFTP, SSH offers encrypted file transfers that protect your data from being intercepted. Tools like SCP are simple for one-time transfers, while rsync is ideal for ongoing synchronization tasks, especially for large files or directories. Understanding these tools will help you manage file transfers more effectively, especially in a remote server environment.