Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to scp back to local when I've already sshed into remote machine?

Tags:

linux

scp

ssh

Often I face this situation: I sshed into a remote server and ran some programs, and I want to copy their output files back to my local machine. What I do is remember the file path on remote machine, exit the connection, then scp user@remote:filepath .

Obviously this is not optimal. What I'm looking for is a way to let me scp file back to local machine without exiting the connection. I did some searching, almost all results are telling me how to do scp from my local machine, which I already know.

Is this possible? Better still, is it possible without needing to know the IP address of my local machine?

like image 423
laike9m Avatar asked Aug 25 '14 08:08

laike9m


People also ask

Can you SCP from remote to local?

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 from remote host to local machine?

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 SCP while 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.

Can you transfer a file from the remote machine to the local machine?

Copy and paste the required file from the remote machine in the cloud storage disk. Now, the file will appear in the file transfer window as shown below. Click the download icon against the file. Now the file will be downloaded to your local machine.


1 Answers

Given that you have an sshd running on your local machine, it's possible and you don't need to know your outgoing IP address. If SSH port forwarding is enabled, you can open a secure tunnel even when you already have an ssh connection opened, and without terminating it.

Assume you have an ssh connection to some server:

local $ ssh [email protected] Password: remote $ echo abc > abc.txt  # now we have a file here 

OK now we need to copy that file back to our local server, and for some reason we don't want to open a new connection. OK, let's get the ssh command line by pressing Enter ~C (Enter, then tilde, then capital C):

ssh> help Commands:       -L[bind_address:]port:host:hostport    Request local forward       -R[bind_address:]port:host:hostport    Request remote forward       -D[bind_address:]port                  Request dynamic forward       -KR[bind_address:]port                 Cancel remote forward 

That's just like the regular -L/R/D options. We'll need -R, so we hit Enter ~C again and type:

ssh> -R 127.0.0.1:2222:127.0.0.1:22 Forwarding port. 

Here we forward remote server's port 2222 to local machine's port 22 (and here is where you need the local SSH server to be started on port 22; if it's listening on some other port, use it instead of 22).

Now just run scp on a remote server and copy our file to remote server's port 2222 which is mapped to our local machine's port 22 (where our local sshd is running).

remote $ scp -P2222 abc.txt [email protected]: [email protected]'s password: abc.txt                   100%    4     0.0KB/s   00:00 

We are done!

remote $ exit logout Connection to example.com closed. local $ cat abc.txt abc 

Tricky, but if you really cannot just run scp from another terminal, could help.

like image 184
afenster Avatar answered Sep 17 '22 04:09

afenster