Secure Shell (SSH) is a versatile protocol that enables secure access to remote computers. With its scp utility, you can transfer files between remote systems efficiently, using a syntax similar to the cp command.

Additionally, SSH can be utilized by other file transfer applications like sftp and rsync to ensure secure transfers.

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 the simplest method for transferring files remotely. It requires SSH access to the remote server and operates like the cp command but for remote transfers. When using scp, you must specify the remote host's DNS name or IP address and provide login credentials. You can use scp for local-to-remote and remote-to-local transfers.

  1. Copy a single file from local to remote with scp.
    $ scp myfile.txt remoteuser@remoteserver:/remote/folder/

    If the target folder (/remote/folder/) is not specified, it will copy the file to the remote user's home directory.

  2. Transfer a single file from remote to local using scp.
    $ scp remoteuser@remoteserver:/remote/folder/remotefile.txt  localfile.txt

    Using . as the copy target (replacing localfile.txt will copy the remote file to the current working directory using the same filename (remotefile.txt)

  3. Copy multiple files from local to remote with scp.
    $ scp myfile.txt myfile2.txt remoteuser@remoteserver:/remote/folder/
  4. Copy all files from local to remote using scp.
    $ scp * remoteuser@remoteserver:/remote/folder/
  5. Copy files and folders recursively from local to remote with scp.
    $ scp -r * remoteuser@remoteserver:/remote/folder/

    remoteuser need to exist and have write permission to /remote/folder/ in the remote system.

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

Transfer file using sftp

Secure FTP (sftp) functions similarly to FTP, but with a secure connection. Most commands are interchangeable between the two. The following sftp examples demonstrate the similarities to standard FTP commands:

$ sftp user@192.168.1.10
Connected to 192.168.1.10.
sftp> dir
file1      file2  file3   
sftp> pwd
Remote working directory: /home/user
sftp> get file2
Fetching /home/user/file2 to file2
/home/user/file2                         100% 3740KB 747.9KB/s   00:05    
sftp> bye
$ 

WinSCP can also be used for file transfer using SFTP protocol, but with graphical interface. The other popular tool is FileZilla.

Transfer file using rsync

To secure your rsync sessions with SSH, use --rsh=ssh or -e ssh alongside your usual rsync commands. The two commands below produce the same results:

$ rsync -av --delete --rsh=ssh /path/to/source remoteuser@192.168.1.10:/remote/folder/
$ rsync -av --delete -e "ssh" /path/to/source remoteuser@192.168.1.10:/remote/folder/

If these options aren't specified, rsync will first attempt to connect to rsyncd. If rsyncd isn't running on the remote system, it will automatically revert to SSH.

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.