How do I scp via ssh?

41,408

Solution 1

To copy from REMOTE to LOCAL:

scp -P 12345 user@server:/path/to/remote/file /path/to/local/file

To copy from LOCAL to REMOTE:

scp -P 12345 /path/to/local/file user@server:/path/to/remote/file

Note: The switch to specify port for scp is -P instead of -p

If you want to copy all files in a directory you can use wildcards like below:

scp -P 12345 user@server:/path/to/remote/dir/* /path/to/local/dir/

or even

scp -P 12345 user@server:/path/to/remote/dir/*.txt /path/to/local/dir/

Solution 2

If you are doing this frequently I would suggest adding some config in the file ~/.ssh/config

add the following lines

Host highlabs
   Hostname gateway.highlabs.co
   User marcus
   Port 12345

Then you can

ssh highlabs

or

scp highlabs:/path/to/file /local/path/to/file

to copy from the server

or

scp /local/path/to/file highlabs:/remote/path/to/file

to copy to the server

If you are using key auth tab completion works the whole way, For example ssh hi<tab> will finish off the word and scp highlabs:/et<tab> will expand to /etc after checking the files on the remote server

Solution 3

You should use something like this

scp -P 12345 -p some_file [email protected]:

This will copy some_file to your home directory on the remote server. Change the name or path by putting the alternative immediately after the : (no space). Swap the arguments to copy back to the local system.

The -P 12345 is equivalent to your -p 12345 and the -p flag tells scp to maintain the timestamps and permissions for the destination file.

Share:
41,408
TheGrapeBeyond
Author by

TheGrapeBeyond

I am working on seismological and seismic data processing on current hot posts around the globe.

Updated on September 18, 2022

Comments

  • TheGrapeBeyond
    TheGrapeBeyond almost 2 years

    I have a LINUX machine (remote), and a MAC machine (local). Our system administrator set up an "SSH" method, whereby I can ssh from my MAC, to my LINUX machine, via this command on my MAC:

    ssh [email protected] -p 12345
    

    When I do this, I am prompted to put in the password for my LINUX machine, and when I do, I have access, which is great.

    What I want to do now though, is be able to scp from my MAC machine, to my LINUX machine, so that I can transfer files over. How do I do that? I have googled around but I am not sure what to do.

    Thank you

    • imz -- Ivan Zakharyaschev
      imz -- Ivan Zakharyaschev over 6 years
      Or rather of a question regarding the copying in the direction from the local machine to the remote one: unix.stackexchange.com/q/115560/4319
  • TheGrapeBeyond
    TheGrapeBeyond over 6 years
    What is "user@server" here though?
  • TheGrapeBeyond
    TheGrapeBeyond over 6 years
    Thanks, I am trying to reconcile what you wrote with that @Jesse_b wrote - I would like to copy a file from my mac to the remote, and vice-versa..
  • jesse_b
    jesse_b over 6 years
    user = username . server = hostname or IP address. From your mac it would be [email protected] but if you ssh into the linux machine and wanted to copy something back to your mac, you would need to use your mac username/hostname.
  • TheGrapeBeyond
    TheGrapeBeyond over 6 years
    Ok I think I got it to work... however it keeps asking me for my password everytime I run the command - how do I make it so I just need to enter the password once? Thanks!!
  • TheGrapeBeyond
    TheGrapeBeyond over 6 years
    Hi Jesse, yes I guess what I dont understand is what the "server" should be since I do not know the IP of the remote...
  • jesse_b
    jesse_b over 6 years
    if gateway.highlabs.co is the remote machine then gateway.highlabs.co is the hostname/FQDN...or at least is aliased/in your /etc/hosts.
  • TheGrapeBeyond
    TheGrapeBeyond over 6 years
    Oh I see what you mean - I think this worked! I just had to put the -P 12345 right after the scp command though. :D One last q: How would I move a bunch of files altogether at the same time with one scp command? Thank you so much you saved my Saturday!! :D
  • jesse_b
    jesse_b over 6 years
    @TheGrapeBeyond I've updated my answer.
  • roaima
    roaima over 6 years
    @TheGrapeBeyond the command uses the ssh transport to log you in each time you copy a file. Search (on the this site, amongst other places) for "password-less logins" and "ssh certificates".
  • Omar Tariq
    Omar Tariq about 5 years
    Thanks. This is a life saver.
  • Jakub Langr
    Jakub Langr over 4 years
    Hi @exussum that is really helpful—but the tab completion does not work on my Mac OSX (10.14.5) using Z-shell. Any ideas how to change that? I would love the TAB competition for remote filepaths to work.
  • exussum
    exussum over 4 years