1

What terminal command can I use to transfer a directory from a remote machine (linux) to my local machine (mac)? Most importantly, I'd like to do this from the command line while SSH'ed into the remote machine. I know how to use SCP while on my local machine command line, but I have no idea how I'd use it from the remote machine (nor what my local path would be).

9
  • Is your local machine behind a firewall? Is there a particular reason you need to do this while remotely logged in? Commented Sep 21, 2012 at 18:02
  • I'm using su to access the files, so didn't think I could do this while not logged in. Commented Sep 21, 2012 at 18:04
  • Ah. Well, if you have read access to the files while not root, you should still be able to access them via scp from your local machine. If they're not readable by anyone that isn't root, then yeah, you'd need to run the copy as root. Commented Sep 21, 2012 at 18:06
  • Right. So I have to run the copy as root. Seems like SCP still works if I know the full path to my mac, right? How would I find this? Commented Sep 21, 2012 at 18:07
  • Well, that goes back to my first question, are you behind a firewall/router, or is your Mac publicly accessible (if you're at home, are you directly plugged into your modem?)? Commented Sep 21, 2012 at 18:08

3 Answers 3

6

Start the sshd on your local computer if you hadn't already done this. Then start a second session from your local computer to the remote computer:

ssh -R 2222:127.0.0.1:22 user@remote 

This forwards the remote port 2222 to the sshd listening on 127.0.0.1 on your local computer, creating a reverse tunnel. Then run scp on the remote computer:

scp -P2222 file 127.0.0.1:/path/ 
2
  • I did not know that. Much simpler! Commented Sep 21, 2012 at 18:36
  • Wow cool. This is the way to go. Commented Sep 21, 2012 at 18:45
1

You can also do the opposite, to grab a file from a remote machine to the local machine,

ssh -L 2222:[email protected]:22 [email protected] -N & scp -P 2222 127.0.0.1:/path/to/file/on/remote/machine.diff /tmp 

The ssh command sets up a tunnel from localhost:2222 to the remote machine 192.168.1.37 behind the net facing server remoteserver.com. The second command lets you copy it locally.

Some good examples and tips here, and how to configure it in ~/.ssh/config to automate it.

0

If you're locally behind a router/firewall that is giving you a private IP address, you'll need to configure the router to NAT a port to map to your local port 22. You can then access it from the remote computer by addressing your public IP address on whatever port you choose (you can map port 22 directly, but I generally personally recommend avoiding this as standard server ports are common targets for malware trying to find a way in).

2
  • That won't be necessary. Commented Sep 21, 2012 at 18:34
  • One other note: make sure that Remote Login is enabled via System Preferences if on a mac. Commented Sep 21, 2012 at 18:34

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.