20

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 marcus@gateway.highlabs.co -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

3 Answers3

29

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/
jesse_b
  • 37,005
  • What is "user@server" here though? – TheGrapeBeyond Dec 30 '17 at 19:35
  • 1
    user = username . server = hostname or IP address. From your mac it would be marcus@gateway.highlabs.co 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. – jesse_b Dec 30 '17 at 19:42
  • 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... – TheGrapeBeyond Dec 30 '17 at 19:44
  • 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. – jesse_b Dec 30 '17 at 19:46
  • 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 – TheGrapeBeyond Dec 30 '17 at 19:49
  • @TheGrapeBeyond I've updated my answer. – jesse_b Dec 30 '17 at 19:52
  • Thanks for saving my day. I was using lower case p for the port and struggling with it. Thanks. – Vineel Sep 24 '20 at 17:13
7

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

exussum
  • 4,023
  • 1
    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. – Jakub Langr Nov 06 '19 at 13:11
  • Try https://unix.stackexchange.com/questions/203931/whats-the-magic-that-allows-me-to-tab-complete-remote-files-as-i-type-an-scp-co – exussum Nov 06 '19 at 19:23
5

You should use something like this

scp -P 12345 -p some_file marcus@gateway.highlabs.co:

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.

Chris Davies
  • 116,213
  • 16
  • 160
  • 287
  • 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.. – TheGrapeBeyond Dec 30 '17 at 19:40
  • 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 Dec 30 '17 at 19:43
  • @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". – Chris Davies Dec 30 '17 at 20:15