简体   繁体   中英

Vagrant : Permission denied (publickey)

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)

在与 vagrant box 的连接中使用私钥

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

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 .

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

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

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

我能够用上面的配置解决

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM