SSH (Secure Shell) is a protocol widely used to access and manage remote systems securely. One of its key features is the ability to transfer files between systems using various tools like scp, sftp, and rsync. These tools allow users to securely copy files from local to remote systems and vice versa, ensuring data integrity and privacy during transmission.

Each method of file transfer using SSH has its own use cases. scp is ideal for simple file transfers, sftp offers an interactive file management experience, and rsync excels in synchronizing directories efficiently. Understanding how to use these tools effectively helps in managing files across different systems securely.

Examples for the following file transfer methods are based on the above setup.

Make sure you have SSH access to the remote server with adequate permission to the remote files and folders.

Transfer file using scp

scp (Secure Copy) is a straightforward command-line tool that allows you to securely transfer files between local and remote systems over SSH. It is easy to use and resembles the cp command in Linux, but it works across networked systems. This makes it ideal for users who need a simple, no-frills way to copy files to and from remote servers. Because scp is based on the SSH protocol, it benefits from the same security features, ensuring that your data is encrypted during transfer.

Although scp is simple and effective for transferring files, it is not as flexible or efficient as other methods like rsync. For example, scp does not automatically skip files that already exist or are up-to-date on the destination, which can lead to longer transfer times. However, when you need to quickly transfer files with minimal setup, scp is a reliable choice. It is especially useful for copying a few files or directories without worrying about advanced synchronization or interactive file management.

  1. Ensure you have SSH access to the remote server with the appropriate permissions.
  2. Use scp to transfer a single file from your local system to the remote server.
    $ scp /home/localuser/localfolder/subdir1/file1.txt remoteuser@remoteserver:/home/remoteuser/remotefolder/subdir1/
  3. Transfer a file from the remote server to your local machine using scp.
    $ scp remoteuser@remoteserver:/home/remoteuser/remotefolder/subdir1/file1.txt /home/localuser/localfolder/subdir1/
  4. Use scp to copy multiple files from your local system to the remote server.
    $ scp /home/localuser/localfolder/subdir1/file1.txt /home/localuser/localfolder/subdir2/file2.txt remoteuser@remoteserver:/home/remoteuser/remotefolder/
  5. Transfer entire directories and their contents by using the -r option in scp.
    $ scp -r /home/localuser/localfolder/ remoteuser@remoteserver:/home/remoteuser/remotefolder/

GUI programs such as WinSCP can also be used to transfer files between local and remote host using scp methods.

Transfer file using sftp

sftp (Secure File Transfer Protocol) provides a more interactive way to transfer files between systems over SSH. Unlike scp, which is used for one-off file transfers, sftp opens an interactive session that allows you to navigate the remote filesystem, list directory contents, and transfer files back and forth. This makes sftp a better option when you need to explore remote directories or manage multiple files interactively.

While sftp is slightly more complex to use than scp, it offers greater flexibility in file management. You can navigate the remote filesystem just as you would your local one, making it easier to find and transfer files. sftp also supports batch transfers and allows you to resume interrupted transfers, which adds to its utility. However, it may not be as fast as scp or rsync when transferring large sets of files, especially if you don't need the interactive features.

  1. Start an sftp session by connecting to the remote server.
    $ sftp remoteuser@remoteserver
  2. Use sftp commands like ls and cd to navigate directories on the remote server.
    sftp> ls /home/remoteuser/remotefolder/
    subdir1/  subdir2/
  3. Transfer a file from the remote server to your local system using the get command.
    sftp> get /home/remoteuser/remotefolder/subdir1/file1.txt /home/localuser/localfolder/subdir1/
  4. Upload a file from your local machine to the remote server using the put command.
    sftp> put /home/localuser/localfolder/subdir1/file1.txt /home/remoteuser/remotefolder/subdir1/
  5. Close the sftp session once the file transfers are complete.
    sftp> bye

Transfer file using rsync

rsync is a robust tool for synchronizing files and directories between local and remote systems over SSH. Unlike scp and sftp, rsync is designed to efficiently synchronize data by transferring only the differences between the source and destination. This makes it particularly useful for backing up or mirroring large datasets, as it minimizes the amount of data sent over the network.

In comparison to scp and sftp, rsync is more complex but offers significant advantages in terms of speed and efficiency, especially for repetitive tasks. rsync can also preserve file permissions, timestamps, and symbolic links, making it ideal for comprehensive backups. Additionally, rsync can handle large volumes of data and works well for keeping directories in sync across multiple systems, making it a powerful choice for users with more advanced file transfer needs.

If remote shell options (-e)aren't specified, rsync will first attempt to connect to rsyncd. If rsyncd isn't running on the remote system, it will eventually revert to SSH.

  1. Verify that you have SSH access to the remote server with the necessary permissions.
  2. Use rsync to synchronize a local directory with a remote directory.
    $ rsync -av --delete -e ssh /home/localuser/localfolder/ remoteuser@remoteserver:/home/remoteuser/remotefolder/
  3. Transfer files from a remote directory to a local directory using rsync.
    $ rsync -av --delete -e ssh remoteuser@remoteserver:/home/remoteuser/remotefolder/ /home/localuser/localfolder/
  4. Use rsync to copy only files that are new or have changed.
    $ rsync -av --update -e ssh /home/localuser/localfolder/ remoteuser@remoteserver:/home/remoteuser/remotefolder/
  5. Specify patterns to include or exclude files during transfer with rsync.
    $ rsync -av --exclude '*.log' -e ssh /home/localuser/localfolder/ remoteuser@remoteserver:/home/remoteuser/remotefolder/

Mount remote filesystem locally

You can mount remote filesystems on your local host and access them as if they were local. To do this, you'll need SSH access to the remote host and the sshfs utility.

Discuss the article:

Comment anonymously. Login not required.