Considering I have connected via ssh to a remote host and I want to copy a file from that remote host to my local device:

I know that I can just copy that file from a shell on the local device by using something like:

> scp user@remote:/path/to/file ./

and be done with it.

But when I am already on the remote host and I have no idea what my local IP is, is there a way to specify a copy to the local machine to the directory I was when I connected via ssh?

> pwd
> ssh user@remote
Connected to remote host
[==========] 100% Done.
> exit
> ls
  • 1
    Can we assume that your username is the same on both systems? – Jeff Schaller Oct 10 '19 at 14:55
  • No, this is usually not the case. – salbeira Oct 14 '19 at 21:23

Yes, this is in fact possible using the environment variable SSH_CLIENT.

Try this:

scp /path/to/my_file username@$(echo $SSH_CLIENT | awk '{ print $1}'):

Note that this will push the file to your home directory.

  • Consider using "${SSH_CONNECTION%% *}" instead of the $( ... ) part. – Jeff Schaller Oct 10 '19 at 14:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.