简体   繁体   English

Vagrant:权限被拒绝(公钥)

[英]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 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我正在终端上尝试ssh ubuntu@192.168.33.71

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.总之,您的同步文件夹会覆盖虚拟机中的 .ssh 文件夹,因此您无法使用 ssh 登录。

The answer is from this issue .答案来自这个问题

Please brief your question , from where to where you are SSHing.请简要说明您的问题,从 SSH 连接到哪里。 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 box 进行 SSHing.. 那么您总是必须在任何命令之前使用 vagrant。在 vagrant 的情况下,只有ssh ubuntu@192.168.33.71将不起作用。

vagrant ssh user@vmmachine流浪 ssh 用户@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)如果您使用的是默认 vagrant 用户以外的其他用户,则必须将您的主机公钥内容复制到来宾机器用户的 authorized_keys 文件中。(仅当您使用 vagrant SSH 连接来宾机器时才使用)

default location for authorized_keys: authorized_keys 的默认位置:

/home/ubuntu/.ssh/authorized_keys /home/ubuntu/.ssh/authorized_keys

Am guessing if you try vagrant ssh as mentioned by @Anurag you are able to connect.我猜如果你像@Anurag 提到的那样尝试vagrant ssh ,你就可以连接。

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.要修复Permission denied (publickey)错误,以便您能够从主机中的任何位置通过 ssh 连接到该框,您可以创建一个 ssh 密钥并将公钥复制到来宾上的 authorised_keys 文件。 ssh-keygen you can choose a different file to save the keys. ssh-keygen您可以选择不同的文件来保存密钥。 Then add the identity with ssh-add <path to your key> .然后使用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:假设您的主目录(在主机上)中已经有id_rsa.pub密钥,那么您可以像这样简单地配置Vagrantfile

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接下来,您将能够使用ssh vagrant@vm_ip_address进行ssh vagrant@vm_ip_address

I had the same problem with debian/jessie64 box.我对debian/jessie64盒子有同样的问题。 I'm running Vagrant on libvirt.我在 libvirt 上运行 Vagrant。 I tried everything but nothing helped, then I took centos/7 box and everything is fine.我尝试了一切,但没有任何帮助,然后我拿了centos/7盒子,一切都很好。 I guess it have something to do with cloud-init not properly configured in the box itself.我想这与盒子本身中未正确配置的cloud-init有关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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