SSH or Secure Shell is a protocol that allows secure access to remote computers. SSH also comes with scp utility for transferring file between remote computers. It uses the SSH protocol and has the syntax almost similar to the cp command.
Other file transfer applications such as sftp and rsync can also utilize SSH to secure their file transfers. These applications allow us to copy our files from local to remote servers and to copy files from remote servers to our local machine.
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:
Transfer file using scp
The easiest of these is scp or secure copy. While cp is for copying local files, scp is for remote file transfer. The main difference between cp and scp is that, you'll have to specify the remote host's DNS name or IP address and provide a login credential for the command to work when using scp. You can scp files from local to remote and from remote to local.
Copy single file from local to remote using 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.
scp from remote to local using a single file .
$ scp remoteuser@remoteserver:/remote/folder/remotefile.txt localfile.txtUsing . as the copy target (replacing localfile.txt will copy the remote file to the current working directory using the same filename (remotefile.txt)
Copy multiple files from local to remote using scp.
$ scp myfile.txt myfile2.txt remoteuser@remoteserver:/remote/folder/Copy all files from local to remote using scp.
$ scp * remoteuser@remoteserver:/remote/folder/Copy all files and folders recursively from local to remote using 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
sftp or Secure FTP, on the other hand works almost exactly like ftp but with a secure connection. Most of the commands are similar and can be used interchangeably. The following sftp example will work exactly as ftp would.
$ sftp 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
You can also use ssh to secure your rsync session. To do this, use --rsh=ssh or -e “ssh” with your normal rsync commands. The following 2 commands will work exactly the same;
$ rsync -av --delete --rsh=ssh /path/to/source remote:/remote/folder/ $ rsync -av --delete -e "ssh" /path/to/source remote:/remote/folder/If these options are not specified, rsync will first try to connect to rsyncd but will automatically fall back to SSH if rsyncd is not running in the remote system.
Mount remote filesystem locally
Remote filesystems could be mounted to the local host and accessed as a local filesystem. Mounting remote filesystem requires SSH access to the remote host and using sshfs.
Discuss the article:
Comment anonymously. Login not required.
I'm using Linux (centos) machine, I already connected to the other system using ssh. Now my question is how can I copy files from one system to another system?
Suppose, in my environment, I have two system like System A and System B. I'm using System A machine and some other using System B machine.
How can I copy a file from System B to System A? And, copy a file from System A to System B?
asked Dec 24, 2013 at 9:43
6
Syntax:
scp <source> <destination>To copy a file from B to A while logged into B:
scp /path/to/file username@a:/path/to/destinationTo copy a file from B to A while logged into A:
scp username@b:/path/to/file /path/to/destination
HalosGhost
4,62410 gold badges31 silver badges40 bronze badges
answered Dec 24, 2013 at 9:48
DopeGhotiDopeGhoti
69.5k8 gold badges93 silver badges130 bronze badges
22
In case if you need an alternate approach.
Install sshfs. if you use ubuntu/debian:
sudo apt-get install sshfsor, if you use centos/rhel:
sudo yum install fuse-sshfsor, in macOS
brew install sshfsCreate an empty dir
mkdir /home/user/testdir"link" or "mount" the two directories
sshfs :/remote/dir /home/user/testdir"unlink" the dirs
fusermount -u /home/user/testdirOn BSD and macOS, to unmount the filesystem:
umount mountpointor
diskutil unmount mountpointFor more see here, linuxjournal.com libfuse/sshfs
Rakib Fiha
5623 gold badges9 silver badges23 bronze badges
answered Dec 24, 2013 at 9:58
Ruban SavvyRuban Savvy
8,0817 gold badges28 silver badges43 bronze badges
8
Sometimes you need to get fancy with tar:
tar -C / -cf - \ opt/widget etc/widget etc/cron.d/widget etc/init.d/widget \ --exclude=opt/widget/local.conf | ssh otherhost tar -C / -xvf -answered Dec 24, 2013 at 15:17
Dan GarthwaiteDan Garthwaite
7,3061 gold badge14 silver badges9 bronze badges
7
If you want to keep the files on both systems in sync then have a look at the rsync program:
(see tutorial here)
answered Dec 24, 2013 at 21:30
KiffinKiffin
4683 silver badges5 bronze badges
1
If they are running SCP / SSH on a different port, make sure you specify the uppercase -P port option.
Example:
scp -P 7121 /users/admin/downloads/* :/home/answered Feb 6, 2021 at 20:13
CarterCarter
3012 silver badges3 bronze badges
2
A simpler method that works with via SSH controlled NVIDIA Jetson computers is to connect with SFTP (SSH File Transfer Protocol).
Suppose I wish to move the document UFO_blueprint.odt from NASA's remote servers and move it into my Documents.
cd to where you want the file saved
$ cd DocumentsConnect
$ sftp sammy@your_server_ip_or_remote_hostnameGo the directory that contains the file to be transferred.
$ cd NASA/secret_files/Transfer
$ get UFO_blueprint.odt
To get the complete directory, instead use
$ get -r secret_files/
AdminBee
19.4k16 gold badges43 silver badges67 bronze badges
answered Oct 1, 2020 at 3:53
Pe DroPe Dro
1,1501 gold badge6 silver badges13 bronze badges
2
Unix/Linux recursive copy of files and folders over a secure shell using the code following:
scp -r <host@machine_name:from_remote_directory_path> <to_local_directory_path>A full example might look like:
scp -r sizwe@hprobook:./home/sizwe/PycharmProjects ./home/immaculate/OtherPycharmProjectsNote if you do not have a DNS server to resolve hostnames, use your IP address instead. The example above might look like this
scp -r 192.168.43.167:./home/sizwe/PycharmProjects ./home/immaculate/OtherPycharmProjects
AdminBee
19.4k16 gold badges43 silver badges67 bronze badges
answered Nov 9, 2020 at 11:32
SizweSizwe
711 silver badge1 bronze badge
If speed (and not security) is your priority, then check out netcat. Before I start, let me warn you that this should be used only in a trusted network because netcat transfers are not encrypted.
First, on the receiving side, run this:
nc -lp 22222 >FileName
Then on the sending side:
nc -w3 <receiver IP or hostname> 22222 <FileName
Notes:
- -l: Listen mode
- -p: Port number to listen on (I picked an arbitrary port)
- -w: Connect timeout
answered Apr 28, 2021 at 7:14
If you have one of the common OSes, you can install pigz that is the same as gzip except that it will use multiple cores and be quicker over a fast network. My command is a bit different.
tar cf - -C /opt -S <dir> | pigz | ssh <target> "pigz -d | tar xf - -C /opt -S"or the other way to get files
ssh <target> "tar cf - -C /opt -S <dir> | pigz" | pigz -d | tar xf - -C /opt -Sor backup the root fs with
ssh <target> "tar cf - -C / --one-file-system -S --numeric-owner . | pigz" > root.tgzanswered Aug 4, 2020 at 19:09
1