1270

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?

Z0OM
  • 3,149
user3021349
  • 16,569

13 Answers13

1712

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
HalosGhost
  • 4,790
DopeGhoti
  • 76,081
  • 194
    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. – Garrett Albright Dec 24 '13 at 16:40
  • @GarrettAlbright, I think the one but last System B should be a System A, right? So: "That being said, you can use scp if you're logged into System A via SSH and want to copy files between System B and System C." – jmc Dec 24 '13 at 17:38
  • I didn't think you could scp from one remote location to another remote location. I recall trying it once and being admonished by scp. – DopeGhoti Dec 24 '13 at 20:01
  • 1
    jmc, nope, I meant it like that. If you're shelled into System B via ssh, you can then execute scp on System B to copy files between System B and some other server ("System C"). The scp process will run on System B, which is the "local" system as far as it will be concerned. – Garrett Albright Dec 25 '13 at 05:41
  • 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 08 '14 at 19:48
  • Can I use this to copy to my local machine (MacBook)? And if so, where can I look to find out my MacBook's username? – Marco Prins Jan 21 '15 at 07:59
  • 1
    At any terminal prompt, you can use whoami to find your username. You can use SSH/SCP to copy to a Macbook, but you have to enable "Remote Login" in System Preferences -> Sharing. This will also tell you your username. – DopeGhoti Jan 21 '15 at 17:56
  • 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 '15 at 20:30
  • 23
    Remember -r (recursive) and -p (preserve system metadata): scp -rp – Rutrus Jul 01 '15 at 07:23
  • 2
    I am doing this from a Mac and you don't need your local username. Just the path to the file you want to copy locally and where you want it to go to remotely. Example: scp /home/myuser/sample.txt remoteuser@remoteserver.com:/home/remoteuser/ – Patrick Jul 30 '15 at 15:52
  • 1
    In your comment, you explicitly are using the username on the remote system. You can omit the username if your username is the same on the remote system, or if you have settings in .ssh/config for that host to specify the username for you. The rules for the username for scp are identical to those for ssh. – DopeGhoti Jul 31 '15 at 17:32
  • can path/to/file be a URL to a public file on web? – Jama Aug 25 '15 at 00:19
  • No, SCP doesn't know how to handle HTTP on either the source or the destination. – DopeGhoti Feb 06 '16 at 18:55
  • Is there a way for me to specify what port to use? – Jomar Sevillejo Apr 26 '16 at 01:08
  • 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 '16 at 18:22
  • @GarrettAlbright you comment is very ambiguous. I understood exactly nothing. A simple explanation would be "the first argument of scp is the source and second is the destination". Done! – machineaddict Apr 29 '16 at 11:03
  • 2
    To be precise: Actually it doesn't matter where you're logged in (on your machine!), but instead it matters where the current shell you're using to run the command (!) is logged in. – Zelphir Kaltstahl Jan 15 '17 at 11:22
  • 1
    @machineaddict What my answer addresses that your "simple explanation" does not is that you do not use SCP to copy between two remote systems. Either the source or the destination must be the local machine. – Garrett Albright May 11 '17 at 01:10
  • Note to future readers of this post, an ssh server needs to be running on the source computer – olfek May 26 '18 at 10:54
  • If you have ssh config file things are so nice. You can name your machines and do something like this - scp -F ssh_config my-machine-one:/remote_path_to_file /loca_path_to_file – thinklinux Dec 08 '18 at 10:20
  • @GarrettAlbright What you are missing is that in most cases a user does not know exactly where the file(s) is that they want to copy or their exact names. So, they usually have to ssh into the remote system to find that out before doing the copy. So, in fact they are usually logged into the remote system when they do the copy. – Tyler Durden Jan 11 '19 at 17:25
  • Yes you can move files between two remote hosts with SCP. One is obviously going to be the source, and the other the destination. You must be logged onto the one that executes the command, one way or another. But they are still both remote. Consider Ansible, Telnet, KVM over IP.
  • You can only move a file back to the client with SCP if it is running an SSH server.
  • – mckenzm Sep 09 '19 at 03:37
  • @GarrettAlbright, what should I use if I am logged in via SSH, if not SCP? – Joe Apr 25 '23 at 13:53
  • If you strictly want to use ssh and not scp, you can perform something like tar -zcf - file | ssh user@host 'tar -zxf -'. – DopeGhoti Apr 25 '23 at 23:30
  • @Joe For me the simplest approach is to just open a new terminal/shell/console window and use SCP there. You don't need to log out of the SSH session first. But if you really can't use SCP for some reason (unlikely), see the answer directly above. – Garrett Albright Apr 27 '23 at 18:29
  • @GarrettAlbright, oh, I see. That is easy. It wasn't a matter of not being able to use scp, but like mentioned above by Tyler Durden, I usually have to ssh in first to know where the file I want is located. I found myself using ssh, exiting, then using scp, then using ssh again. I hadn't thought to just open two terminals. – Joe Apr 27 '23 at 20:15
  • 1
    You can usually also use sftp to interactively noodle around in the remote filesystem, and use get file to download file, or put file to upload file. – DopeGhoti Apr 28 '23 at 01:08