Secure Shell (SSH) is a cryptographic network protocol used for secure communication and remote command execution between computers. One of its most common use cases is transferring files securely over a network. In this guide, we will explore various tools and methods for transferring files over SSH, including scp, rsync, and sftp.
1. SCP (Secure Copy Protocol)
SCP is a secure and straightforward method for transferring files over SSH. It uses the same authentication and security as SSH and is available on most Unix-based systems.
- 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
Transferring files over SSH is a secure and efficient way to exchange data between computers. By using tools like scp, rsync, and sftp, you can easily transfer files over SSH while maintaining the confidentiality and integrity of your data. Familiarize yourself with these tools and their options to make the most of your file transfers in a secure environment.