16

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 ubuntu@192.168.33.71 on terminal

Getting Error : Permission denied (publickey)

itsvks
  • 383
  • 1
  • 6
  • 16
  • Possible duplicate of [How to ssh to vagrant without actually running "vagrant ssh"?](https://stackoverflow.com/questions/10864372/how-to-ssh-to-vagrant-without-actually-running-vagrant-ssh) – Matthew Schuchard May 10 '18 at 11:39
  • I have had the displeasure of working with that box and spent a lot of time trying to debug this. It is fundamentally messed up. I would just try another box. – LalienX Sep 18 '18 at 15:21

7 Answers7

12

Use the private key in your connection with the vagrant box

ssh -i .vagrant/machines/default/virtualbox/private_key vagrant@192.168.33.71

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.

周左左
  • 362
  • 3
  • 5
  • while this answer helped me you should paste the relevant details that assisted you; i have edited the answer for you – wal Oct 07 '19 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 ubuntu@192.168.33.71 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

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

Kanyi
  • 391
  • 5
  • 9
  • 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 Jan 30 '19 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 May 30 '21 at 06:20
3
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

zessx
  • 68,042
  • 28
  • 135
  • 158
webjockey
  • 1,647
  • 2
  • 20
  • 28
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.

Bluesboy
  • 51
  • 2
  • 2
-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

Lukasz Dynowski
  • 11,169
  • 9
  • 81
  • 124