Secure Shell (SSH) is a cryptographic network protocol used for secure communication and remote command execution between computers. Copying files between two computers is a very common task for users, but the important think is that transferring files over public network should be secured. Encrypting the data is most suitable way to keep data safe from unauthorized access. SSH ensures this by encrypting the data during transfer, making it unreadable to anyone who tries to intercept it. This means your files are protected, even when shared over public networks.
In this guide, we will discuss 3 easy methods to transfer files over SSH. These methods will help you securely move your files between computers without any risk of unauthorized access.
1. SCP (Secure Copy Protocol)
SCP is a secure and straightforward method for transferring files between computers using SSH. It is simple and best for the quick file transfers. It encrypts the data during the transfer using same encryption method as SSH, which ensure your files remain private and protected.
- Copy a file from a local machine to a remote machine:
scp /path/to/local/file username@remote_host:/path/to/remote/directory
- Copy a file from a remote machine to a local machine:
scp username@remote_host:/path/to/remote/file /path/to/local/directory
- Copy a directory from a local machine to a remote machine:
scp -r /path/to/local/directory username@remote_host:/path/to/remote/directory
- Copy a directory from a remote machine to a local machine:
scp -r username@remote_host:/path/to/remote/directory /path/to/local/directory
- Copy files with a specific extension (e.g., .txt) from a remote machine to a local machine:
scp username@remote_host:'/path/to/remote/directory/*.txt' /path/to/local/directory
2. Rsync Over SSH
Rsync is a powerful and versatile file synchronization tool that can transfer files over SSH. It is particularly useful when transferring large files or directories, as it can compress and transfer only the changes between the source and destination.
- Sync a local directory with a remote directory:
rsync -avz /path/to/local/directory username@remote_host:/path/to/remote/directory
- Sync a remote directory with a local directory:
rsync -avz username@remote_host:/path/to/remote/directory /path/to/local/directory
- Transfer only new or modified files between local and remote directories:
rsync -avzu /path/to/local/directory username@remote_host:/path/to/remote/directory
- Sync a local directory with a remote directory and delete files on the remote side that are not present in the local directory:
rsync -avz --delete /path/to/local/directory username@remote_host:/path/to/remote/directory
- Transfer files with a specific extension (e.g., .txt) from a local directory to a remote directory:
rsync -avz --include='*.txt' --exclude='*' /path/to/local/directory username@remote_host:/path/to/remote/directory
3. SFTP (SSH File Transfer Protocol)
SFTP (SSH File Transfer Protocol) is a secure and reliable protocol for transferring files over SSH. It provides an interactive, command-line interface similar to FTP, allowing you to navigate and manipulate remote file systems securely. Here are some practical examples of using SFTP for transferring files between local and remote systems:
- Connecting to a remote system using SFTP:
To start an SFTP session, run the following command:
sftp username@remote_host
Enter your password when prompted. Once connected, you will be in the remote system’s home directory.
- Navigating directories:
Use the following commands to navigate directories in the remote system:
- To list the contents of the current directory, run:
ls
- To change the current directory, run:
cd /path/to/directory
- To show the current working directory, run:
pwd
- To list the contents of the current directory, run:
- Transferring files:
- Upload a file from the local machine to the remote machine:
put /path/to/local/file /path/to/remote/directory
- Download a file from the remote machine to the local machine:
get /path/to/remote/file /path/to/local/directory
- Upload a directory from the local machine to the remote machine:
put -r /path/to/local/directory /path/to/remote/directory
- Download a directory from the remote machine to the local machine:
get -r /path/to/remote/directory /path/to/local/directory
- Upload a file from the local machine to the remote machine:
- Manipulating files and directories:
- To create a new directory on the remote system, run:
mkdir /path/to/new/directory
- To delete a file on the remote system, run:
rm /path/to/remote/file
- To delete a directory on the remote system, run:
rmdir /path/to/remote/directory
- To rename a file or directory on the remote system, run:
rename /path/to/old/name /path/to/new/name
- To create a new directory on the remote system, run:
- Exiting the SFTP session:
To exit the SFTP session and disconnect from the remote system, run:
exit
Conclusion
In this tutorial, you have learned about three methods (SCP, Rsync, and SFTP) to copy files securely over SSH. Each method has its own benefits and use cases. SCP is simple and great for quick transfers, Rsync is powerful for syncing and updating files, and SFTP provides an interactive way to manage and transfer files. By using these tools, you can ensure your data is protected and transferred efficiently, even over public networks. These methods are essential for anyone working with remote servers or needing to transfer sensitive information securely.