Vagrant : Permission denied (publickey)
Asked Answered
C

10

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)

Crackup answered 10/5, 2018 at 10:47 Comment(2)
Possible duplicate of How to ssh to vagrant without actually running "vagrant ssh"?Spiffy
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.Montiel
D
14

Use the private key in your connection with the vagrant box

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

Dysthymia answered 1/7, 2019 at 9:30 Comment(0)
E
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.

Encephalitis answered 8/7, 2019 at 14:47 Comment(1)
while this answer helped me you should paste the relevant details that assisted you; i have edited the answer for youSelma
T
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

Truelove answered 31/5, 2018 at 12:52 Comment(0)
B
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

Babysit answered 11/12, 2019 at 1:31 Comment(0)
K
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>.

Keilakeily answered 24/7, 2018 at 10:50 Comment(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.Keilakeily
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.Grig
C
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.

Cichlid answered 29/8, 2022 at 21:5 Comment(0)
F
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.

Fourflusher answered 24/11, 2023 at 8:18 Comment(0)
U
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
Undressed answered 20/5 at 15:0 Comment(0)
W
-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

Weide answered 1/9, 2020 at 13:23 Comment(0)
M
-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

Mishap answered 24/10, 2023 at 22:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.