Reputation: 403
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)
Upvotes: 17
Views: 41939
Reputation: 1
I'll tell you my solution, I hope it's the right one.
Upvotes: 0
Reputation: 411
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.
Upvotes: 0
Reputation: 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
Upvotes: -1
Reputation: 86
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.
Upvotes: 0
Reputation: 382
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.
Upvotes: 11
Reputation: 13700
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
Upvotes: -1
Reputation: 1755
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
Upvotes: 7
Reputation: 617
Use the private key in your connection with the vagrant box
ssh -i .vagrant/machines/default/virtualbox/private_key [email protected]
Upvotes: 15
Reputation: 391
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>
.
Upvotes: 3
Reputation: 837
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
Upvotes: 5