vagrant
vagrant copied to clipboard
Vagrant ssh will exit on Ctrl+C
In the guest if you open up an interactive tool e.g. less, if you Ctrl+C it will exit not just the tool but the ssh session as well (as if you had run exit).
Vagrant version
Vagrant 2.2.19 (not the latest but could not see a related issue in closed tickets or release notes)
Host operating system
Edition Windows 10 Pro Version 21H2 Installed on 28/06/2022 OS build 19044.2006 Experience Windows Feature Experience Pack 120.2212.4180.0
Guest operating system
Ubuntu 20.04 (Homestead v13.2.1)
Vagrantfile
Is the default file as cloned with homestead, tagged v13.2.1:
# -*- mode: ruby -*-
# vi: set ft=ruby :
require 'json'
require 'yaml'
VAGRANTFILE_API_VERSION ||= "2"
confDir = $confDir ||= File.expand_path(File.dirname(__FILE__))
homesteadYamlPath = confDir + "/Homestead.yaml"
homesteadJsonPath = confDir + "/Homestead.json"
afterScriptPath = confDir + "/after.sh"
customizationScriptPath = confDir + "/user-customizations.sh"
aliasesPath = confDir + "/aliases"
require File.expand_path(File.dirname(__FILE__) + '/scripts/homestead.rb')
Vagrant.require_version '>= 2.2.4'
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
if File.exist? aliasesPath then
config.vm.provision "file", source: aliasesPath, destination: "/tmp/bash_aliases"
config.vm.provision "handle_aliases", type: "shell" do |s|
s.inline = "awk '{ sub(\"\r$\", \"\"); print }' /tmp/bash_aliases > /home/vagrant/.bash_aliases && chown vagrant:vagrant /home/vagrant/.bash_aliases"
end
end
if File.exist? homesteadYamlPath then
settings = YAML::load(File.read(homesteadYamlPath))
elsif File.exist? homesteadJsonPath then
settings = JSON::parse(File.read(homesteadJsonPath))
else
abort "Homestead settings file not found in #{confDir}"
end
Homestead.configure(config, settings)
if File.exist? afterScriptPath then
config.vm.provision "Run after.sh", type: "shell", path: afterScriptPath, privileged: false, keep_color: true
end
if File.exist? customizationScriptPath then
config.vm.provision "Run customize script", type: "shell", path: customizationScriptPath, privileged: false, keep_color: true
end
if Vagrant.has_plugin?('vagrant-hostsupdater')
config.hostsupdater.remove_on_suspend = false
config.hostsupdater.aliases = settings['sites'].map { |site| site['map'] }
elsif Vagrant.has_plugin?('vagrant-hostmanager')
config.hostmanager.enabled = true
config.hostmanager.manage_host = true
config.hostmanager.aliases = settings['sites'].map { |site| site['map'] }
elsif Vagrant.has_plugin?('vagrant-goodhosts')
config.goodhosts.aliases = settings['sites'].map { |site| site['map'] }
end
if Vagrant.has_plugin?('vagrant-notify-forwarder')
config.notify_forwarder.enable = true
end
end
Debug output
Expected behaviour
The program in the session should be terminated
Actual behaviour
The whole ssh session ends
Steps to reproduce
- in the homestead directory
vagrant ssh less- hit Ctrl+C
References
None found
I am having the same issue (with vagrant 2.2.19) Ssession exits upon pressing ctrl+c with cygwin bash shell and cygwin openssh / ssh client. However when using vagrant supplied ssh client via windows cmd.exe shell ( not using cygwin ) then the ctrl+c does not kill the ssh client session. Additionally, as others reported (#12838), if I launch cygwin ssh directly (not via vagrant) then the ssh client behaves as expected and does not exit session when pressing ctrl+c. So it appears that the problem is related to the way vagrant is launching the ssh task.
@dmeador Can confirm this is also happening in cygwin running in Windows Terminal Preview 1.16.2641.0 (don't have bash/cygwin revs handy but not as new) when using vagrant ssh.
edit: I just launched cmd instead of bash via Wintendo Terminal and it handle's ^C just like any other session.
@chrisroberts I'd been looking into this issue myself but thought it was Windows Terminal as a root cause. This same issue is happening to git-bash on windows, so you may want to look to them for solution. Just a thought. Thank you!
Additional info, when I run "vagrant ssh --debug", the vagrant script calls the following command line for ssh, and this command will terminate the session with ctrl+c.
DEBUG safe_exec: Converted - Command: "C:\\cygwin64\\bin/ssh.EXE" Args: ["[email protected]", "-p", "2222", "-o", "LogLevel=FATAL", "-o", "Compression=yes", "-o", "DSAAuthentication=yes", "-o", "IdentitiesOnly=yes", "-o", "StrictHostKeyChecking=no", "-o", "UserKnownHostsFile=/dev/null", "-i", "D:/<user_path>/.vagrant/machines/default/virtualbox/private_key"]
However, as I stated above, when execute the command directly (ie not through vagrant), then ctrl+c does not cause termination of ssh session: Working command (after vagrant up is called): ssh [email protected] -p 2222 -o "LogLevel=FATAL" -o "Compression=yes" -o "DSAAuthentication=yes" -o "IdentitiesOnly=yes" -o "StrictHostKeyChecking=no" -o "UserKnownHostsFile=/dev/null" -i "<path_to_private_key>/.vagrant/machines/default/virtualbox/private_key"
Pressing ctrl+c on the above does not exit the ssh session. Also " which ssh" returns cygwin supplied ssh binary: /usr/bin/ssh
Any time I run ssh manually outside of the 'vagrant' wrapper script, the ssh session does not exit with ctrl+c. Hope that helps.
I can conclude that vagrant ssh command is broken, here is my workaround
sed -i '1i Include config.d/*' ~/.ssh/config
vagrant ssh-config --host my-vm > ~/.ssh/config.d/my-vm
ssh my-vm
In short the solution is:
Take a terminal that works.
- I find one working
gitbash v2.32.0.windows..
The latest available gitbash currently is v2.38.1. But only the old one is working correctly with Vagrant. The strange thing is that the latest one (gitbash v2.38.1) is working fine with SSH connections to AWS EC2 instances.
- Alternatively.
Windows PowerShellis working fine with vagrant.
If someone needs my bad experience, here it is. The following terminals DON'T WORK
- gitbash v2.38.1 (latest for now)
- gitbash V2.36.0
- ConEmu v220807 Alhpa (latest for now)
- cmder v1.3.20.1282 (latest for now)
I confirm this issue with:
- Windows 7
C:\Windows\system32\cmd.exe(Microsoft Windows [Version 6.1.7601]) - Vagrant 2.3.3
Also, with git-cmd and all kinds of terminals from a vanilla VSCode.
The only one that works for me is git-bash.exe.
The version I was using when I had the problem was as follows:
- vagrant: 2.3.1
- git bash(windows 11): Git-2.39.0.2-64-bit.exe
After I rolled back the git bash version, I didn't have this problem:
- vagrant: 2.3.1
- git bash(windows 11): Git-2.24.0.2-64-bit.exe
I can conclude that
vagrant sshcommand is broken, here is my workaroundsed -i '1i Include config.d/*' ~/.ssh/config vagrant ssh-config --host my-vm > ~/.ssh/config.d/my-vm ssh my-vm
This is handy. Minimally, if you are isolated in cygwin for instance, just go like this:
vagrant ssh-config > ~/.ssh/config ssh default
Done. Later you can name the host if you really have multiple ones, --host just names it whatever, has nothing to do with the box, just your ssh config.
Here's a workaround: vagrant ssh-config >> ~/.ssh/config ssh default
I can confirm that we are having the issue as well. The previous suggested solution is a work-around. I use this config:
- Windows 10
- vagrant 2.3.7
- Windows Terminal 1.17
- Powershell as starting-point for
vagrant ssh
In case you don't want ssh config to go outside your project directory you can use this variant of the workaround
vagrant ssh-config > ./ssh_config
ssh -F ./ssh_config default
I fixed this issue on my PC installing the Windows optional feature "OpenSSH client" and adding to the System Path the line "C:\Windows \system32\OpenSSH" on top. In this way, vagrant ssh works flawlessly.
Here's a workaround: vagrant ssh-config >> ~/.ssh/config ssh default
Thanks this one worked for me as well