How to copy a file from a remote server to a local machine?

1,851,706

Solution 1

The syntax for scp is:

If you are on the computer from which you want to send file to a remote computer:

scp /file/to/send username@remote:/where/to/put

Here the remote can be a FQDN or an IP address.

On the other hand if you are on the computer wanting to receive file from a remote computer:

scp username@remote:/file/to/send /where/to/put

scp can also send files between two remote hosts:

scp username@remote_1:/file/to/send username@remote_2:/where/to/put

So the basic syntax is:

scp username@source:/location/to/file username@destination:/where/to/put

You can read man scp to get more ideas on this.

Solution 2

You can use rsync as an alternative. It is mainly for syncing files.. but you can use it for this purpose as well.

rsync -avzh --stats --progress remoteuser@remoteip:/path/  localpath 

to add ssh options:

rsync -e "ssh -P $port_value" remoteuser@remoteip:/path/  localpath

--progress and --stats are useful for real-time display of transfer.

I think it a better option then SCP, since it skips already transferred files, which is noticeable when you're copy-ing lot of files.

Solution 3

scp [email protected]:/root/Jmeter/reports.jtl Downloads/

Solution 4

scp username@ipaddress:pathtofile localsystempath

scp sadananad@ipaddress:/home/demo/public_html/myproject.tar.gz .

If your using with port:

scp -Pportnumber username@ipaddress:pathtofile localsystempath 

scp -P2233 sadananad@ipaddress:/home/demo/public_html/myproject.tar.gz .

Solution 5

If you completely trust everyone in the network and you can connect a port of the destination machine directly, you can use netcat: nc.

Let's say the IP address of the destination machine is 192.168.1.123

On the destination run:

nc -l -p 7777 0.0.0.0 | tar zxvf - -C dest_dir

You can choose a different port, and also bind to another IP of your interfaces, 0.0.0.0 just catches on all interfaces.

On the source run:

tar zcvf - filename | nc 192.168.1.123 7777

IMHO, this is the fastest possible way to send a file from one computer to another using digital networks.

The arguments and command line options might slightly change between different versions of nc and tar, but it will definitely work with recent Linux distributions.

Share:
1,851,706

Related videos on Youtube

omega
Author by

omega

Updated on September 18, 2022

Comments

  • omega
    omega almost 2 years

    In my terminal shell, I ssh'ed into a remote server, and I cd to the directory I want.

    Now in this directory, there is a file called table that I want to copy to my local machine /home/me/Desktop.

    How can I do this?

    I tried scp table /home/me/Desktop but it gave an error about no such file or directory.

    Does anyone know how to do this?

    • ConvexMartian
      ConvexMartian over 7 years
      If you find yourself copying with scp often, you can mount the remote directory in your file browser and drag-and-drop. On my Ubuntu 15 host, it's under the menu bar "Go" > "Enter Location" > [email protected]:/home/debian. Alternatively, one can use sshfs to mount the remote machine's filesystem on the host. But that setup is a little more involved.
    • sakisk
      sakisk about 7 years
      Give rsync a try. It's great both for local and remote copies, gives you copy progress, etc. An example
  • camdixon
    camdixon almost 8 years
    What if I want to copy multiple files? I added a space and just used another /file/to/send Thanks for your awesome answer!
  • Henry
    Henry almost 8 years
    scp -r will copy recursively.
  • Sushivam
    Sushivam over 7 years
    What i want to copy the files from network to my VM ...how to achieve the same using scp
  • Guy Avraham
    Guy Avraham over 6 years
    @heemayl +1 for the good answer. Thought to add that in the case that it is a secure connection (that requires an authentication) you can use the following (for copying file from local to remote): scp -i mykey.pem somefile.txt [email protected]:/some/folder/in/remote
  • mehov
    mehov over 6 years
    Use scp -P 123 to specify custom port
  • Max Yudin
    Max Yudin over 5 years
    This will copy the file to the same remote directory.
  • unknownerror
    unknownerror over 5 years
    If i have an ssh session active, then i have to exit ssh session to scp from remote to local. is there any other way?
  • Brady Dowling
    Brady Dowling almost 5 years
    Here's a brief video showing the process, including the bit about the secure connection.
  • Mattes D
    Mattes D over 4 years
    I think your tar parameters are wrong, the source should not have "x" and could use "v" as well, for parity.
  • onur güngör
    onur güngör over 4 years
    @MattesD: Thank you, you are right, fixed it.
  • Habib Rehman
    Habib Rehman over 2 years
    what if we require to login to ssh by .pem file, is there a way to copy file while we stay inside the connection as i'll be able to do it as sudo
  • Mike Eng
    Mike Eng over 2 years
    This doesn't cover the case mentioned in the question, where you are ssh'd into a remote server and want to copy a file from that remote server to a local machine. Neither does the video mentioned in this previous comment. How can one use scp or rsync when already ssh'd into a remote server?
  • Sl4rtib4rtf4st
    Sl4rtib4rtf4st over 2 years
    What if the remoteip is in a network?