TransWikia.com

How to copy files from one machine to another using ssh

Unix & Linux Asked by user3021349 on November 11, 2021

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?

10 Answers

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 by Hopping Bunny on November 11, 2021

ssh user@domain "printf 'dir' | pax -w " | pax -r

Use it instead of scp. Pax standardized [1] by IEEE, and scp deprecated by lwn[2] usersg.

[1] https://man.bsd.lv/pax.1#STANDARDS [2] https://lwn.net/Articles/835962/

From someone else on "the CLI-way" ;-)

Answered by sylvainsab on November 11, 2021

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/* [email protected]:/home/

Answered by Carter on November 11, 2021

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/OtherPycharmProjects

Note 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

Answered by Sizwe on November 11, 2021

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.

  1. cd to where you want the file saved

    $ cd Documents
    
  2. Connect

    $ sftp sammy@your_server_ip_or_remote_hostname
    
  3. Go the directory that contains the file to be transferred.

    $ cd NASA/secret_files/
    
  4. Transfer

    $ get UFO_blueprint.odt
    

To get the complete directory, instead use

$ get -r secret_files/

Answered by Pe Dro on November 11, 2021

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 -S

or backup the root fs with

ssh <target> "tar cf - -C / --one-file-system -S --numeric-owner . | pigz" > root.tgz

Answered by Stuart Rothrock on November 11, 2021

If you want to keep the files on both systems in sync then have a look at the rsync program:

(see tutorial here)

Answered by Kiffin on November 11, 2021

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 by Dan Garthwaite on November 11, 2021

In case if you need an alternate approach.

Install sshfs. if you use ubuntu/debian:

sudo apt-get install sshfs

or, if you use centos/rhel:

sudo yum install fuse-sshfs

or, in macOS

brew install sshfs

Create an empty dir

mkdir /home/user/testdir

"link" or "mount" the two directories

sshfs [email protected]:/remote/dir /home/user/testdir

"unlink" the dirs

fusermount -u /home/user/testdir

On BSD and macOS, to unmount the filesystem:

umount mountpoint

or

diskutil unmount mountpoint

For more see here, linuxjournal.com libfuse/sshfs

Answered by Ruban Savvy on November 11, 2021

Syntax:

scp <source> <destination>

To copy a file from B to A while logged into B:

scp /path/to/file username@a:/path/to/destination

To copy a file from B to A while logged into A:

scp username@b:/path/to/file /path/to/destination

Answered by DopeGhoti on November 11, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP