Transfer Files Between Linux Machines Over SSH


Introduction

Transferring files between Linux machines over SSH is a common task for system administrators and developers. SSH (Secure Shell) is a protocol that allows you to securely transfer files between machines, as well as remotely access and manage them. In this article, we will explore different ways to transfer files over SSH and demonstrate the process with examples and commands.

Setting up SSH

Before you can transfer files over SSH, you need to set up SSH on both the source and destination machines. SSH is a secure communication protocol that allows you to remotely access and manage your Linux machines. It creates an encrypted tunnel between the two machines, protecting your data from eavesdropping and tampering.

You can check if SSH is already installed on your machine by running the following command −

$ ssh -v

If the output shows the version of SSH, it is already installed. If not, you can install it using your distribution's package manager.

$ sudo apt-get install ssh # on Ubuntu
$ sudo yum install ssh # on CentOS

Once SSH is installed, you can start the SSH service and configure it for remote access.

$ sudo service ssh start # on Ubuntu
$ sudo systemctl start ssh # on CentOS

SCP (Secure Copy)

SCP (Secure Copy) is a command-line utility that allows you to securely transfer files between machines over SSH. The syntax for SCP is similar to the regular cp command, but with the addition of the “-scp” option.

To copy a file from a local machine to a remote machine, use the following syntax −

$ scp [source file] [user]@[destination host]:[destination path]

For example, to copy a file named "file.txt" from the local machine to the remote machine with IP address "192.168.1.100" and save it in the home directory of the user "john", use the following command −

$ scp file.txt john@192.168.1.100:~/

To copy a file from a remote machine to a local machine, use the following syntax −

$ scp [user]@[source host]:[source path] [destination file]

For example, to copy a file named "file.txt" from the remote machine with IP address "192.168.1.100" and save it in the home directory of the local machine, use the following command −

$ scp john@192.168.1.100:~/file.txt ~

SCP is a great tool for quickly copying a single file between machines, but if you need to transfer multiple files or entire directories, it can be more efficient to use SFTP.

SFTP (Secure File Transfer Protocol)

Another way to transfer files over SSH is through SFTP (Secure File Transfer Protocol). SFTP is similar to FTP (File Transfer Protocol), but it uses SSH to encrypt the data transfer.

To transfer files using SFTP, you first need to open an SFTP session with the remote machine using the following command −

$ sftp [user]@[host]

For example, to open an SFTP session with the remote machine with IP address "192.168.1.100" and the user "john", use the following command −

$ sftp john@192.168.1.100

For example, to open an SFTP session with the remote machine with IP address "192.168.1.100" and the user "john", use the following command −

$ sftp john@192.168.1.100

Once connected, you can use the following SFTP commands to transfer files −

  • put [source file] [destination file] − uploads a file from the local machine to the remote machine

  • get [source file] [destination file] − downloads a file from the remote machine to the local machine

  • ls − lists the files in the current directory on the remote machine

  • lls − lists the files in the current directory on the local machine

  • cd [directory] − changes the current directory on the remote machine

  • lcd [directory] − changes the current directory on the local machine

Advance Feature

There are many advanced features you can use with SCP and SFTP for more complex file transfer scenarios. For example, you can use the ‘-p’ option with SCP to preserve the file's permissions and timestamps, or use the ‘-r’ option to copy entire directories recursively. Additionally, you can use ssh-keygen to generate a key pair, which can be used to authenticate to the remote machine without a password.

Conclusion

Transferring files between Linux machines over SSH is a secure and efficient way to manage and exchange data. SCP and SFTP are two common tools used to transfer files over SSH. Both tools are easy to use and provide a secure way to transfer data. By following the commands and examples provided in this article, you should be able to transfer files between Linux machines over SSH with ease. Additionally, you can use the advanced features mentioned above to tailor your file transfer process to suit your specific needs.

Updated on: 13-Feb-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements