Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I refer to my local computer for scp'ing when logged into remote?

Tags:

unix

scp

This must be a really simple question, but I am trying to move a file from a remote server to my local computer, while logged into the remote (via ssh).

All of the guides say to just use

scp name@remote:/path/to/file local/path/to/file

But as far as I can understand, that would be what I would use from my local machine. From the remote machine, I assume that I want to use something like

scp /path/to/file my_local_computer:/local/path/to/file

but (if that's even correct) how do I know what to put in for my_local_computer?

Thanks!

like image 898
Asbestos Avatar asked Aug 29 '13 13:08

Asbestos


People also ask

How do I SCP from remote to local While in remote?

Copy or Download a File From Remote to Local Using SCP SCP syntax is pretty simple. Just invoke SCP followed by the remote username, @, the IP address or host, colon, and the path to the file. If not specified, the default path is the remote user's home directory.

How do I SCP to a local server?

To copy the files you will need to first invoke the SCP, followed by the remote username@IP address, path to file. If you do not specify the path, it is assumed as default in this case which will be the user's home directory, this will be followed the path where the file will be stored locally.

Can you use SCP while in SSH?

The scp command uses SSH to transfer data, so it requires a password or passphrase for authentication. Unlike rcp or FTP, scp encrypts both the file and any passwords exchanged so that anyone snooping on the network cannot view them.

How do I find my IP for SCP?

After making sure the ssh server is running on your remote machine, you should also note its IP address. You'll need that over on your local machine to connect using scp. Use the ifconfig command on the remote machine to get the IP address.


2 Answers

You can automatically figure out where you're logged in from by checking the environment variables SSH_CONNECTION and/or SSH_CLIENT. SSH_CONNECTION for example shows the client address, the outgoing port on the client, the server address and the incoming port on the server. See section ENVIRONMENT in man ssh

So, if you want to copy a file from the server to the client from which you're logged in from, the following (which infers the client ip by taking the first part of SSH_CONNECTION) should work:

scp /path/to/file $(echo $SSH_CONNECTION | cut -f 1 -d ' '):/local/path/to/file

Andreas

like image 191
Andreas Avatar answered Sep 28 '22 04:09

Andreas


You are on the right track! The man page for scp should tell you how to do what you want: http://linux.die.net/man/1/scp

If you are having trouble understanding the man page, then I will attempt to instruct you:

  1. If you want to push a file from your local machine to a remote machine

    scp /path/to/local/file testuser@remote-host:/path/to/where/you/want/to/put/file

  2. If you want to pull a file from a remote machine to your local machine

    scp testuser@remote-host:/path/to/file/you/want/to/pull /path/on/local/machine/to/place/file

  3. If you are logged into a remote machine and want to push a file to your local machine (assuming you have the ability to scp to the local machine in the first place)

    scp /path/on/remote/machine/to/file testuser@local-host:/path/on/local/machine/to/put/file

Now, to determine what your local-host address is, you can check the IP address of your local machine or if your local machine has been provided a DNS entry, you could use it.

I.E., scp ~/myfile [email protected]:/home/testuser/myfile OR scp ~/myfile testuser@my-host:/home/testuser/myfile

For the DNS entry, provided you are on a correctly configured network, you would not need a fully qualified domain. Otherwise, you would need to do something like [email protected]:/home/testuser/myfile

like image 41
igg1 Avatar answered Sep 28 '22 03:09

igg1