Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vagrant ssh does not work on Git Bash/MSYS2 environments after 2.4.0 update #13284

Closed
guillermo-jano opened this issue Nov 2, 2023 · 13 comments · Fixed by #13329
Closed

vagrant ssh does not work on Git Bash/MSYS2 environments after 2.4.0 update #13284

guillermo-jano opened this issue Nov 2, 2023 · 13 comments · Fixed by #13329

Comments

@guillermo-jano
Copy link

guillermo-jano commented Nov 2, 2023

After #13219, looks like Vagrant now defaults to generating ed25519 key pairs when replacing the insecure key upon new box creation. On Windows, this is generated with CRLF line endings and apparently the OpenSSL version shipping with Git Bash and MSYS2 (probably also Cygwin, not tested) does not like that (this seemingly wasn't a problem with RSA keys though). vagrant ssh works as expected on Windows PowerShell and traditional Command Prompt (CMD.EXE).

Debug output

$ vagrant ssh
[email protected]'s password:

If I try vagrant ssh -- -v instead I can see

Load key "C:/MYBOX/.vagrant/machines/default/virtualbox/private_key": error in libcrypto

Also if I run dos2unix .vagrant/machines/default/virtualbox/private_key and then run vagrant ssh again connection works as expected. So apparently the problem is the OpenSSL version in Git Bash/MSYS2 does not like ed25519 keys with CRLF line endings.

Expected behavior

vagrant ssh should open a session with the newly created box without asking for a password

Actual behavior

vagrant ssh asks for a password

Reproduction information

Vagrant version

Vagrant 2.4.0

Host operating system

Microsoft Windows 10 Enterprise 10.0.19044 Build 19044

Guest operating system

Ubuntu 18.04.3 LTS

Steps to reproduce

  1. Create a new Vagrant box with vagrant up
  2. Run vagrant ssh from a Git Bash / MSYS2 environment (probably also Cygwin, not tested)

Vagrantfile

Vagrant.configure("2") do |config|
  config.vm.box = "hashicorp/bionic64"
end
@iliareshetov
Copy link

I experienced a similar problem, which was resolved by switching CRLF to LF.

@guillermo-jano
Copy link
Author

guillermo-jano commented Nov 7, 2023

Yes the dos2unix command line I mentioned in the issue body fixes the problem, but IMO it would be better if this worked out of the box as it used to.

@iliareshetov
Copy link

Hey, I’m with you on this and I just want to confirm that this issue is there and it’s rather hard to figure out why it doesn’t work.

The command vagrant ssh --debug doesn’t help much and only when I used the ssh command and only then got error in libcrypto, which brought me here.

Definitely should work out of the box.

@OIOIOOOI
Copy link

Same issue but different error.
Having keys generated with CRLF and trying vagrant ssh yields Error: [email protected]: Permission denied (publickey).
This is solved by changing the line endings, but it's truly an annoying thing to do every time the VM is created.

@guillermo-jano
Copy link
Author

Yes, hopefully this gets some attention from the developers and can be fixed in a future release.

@DanRagle
Copy link

DanRagle commented Dec 6, 2023

Can confirm this happens in cygwin environments, as well. So new box provisions are broken since the first rsync (happens after the insecure key is replaced with the borked CR/LF version) fails.

@mkot02
Copy link

mkot02 commented Dec 14, 2023

I came across the same issue. I have setup with multiple hosts and ansible running in one of them. Ansible fails to do configuration on other hosts with message: invalid format.

As a workaround I'm converting private_keys using this command:

find .vagrant -type f -name private_key | xargs dos2unix

I have this problem after upgrading to 2.4.0

@EyalDagiAvdor
Copy link

EyalDagiAvdor commented Dec 20, 2023

Can confirm this happens in git bash on vagrant 2.4.0.
vagrant ssh
[email protected]'s password:

The workaround, find .vagrant -type f -name private_key | xargs dos2unix, fixes the issue, but this should work out of the box.

@ghalse
Copy link

ghalse commented Jan 11, 2024

I believe this problem stems from how the private key is written here:
https://github.com/hashicorp/vagrant/blob/v2.4.0/plugins/communicators/ssh/communicator.rb#L221-L223

Note the \n generated here is converted to \r\n in the resulting file, suggesting that a conversion is happening.

The open("w+") when the private key is written is in text file mode, and so Ruby does line-ending conversions depending on operating system.

The problem can be resolved by changing the open() to use binary mode to suppress the EOL <-> CRLF conversion on Windows, preserving the line endings generated in keypair.rb.

--- a/plugins/communicators/ssh/communicator.rb        2024-01-11 16:06:33.948133477 +0200
+++ b/plugins/communicators/ssh/communicator.rb     2024-01-11 16:06:53.192133293 +0200
@@ -218,7 +218,7 @@

           # Write out the private key in the data dir so that the
           # machine automatically picks it up.
-          @machine.data_dir.join("private_key").open("w+") do |f|
+          @machine.data_dir.join("private_key").open("wb+") do |f|
             f.write(priv)
           end

As of writing this, the problem still exists in the main branch too, even after #13327.

@guillermo-jano
Copy link
Author

I think this has been the case forever, however the SSH client in Git Bash / MSYS2 does not seem to have an issue with RSA private keys but it does not like ed25519 keys in CRLF format. Anyway since the built-in Windows SSH client is fine with LF keys, I'd say your solution should harm no one, and vagrant ssh would work in every kind of environment.

@ghalse
Copy link

ghalse commented Jan 12, 2024

I think this has been the case forever, however the SSH client in Git Bash / MSYS2 does not seem to have an issue with RSA private keys but it does not like ed25519 keys in CRLF format.

It is a bit more subtle than that. The old RSA private keys were PEM encoded using OpenSSL; the ED25519 keys are encoded using OpenSSH's more lightweight format. Having looked at OpenSSH's source code for this, I think you'd have the same problem with an RSA key saved using the OpenSSH file format. (i.e. the changes in the way vagrant generates the keys might make this a problem for new RSA keys too).

@guillermo-jano
Copy link
Author

Ahh, that might be, not sure how to ask Vagrant to generate a RSA key instead of ed25519 now anyway...

@chrisroberts
Copy link
Member

I had not forgotten about this issue! The fix will be included in 2.4.1 🙂

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 12, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants