I have an ssh session and I'm running a script.  I want to terminate the script without exiting the ssh session.  If I run the script in a local PC, I press Ctrl+C and the script stops, while if I press Ctrl+C in remote session I close the ssh session itself (and also the script in this case).  How can I stop the script in remote shell without interrupting the ssh connection?

I connect with the usual ssh user@ip command, in a standard Ubuntu distribution, using the Git Bash from Windows.

In my Windows client the .ssh/config is as follows:

Host 192.168.XXX.YYY
  HostName 192.168.XXX.YYY
  User auser
  ForwardAgent yes

Host 192.168.XXX.ZZZ
  HostName 192.168.XXX.ZZZ
  User auser

They are not related to the remote server that I'm using.

  • 3
    What about CTRL-Z to put it into the background, then kill it? Although I am not sure why CTRL-C would kill the ssh session. Commented Feb 3, 2022 at 19:00
  • It's strange that Ctrl-C closes the ssh session. I never experienced this, pressing Ctrl-C in a ssh session always worked correctly for me, ie. did break the command running in the ssh session, but didn't exit the session. You must have configured something in a quite nonstandard way...
    – raj
    Commented Feb 3, 2022 at 19:02
  • 3
    What exact local command (ssh …) do you use to connect? Does the command give you an interactive shell and then you run the script by hand? How exactly do you run it? Are there any custom settings in your (local) ~/.ssh/config and/or /etc/ssh/ssh_config that apply to the connection? What is your local OS? What is the remote OS? Please edit the question and be specific. Commented Feb 3, 2022 at 19:08
  • @KamilMaciorowski I connect to remote server by using ssh user@ip, then I go manually to the folder where the python script is and I run it with python3 myscript.py. At this point the script is running, and if I try to stop it with Ctrl+C the session ends.
    – Jepessen
    Commented Feb 3, 2022 at 19:46
  • You say you logged into the shell and then started the script, its easy to write an example (with either shell or python) to show this is not normal behavior. Sounds to me like maybe that myscript.py is doing something in response to KeyboardInterrupt or a Ctrl-C INT signal, like killing the parent process.
    – mpe
    Commented Feb 3, 2022 at 22:13

3 Answers 3


I got same behavior after installed Git for Windows

I can avoid it after downgrade to version 2.34.1 from here.

  • I have the same behavior on version 2.38.1.windows.1. I find it working fine on v2.32.0, but I like your version (2.34.1) more. Thank you Commented Nov 17, 2022 at 22:28

I encountered this today as well — it appears to be a bug in CtrlC handling with the bundled /usr/bin/ssh in Git for Windows. Within Git bash, using its pre-packaged ssh the session would terminate when pressing CtrlC, rather than sending that to the remote session to interrupt what was running there. However, if I ran /c/Windows/System32/OpenSSH/ssh, CtrlC behaved as expected. You can see which ssh is executing by typing which ssh in the bash session.

I corrected the problem by re-installing Git for Windows v2.35.1.2 — deselect the "Show only new options" checkbox so you can reconfigure everything, and as you install one of the options will be "Use external OpenSSH". You must already have OpenSSH installed on your Windows system for this to work, of course — you can do that on a Windows 10/11 box by going to Settings→Apps→Optional Features→Add An Optional Feature and selecting the "OpenSSH Client".

Update: I also reported the issue and it has been fixed in the latest snapshot. So if you'd prefer to remain on the bundled ssh it should be fixed in the next release.

  • I have the same behavior on version 2.38.1.windows.1. So it is not fixed yet. I also tried your suggestion with "Use external OpenSSH" - it did not help, but make it worse, it is not only throwing me out on <ctrl>+<c>, but also does not show half a characters in the command line. What was the latest version when you see it was working? Commented Nov 17, 2022 at 22:44

gitbash is glitchy indeed on this matter. Try Windows PowerShell. It is waking fine.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .