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.
Methods for remote file transfer using SSH:
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.
$ 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.
$ 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)
$ scp myfile.txt myfile2.txt remoteuser@remoteserver:/remote/folder/
$ scp * remoteuser@remoteserver:/remote/folder/
$ 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.
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 $
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.
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.
Comment anonymously. Login not required.