Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keep SSH running on Windows 10 Bash

I am having a problem keeping SSH running on the Windows Subsystem for Linux. It seems that if a shell is not open and running bash, all processes in the subsystem are killed. Is there a way to stop this?

I have tried to create a service using nssm but have not be able to get it working. Now I am attempting to start a shell and then just send it to the background but I haven't quite figured out how.

like image 210
MrJagaloon Avatar asked Feb 09 '17 16:02

MrJagaloon


People also ask

How do I keep SSH connection alive on Windows 10?

PuTTY keep connections alive Load your connection session. In the Category pane, click Connection. Under "Sending of null packets to keep session active", in the "Seconds between keepalives", type 240 for 4 minutes (or less). PuTTY will now send a packet to the server every 240 seconds to keep the connection alive.


2 Answers

You have to keep at least one bash console open in order for background tasks to keep running: As soon as you close your last open bash console, WSL tears-down all running processes.

And, yes, we're working on improving this scenario in the future ;)

Update 2018-02-06

In recent Windows 10 Insider builds, we added the ability to keep daemons and services running in the background, even if you close all your Linux consoles!

One remaining limitation with this scenario is that you do have to manually start your services (e.g. $ sudo service ssh start in Ubuntu), though we are investigating how we might be able to allow you to configure which daemons/services auto-start when you login to your machine. Updates to follow.

like image 115
Rich Turner Avatar answered Sep 27 '22 17:09

Rich Turner


To maintain WSL processes, I place this file in C:\Users\USERNAME\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\wsl.vbs

set ws=wscript.createobject("wscript.shell")
ws.run "C:\Windows\System32\bash.exe -c 'sudo /etc/rc.local'",0

In /etc/rc.local I kick off some services and finally "sleep" to keep the whole thing running:

/usr/sbin/sshd
/usr/sbin/cron
#block on this line to keep WSL running
sleep 365d

In /etc/sudoers.d I added a 'rc-local' file to allow the above commands without a sudo password prompt:

username * = (root) NOPASSWD: /etc/rc.local
username * = (root) NOPASSWD: /usr/sbin/cron
username * = (root) NOPASSWD: /usr/sbin/sshd

This worked well on 1607 but after the update to 1704 I can no longer connect to wsl via ssh.

Once you have cron running you can use 'sudo crontab -e -u username' to define cron jobs with @reboot to launch at login.

like image 21
hsmiths Avatar answered Sep 27 '22 17:09

hsmiths