6

I'm trying to copy some files from my machine to an Azure VM. I say in advance that I can ssh the Azure's VM, I gave my public key during the creation of the machine. Both machines are Linux.

When I try:

sudo scp /path/ca.crt ubuntu@ip.add.res.s:~/

I receive in output:

Permission denied (publickey). lost connection

I'm not pointing on a root directory (my file is in a root directory that's why the sudo); I'm not pointing to a wrong user; if I can connect through ssh why scp should be denied?

ale93p
  • 464
  • 8
  • 20

1 Answers1

10

Using sudo to access a root file, scp is going to look for the identity file id_rsa in /root/.ssh/ instead of in /home/user/.ssh/. That's why I have to specify the identity file in the scp command (not the public key):

sudo scp -i ~/.ssh/id_rsa /path/ca.crt ubuntu@ip.add.res.s:/home/ubuntu/

I hope that this can be useful to someone else.

Cheers.

ale93p
  • 464
  • 8
  • 20
  • `scp` will use `~/.ssh/id_rsa` by default. I think if you don't use `-i ` will the same result. What other you change? – Shui shengbao Apr 26 '17 at 09:35
  • @Walter-MSFT nothing more, just that. Using `scp -v -v -v` I noticed that (obviusly) using sudo it was going to look for the id_rsa file in `/root/.ssh/` instead of in `/home/user/.ssh/`, that's why I have to specify the correct path (shame on me). – ale93p Apr 26 '17 at 09:39
  • 1
    I know the reason, very good answer. I think you had better add it to your answer. – Shui shengbao Apr 26 '17 at 09:41
  • 1
    This plus [this other answer](https://unix.stackexchange.com/a/347412) helped me resolve my issue. – ArdentLearner Feb 05 '21 at 15:52
  • 1
    I fixed it by giving permissions to write to the remote destination folder from VM: `chmod 777 _remote-destination-folder-vm_` – Florin Vîrdol Aug 25 '21 at 21:01