Vagrant ssh authentication failure

2019-01-05 07:18发布

The problem with ssh authentication:

==> default: Clearing any previously set forwarded ports...
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
    default: Adapter 2: bridged
==> default: Forwarding ports...
    default: 22 => 2222 (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: Error: Connection timeout. Retrying...
    default: Error: Connection timeout. Retrying...
    default: Error: Connection timeout. Retrying...
    default: Error: Connection timeout. Retrying...
    default: Error: Authentication failure. Retrying...
    default: Error: Authentication failure. Retrying...
    default: Error: Authentication failure. Retrying...
    default: Error: Authentication failure. Retrying...
    default: Error: Authentication failure. Retrying...

I can Ctrl+C out of the authentication loop and then successfully ssh in manually.

I performed the following steps on the guest box:

  • Enabled Remote Login for All Users.

  • Created the ~/.ssh directory with 0700 permissions.

  • Created the ~/.ssh/authorized_keys file with 0600 permissions.

  • Pasted this public key into ~/.ssh/authorized_keys

I've also tried using a private (hostonly) network instead of the public (bridged) network, using this line in the Vagrantfile:

config.vm.network "private_network", ip: "172.16.177.7"

I get the same output (except Adapter 2: hostonly) but then cannot ssh in manually.

I also tried config.vm.network "private_network", ip: "10.0.0.100".

I also tried setting config.ssh.password in the Vagrantfile. This does output SSH auth method: password but still doesn't authenticate.

And I also tried rebuilding the box and rechecking all the above.

It looks like others have had success with this configuration, so there must be something I'm doing wrong.

I found this thread and enabled the GUI, but that doesn't help.

27条回答
姐就是有狂的资本
2楼-- · 2019-01-05 07:38

If you experience this issue on vagrant 1.8.5, then check out this thread on github:

https://github.com/mitchellh/vagrant/issues/7610

It's caused basically by a permission issue, the workaround is just

vagrant ssh 
password: vagrant 
chmod 0600 ~/.ssh/authorized_keys
exit

then

vagrant reload 

FYI: this issue only affects CentOS, Ubuntu works fine.

查看更多
劫难
3楼-- · 2019-01-05 07:38

I have found a way around the mess with the keys on Win 8.2 where I did not succeed with any of the methods mentioned here. It may be interesting that exactly the same combination of VirtualBox, Vagrant, and the box run on Win 7 Ultimate without any problems.

I switched to the password authentication by adding the following commands in Vagrantfile:

config.ssh.password = "vagrant"
config.ssh.insert_key = false

Note that I'm not sure that this is the only changes required because I already did:

  1. I generated a new RSA key pair and changed authorized_keys file accordingly (all in the virtual machine, see the suggestions above and elsewhere)

  2. I copied the private key to the same directory where Vagrantfile resides and added

     config.ssh.private_key_path = "./id_rsa"
    

But I believe that these changes were irrelevant. I spent a plenty of time trying, so I did not change the working configuration by obvious reasons :)

查看更多
ゆ 、 Hurt°
4楼-- · 2019-01-05 07:40

None of the above worked for me. Somehow the box had the wrong public key added in the vagrant user authorised_keys file.

If you can still ssh on the box with the vagrant password (password is vagrant), i.e.

ssh vagrant@localhost -p 2222

then copy the public key content from https://raw.githubusercontent.com/mitchellh/vagrant/master/keys/vagrant.pub to the authorised_keys file with the following command

echo "ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA6NF8iallvQVp22WDkTkyrtvp9eWW6A8YVr+kz4TjGYe7gHzIw+niNltGEFHzD8+v1I2YJ6oXevct1YeS0o9HZyN1Q9qgCgzUFtdOKLv6IedplqoPkcmF0aYet2PkEDo3MlTBckFXPITAMzF8dJSIFo9D8HfdOV0IAdx4O7PtixWKn5y2hMNG0zQPyUecp4pzC6kivAIhyfHilFR61RGL+GPXQ2MWZWFYbAGjyiYJnAmCP3NOTd0jMZEnDkbUvxhMmBYSdETk1rRgm+R4LOzFUGaHqHDLKLX+FIPKcF96hrucXzcWyLbIbEgE98OHlnVYCzRdK8jlqm8tehUc9c9WhQ== vagrant insecure public key" > .ssh/authorized_keys

When done exit the VM and try vagrant ssh again. It should work now.

查看更多
Root(大扎)
5楼-- · 2019-01-05 07:40

In my experience, this has been a surprisingly frequent problem with new vagrant machines. By far the easiest way to solve it, instead of altering the configuration itself, has been creating the required ssh keys manually on the client, then using the private key on the host.

  1. Log in to vagrant machine: vagrant ssh, use default password vagrant.
  2. Create ssh keys: for example, ssh-keygen -t rsa -b 4096 -C "vagrant" (as adviced by GitHub's relevant guide).
  3. Rename the public key file (by default id_rsa.pub), overriding the old one: mv .ssh/id_rsa.pub .ssh/authorized_keys.
  4. Reload ssh service in case needed: sudo service ssh reload.
  5. Copy the private key file (by default id_rsa) to the host machine: for instance, use a fine combination of cat and clipboard, cat .ssh/id_rsa, paint and copy (better ways must exist, go invent one!).
  6. Logout from the vagrant machine: logout.
  7. Find the current private key used by vagrant by looking at its configuration: vagrant ssh-config (look for instance ÌdentityFile "/[...]/private_key".
  8. Replace the current private key with the one you created at the host machine: for example, nano /[...]/private_key and paste from the clipboard, if all else fails. (Note, however, that if your private_key is not project specific but shared by multiple vagrant machines, you better configure the path yourself in order to not break other perfectly working machines! Changing the path is as simple as adding a line config.ssh.private_key_path = "path/to/private_key" into the Vagrantfile.) Furthermore, if you are using PuPHPet generated machine, you can store your private key to file puphpet/files/dot/ssh/id_rsa and it will be added to Vagrantfile's ssh config automatically.
  9. Test the setup: vagrant ssh should now work.

Should that be the case, congratulate yourself, logout, run vagrant provision if needed and carry on with the meaningful task at hand.

If you still face problems, it may come handy to add verbose flag to ssh command to ease debugging. You can pass that (or any other option, for that matter) after double dash. For example, typing vagrant ssh -- -v. Feel free to add as many v's as you need, each will give you more information.

查看更多
ら.Afraid
6楼-- · 2019-01-05 07:40

This can also happen if you're trying to force your VM to use a root user by default for SSH....

For example, a config like so in your Vagrantfile may cause this failure:

config.ssh.username = 'root'
config.ssh.password = 'vagrant'
config.ssh.insert_key = 'true'

Solution: Comment out those lines and try again!

查看更多
Melony?
7楼-- · 2019-01-05 07:40

I have started the machine, then:

vagrant ssh-config

I've gotten the following:

Host default HostName 127.0.0.1 User vagrant Port 2222 UserKnownHostsFile /dev/null StrictHostKeyChecking no PasswordAuthentication no IdentityFile /Users/my-user-name/Documents/PHP-Projects/my-php-project/puphpet/files/dot/ssh/id_rsa IdentityFile /Users/my-user-name/.vagrant.d/insecure_private_key IdentitiesOnly yes LogLevel FATAL

Then I've ran

cat ~/.ssh/id_rsa > /Users/my-user-name/Documents/PHP-Projects/my-php-project/puphpet/files/dot/ssh/id_rsa

Machine booted from here on

  • El Capitan 10.11.1 Beta (15B38b)
  • Virtual Box 5.0.8 r103449
  • Vagrant 1.7.4
查看更多
登录 后发表回答