17

I am getting Permission denied (publickey) error when doing ssh by ssh username@ip while ssh working when we are doing vagrant ssh

VagrantFile :

Vagrant.configure("2") do |config|
 config.vm.box = "ubuntu/xenial64"
 config.vm.network "forwarded_port", guest: 80, host: 8071
 config.vm.network "private_network", ip: "192.168.33.71"
end

I am trying ssh [email protected] on terminal

Getting Error : Permission denied (publickey)

2

10 Answers 10

14

Use the private key in your connection with the vagrant box

ssh -i .vagrant/machines/default/virtualbox/private_key [email protected]

11
config.vm.synced_folder '.' and '/home/vagrant/' caused this problem.
Because the configure makes home directory on the host overwritten and destroy .ssh settings on the host.
I got the same problem a few seconds ago. I checked the .ssh was overwritten by Vagrant GUI.

In the summary, your synced folder overrides the .ssh folder in the virtual machine, so you cannot login with ssh.

The answer is from this issue.

1
  • while this answer helped me you should paste the relevant details that assisted you; i have edited the answer for you
    – wal
    Commented Oct 7, 2019 at 12:09
5

Please brief your question , from where to where you are SSHing. If you are SSHing through Vagrant box.. then you always have to use vagrant before any command.In case of vagrant only ssh [email protected] will not work.

vagrant ssh user@vmmachine

If you are using other user than default vagrant user you have to copy your Host machine public key content into guest machine user's authorized_keys file.(Use only if you are SSHing using vagrant to guest machine)

default location for authorized_keys:

/home/ubuntu/.ssh/authorized_keys

5
config.vm.provision :shell, :inline => "sudo sed -i 's/PasswordAuthentication no/PasswordAuthentication yes/g' /etc/ssh/sshd_config; sudo systemctl restart sshd;", run: "always"

I was able to resolve with the above config

3

Am guessing if you try vagrant ssh as mentioned by @Anurag you are able to connect.

To fix the Permission denied (publickey) error so that you able to ssh to the box from anywhere in your host machine, you can create an ssh key and copy the public key to the authorised_keys file on the guest. ssh-keygen you can choose a different file to save the keys. Then add the identity with ssh-add <path to your key>.

2
  • Not the best solution, but you can try with flag -A which adds all identities. Maybe if we can see the actual error, might help know the problem.
    – Kanyi
    Commented Jan 30, 2019 at 11:05
  • I am having the same Permission denied (publickey) problem when using vagrant ssh inside the project folder which is the copy (made with copy/paste on Windows) of the original project folder which is still working well.
    – Chupo_cro
    Commented May 30, 2021 at 6:20
0

I had the same problem with debian/jessie64 box. I'm running Vagrant on libvirt. I tried everything but nothing helped, then I took centos/7 box and everything is fine. I guess it have something to do with cloud-init not properly configured in the box itself.

0

Another Reason and Solution for Same error

I got the same error and none of the solutions make sense to me. The root cause in my case was the permission of the private key in the windows. By default the key has permission for "Everyone" to access. That makes the key insecure and the ssh does not like that. So I removed all other user permission from my key. Make it exclusive to be read/write from my windows user only. Then things worked as expected.

0

I'll tell you my solution, I hope it's the right one.

  1. Run: vagrant ssh-config From here, remember the ip + port of the connection, as well as the user, and path to private key(default ip = 127.0.0.1, port = 2222, user = vagrant, my path to private key = /home/my_user/Less/Vladimir/.vagrant/machines/default/virtualbox/private_key).
  2. Access the machine using vagrant: vagrant ssh
  3. Look up the public key on the Vagrant machine: cat ~/.ssh/authorized_keys
  4. Copy the entire line
  5. Exit the machine with exit
  6. Clear the code file /home/YOUR_USER/.ssh/known_hosts
  7. Add the line from step 4 in file /home/YOUR_USER/.ssh/known_hosts!
  8. Add data to this command from step 1: ssh [email protected] -p 2222 -i path_to_private_key
-1

Assuming that you already have id_rsa.pub key in your home directory (on the host machine) then, you could simply configule Vagrantfile like this:

config.vm.provision "file", source: "~/.ssh/id_rsa.pub", destination: "/home/vagrant/.ssh/id_rsa.pub"
config.vm.provision :shell, :inline => "cat /home/vagrant/.ssh/id_rsa.pub >> /home/vagrant/.ssh/authorized_keys", run: "always"

Next you will be able to ssh with ssh vagrant@vm_ip_address

-1

I tried everything here but it didn't work. So i ent back to vagrant cloud and downloaded another ubuntu18 package and it worked after asking for password which is vagrant

Not the answer you're looking for? Browse other questions tagged or ask your own question.