1271

I am using a Linux (CentOS) machine, and I have already connected to another 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?

6
  • 1
    I find scp to be a cumber stone often. If it could suite your needs, try this out linuxjournal.com/article/8904 It mounts the remote file system locally. Dec 24, 2013 at 9:57
  • 64
    @Alan I think you mean "cumbersome"? Cumber Stone is a Magic card ;)
    – Izkata
    Dec 24, 2013 at 14:47
  • 3
    somewhat related: make an encrypted archive of local dir/ on remote machine using ssh: tar -c dir/ | gzip | gpg -c | ssh user@remote 'dd of=dir.tar.gz.gpg'
    – jfs
    Dec 24, 2013 at 20:49
  • 2
    If you has access of the ftp of the remote server, we can also use wget to download like $wget -r --level=9 --no-parent --reject "index.html*" ftp://<USERID>:<PASSWORD>@<MACHINE-NAME>/path/to Reference1 Reference2 Jan 19, 2015 at 6:22
  • 3
    Not duplicate, linked question specifically asks not to use scp; for that one scp is fine and most easy way as answer, so mods please remove duplicate flag as if somebody will go to other one, most useful answer will be missed out. Oct 11, 2019 at 12:13

13 Answers 13

1714

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
27
  • 196
    To clarify, you typically don't use scp to copy a file to or from your local machine (System A) while logged in to a remote server (System B) with ssh. scp will log you into the remote server, copy the file, then log you out again in one process, so just run it from a shell on your local machine. That being said, you can use scp if you're logged into System B via SSH and want to copy files between System B and System C. Dec 24, 2013 at 16:40
  • 8
    @DopeGhoti. Yes, you can't move files between two remote computers. Either the source or destination must be a local file. However, if you log in to a remote machine with ssh, you can copy files between two remote machines on that machine's command-line.
    – Gee-Bee
    Aug 8, 2014 at 19:48
  • 9
    Also, an important thing to remember is that you can only scp to a folder on the target machine to which you have permissions. If you are trying to copy it to a destination to which the target user does not have permission, first copy the file to the user's home directory or sub directory and then ssh into the target machine and sudo move it over to the final destination
    – shaveenk
    Apr 25, 2015 at 20:30
  • 23
    Remember -r (recursive) and -p (preserve system metadata): scp -rp <file1> <file2>
    – Rutrus
    Jul 1, 2015 at 7:23
  • 21
    Yes, use -P to specify the TCP port on the remote host. This catches me every time, because ssh uses -p.
    – DopeGhoti
    Apr 28, 2016 at 18:22
183

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

6
  • If I understand correctly, this gives ACCESS to the remote files, but does not COPY them? Apr 20, 2016 at 2:46
  • 3
    @ToolmakerSteve you're perfectly right: it creates a fake directory (mount), so that you can use all your preferred tools to copy or move things: cp, mv, rm, rsync, unison... The scp command would only be able to copy files.
    – Yvan
    Oct 19, 2016 at 5:38
  • Note: [1] If get error Transport endpoint is not connected after ctrl+c cancel and retry, then simply do fusermount -u /home/user/test, ref: stackoverflow.com/a/29400722/1074998 [2] In case you already in the mount directory, you may need to cd .. and re-enter the directory after mount to see the files.
    – 林果皞
    Jan 4, 2018 at 9:15
  • comment to myself: if umount doesn't work: umount -f
    – Duda
    Oct 14, 2021 at 15:04
  • Adding "fuse" to a system is not something I would do without thinking trough. It's quiet a big dependency to just copy files from a server.
    – gagarine
    Mar 23, 2022 at 13:19
78

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 -
5
  • 5
    Can you tell us what this does? For example I have no idea what 'cron' has to do with copying file!
    – aliqandil
    Oct 28, 2016 at 15:00
  • 9
    @aliqandil opt/widget etc/widget etc/cron.d/widget etc/init.d/widget are individual files to be included in the tar archive. The point was to illustrate why sometimes you need to get fancy. This series of commands streams the tar archive over an ssh connection and unarchives it on the other side. Thus no tar archive is ever written to disk. I use this sometimes to cherry pick files to copy in one command. Oct 28, 2016 at 18:51
  • Ow! I taught the first '/' was the source path! (What is it?) So that was a silly question! Thanks. :)
    – aliqandil
    Oct 28, 2016 at 21:01
  • 8
    Tar will chdir to "/" thus all the following paths are relative to "/". Oct 29, 2016 at 14:27
  • 1
    This is awesome. Can confirm that this also works with Azure CLI's ssh extension docs.microsoft.com/en-us/cli/azure/… Feb 17, 2022 at 15:45
40

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/
2
  • 3
    This question is seven years old and has many answers already. While it is true that ssh can use non-default ports and the ssh client's -P option is then required, you don't really help with the problem at hand. Try focusing on recent questions. Feb 7, 2021 at 1:39
  • 1
    Yeah, it's unintuitive both because of the placement and letter size (with ssh its lower case).
    – mirekphd
    Sep 11, 2023 at 18:59
37

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

(see tutorial here)

0
22

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/
2
  • 9
    use put command for uploading files Nov 18, 2020 at 21:45
  • this was the easiest way
    – Diana
    Feb 6 at 2:24
10

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
7

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
6

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" ;-)

1
  • 1
    Good example of copy-from-remote instead of copy-to-remote, and will also work with the other archive programs like tar.
    – Samveen
    Aug 2, 2023 at 10:56
4

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
1
2

Many of the answers suggest scp, but it has since been deprecated in OpenSSH 8.0 on April 17th, 2019.

Assuming a file in the directory /usr/home/risner called records.dat, you copy the file using sftp:

sftp username@host:/path/

You will see:

Changing to: /path/.
sftp>

Copy a file from your local machine to the destination with:

put /usr/home/risner/records.dat

Download a file from the remote machine with:

get records.dat /usr/home/risner

You can run it non-interactively by setting up password-less authentication and calling it this way:

echo "put /usr/home/risner/records.dat" | sftp username@host:/path/
1

If you copy and you need the public key authentication you can use the -o or the -i flag and if you have set a port use -P

scp -i /path/keyfile -P 2000 /path/FileToCopy USER@SERVER:/DestinationPath/

scp -o IdentityFile="/path/keyfile" -P 2000 /path/FileToCopy USER@SERVER:/DestinationPath/

Other options/possibilities:

Newer versions of scp have the option -3

-3 Copies between two remote hosts are transferred through the local host. Without this option the data is copied directly between the two remote hosts

scp between two servers not in the same network

Secure copy over two IPs on the same network to the local machine

Use scp to copy a file to different servers

0

For a single file, if ssh can run non-interactively (see ssh-copy-id for example) you can use the following command:

cat file1 | ssh remotehost 'cat > file2'

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .