67

I've searched all around and can't seem to find this... I'm trying to copy a private key to my local machine which is a Mac.

When I fire up terminal on my mac, I get Jamies-iMac:~ jamie$

So after I ssh into my Ubuntu server I tried

scp /path/to/myfile.txt jamie@Jamies-iMac:/path/to/myfile.txt

which gives me:

ssh: Could not resolve hostname Jamies-iMac: Name or service not known
lost connection

In place of jamie@Jamies-iMac:/path/to/myfile.txt I've tried some other variations but nothing seems to work. Thanks for your help.

6 Answers 6

91

@ovc had it right, but there is a syntax error, and my edit got rejected for some reason. You need to have a colon between the user and filepath on the ubuntu side, and on the mac side you need to have the /Users/username/ portion in the filepath. Like so:

scp [email protected]:/path/to/myfile.txt /Users/Jamie/local/path/to/myfile.txt

This assumes you have private key authentication set up.

3
  • 3
    Its showing "Permission Denied (public key)" May 12, 2015 at 13:14
  • default port is 22, -P to specify port
    – Vahid
    Feb 13, 2017 at 5:02
  • 2
    Use -r to recursively copy a folder Mar 21, 2017 at 15:02
19

You're doing it the wrong way around. Simply use the scp command on the Mac, like this: scp [email protected]:/path/to/myfile.txt /local/path/to/myfile.txt. You may also just use FileZilla which is a graphical client. Connect to your Ubuntu with a URL like sftp://192.168.1.111, of course you need to use the valid IP address.

4
  • Ah, so I can only go one way. I knew it was something simple. Thanks.
    – inorganik
    Jun 25, 2013 at 20:40
  • I made a small edit to your answer, it didn't work until I did it the way that is shown in the edit.
    – inorganik
    Jun 25, 2013 at 23:00
  • 1
    inorganik, it can go both ways long as you have an SSH server running on both machines. I don't beleive that's the case with Macs, though. Otherwise, you'll need to use scp from the machine without the server so that it can perform a connection.
    – ooa
    Jun 26, 2013 at 20:17
  • 1
    +1 for mentioning the graphical client option, I used Cyberduck and it worked a treat. Jun 3, 2019 at 19:48
9

Excellent answers above. Additionally, if you need to use a certificate for authentication, you can use the -i flag.

scp -i /path/to/cert [email protected]:/path/to/myfile.txt /Users/Jamie/local/path/to/myfile.txt
1
  • I didn't see your reply earlier, but this was what I needed. I kind of deduced it from the answer above and then saw yours. Thank you
    – AJC
    Aug 23, 2018 at 21:26
2

Proposition of a solution inspired by this answer .

In order to copy a file from a remote server to your local home computer you will need to open a terminal on your home computer and write a command structured like this :

scp -P $PORT_NUMBER $USERNAME@$IP_ADDRESS:$PATH_TO_THE_FILE_TO_COPY $PATH_TO_DESTINATION

Explanations :

scp : 

Secure Copy command more infos here

$PORT_NUMBER: 

SSH have a default port set to 22, you can edit this port here ex: 23

$USERNAME: 

the username access

$IP_ADDRESS: 

the ip of the remote access

$PATH_TO_THE_FILE_TO_COPY: 

the path where you want to get the file

$PATH_TO_DESTINATION: 

the path where you want to copy the file


For exemple :

scp -P 22 johndoe@$011.235.813.213:/var/projects/calculator/tests/week-1 /Users/John/transit/

Advices :

  • Be sure to have the necessary rights on the element you want to copy from your server.
  • ⚠️ Init this command from your home computer not from the server ⚠️

🖖

1

If the path you're using has spaces, you should use the path in quotes, such as

scp [email protected]:"/path to/myfile.txt" ./myfile.txt

Yet, that didnt work for me.

Allegedly you should use triple backslashes, such as

/Users/me/Application\\\ Data/file.txt

But it worked with path in quotes and double slashes only.

I am ssh-ing from a mac into another mac though.

0
  1. First you have to open the terminal in your local Mac

  2. In the terminal type the below command

    scp [email protected]:/tmp/server_db_backup/db.sql /Users/username/Desktop/

In the above command

  • "59.119.31.42" should be replaced with your server IP
  • "/tmp/server_db_backup/db.sql" this should be replaced with the path of the file in the server
  • "/Users/username/Desktop/" this should be replaced with the local path where you are going to save that file

If you need to pass the certificate to login then run the below command where need to replace the all the above and the path/to/key.pem according to your key location

scp -i /path/to/key.pem [email protected]:/tmp/server_db_backup/db.sql /Users/username/Desktop/

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .