-
Notifications
You must be signed in to change notification settings - Fork 857
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
How to run sshd as a windows service ? #612
Comments
I would suggest instead running urxvt in xming, because sshd needs to be run as root, and there are annoying problems that can arise with that. Right now there is no support for running daemons as windows services because WSL is meant for developer workstations rather than servers, although some unprivileged daemons (like urxvtd) can be run silently with a little bit of workaround. |
That is not even remotely accurate. There are no annoying problems with running sshd as root ( |
Yeah, but I run urxvtd on startup with run.exe, whereas to start the sshd service you have to type a p/w (because you are sudoing it "superuser do", i.e. running it as the root user). I have a shortcut that does
in the startup folder for my Windows user (in addition to running Xming on startup), so I can launch urxvtc whenever I want, with the daemon running totally silently with I'm not saying you can't run sshd, just that you can't do it silently so it "appears like" a Windows service, and the reason why is that you have to type in your sudo password. Sure, I guess you could get around this maybe with some kind of batch file that types in your sudo password? |
Or you could modify your sudoers file to not require a password for that command. |
Yeah, that works too! Good idea! Edit: How did you do it? I set NOPASSWD mode for /bin/service and it still asks me for a pw. |
Make sure you have the syntax right? Test it on an actual Ubuntu machine if you have one available; the sudoers file format can be finicky to get right. (Disclaimer: I haven't actually tried this particular command. I've run other commands with passwordless sudo under WSL, though. |
@therealkenc @aseering, @Manouchehri says in the cmake thread that you also need to star the sshd service in a bash.exe terminal that also has Windows admin credentials in order to open up a socket for sshd. Have you guys found a way around that? |
Can a normal user can ever bind on a system port on Windows? |
@Manouchehri I think so. Bittorrent clients can, and they can even make UPnP requests, I think. |
BitTorrent (shouldn't) run on system ports (0-1024). Most clients bind on a UDP port in the dynamic port range. |
@fpqc -- could you run on a higher port? The problem is that the default sshd port is in the system-port range, as @Manouchehri said. I often bind servers to port 2222, or some other higher-numbered port, to work around that limitation. |
@fpqc, I'm not running WSL in terminal with Windows admin privileges. It binds to 22 fine with |
@therealkenc Permission issues on the Windows or Linux side? ubuntu@DESKTOP-3RQO5S5:~$ ls -l /dev/tty
crw------- 1 ubuntu tty 4, 1 Jul 2 22:23 /dev/tty |
@therealkenc It seems to work for me too doing that, but I have a problem connecting to it with PuTTY, and if I open another terminal and try to connect it appears to connect but then dies.
|
Looks like we actually can bind to 22/TCP without Windows admin privs? ubuntu@DESKTOP-3RQO5S5:~$ sudo strace netcat -l -p22 -w5
sudo: unable to resolve host DESKTOP-3RQO5S5
execve("/bin/netcat", ["netcat", "-l", "-p22", "-w5"], [/* 14 vars */]) = 0
brk(NULL) = 0x1441000
access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
# ...snip...
brk(0x1462000) = 0x1462000
socket(PF_INET, SOCK_STREAM, IPPROTO_TCP) = 3
setsockopt(3, SOL_SOCKET, SO_REUSEADDR, [1], 4) = 0
bind(3, {sa_family=AF_INET, sin_port=htons(22), sin_addr=inet_addr("0.0.0.0")}, 16) = 0
listen(3, 1) = 0
accept(3, {sa_family=AF_INET, sin_port=htons(16627), sin_addr=inet_addr("127.0.0.1")}, [16]) = 4
close(3) = 0
poll([{fd=4, events=POLLIN}, {fd=0, events=POLLIN}], 2, 5000) = 1 ([{fd=4, revents=POLLIN}])
read(4, "Yep, this works.\n", 2048) = 17
write(1, "Yep, this works.\n", 17Yep, this works.
) = 17
poll([{fd=4, events=POLLIN}, {fd=0, events=POLLIN}], 2, 5000) = 0 (Timeout)
close(4) = 0
close(3) = -1 EBADF (Bad file descriptor)
close(3) = -1 EBADF (Bad file descriptor)
exit_group(0) = ?
+++ exited with 0 +++ |
@Manouchehri - I see what's going on with my /dev/tty now. I start bash.exe with user root and then @fpqc - dunno what exactly is going on there. I just followed the secret handshake in #300 and never really looked back since then. In short:
|
@therealkenc Yeah that works, but any idea why adding my PuTTY key to known_hosts doesn't allow me to connect password-free from PuTTY? |
Follow these instructions (or others like it) and you should be good. |
I was able to make sshd run automatically as a foreground task, but only after login. After many failed attempts I set my computer to login automatically as the user I wanted. Here is the process that I used: Add the following line to
And then use the Task Scheduler to create a new basic task with these settings:
Test it by clicking on the Task Scheduler LIbrary, selecting your new task and clicking Run on the right-hand side. A command prompt window should open and remain open. If you want to hide the window, I think you can tick the Hidden box in the scheduled task properties. |
@qris I would also suggest running it with run.exe (available on the xming project page) to hide the console if necessary. |
Thank you everyone, you have helped me a lot. I use two scripts to achieve my goals: script 1 named startssh.bat, the contents is : script 2 named autostartssh.vbe, the contents is : the script 2 can run script 1 like a deamon, then I add scipt 2 to a Windows Task Scheduler, it will run on the computer starts. |
@qris - When I change the setting to "Run whether the user is logged in or not," the task no longer starts, (at least when I click on it and choose "Run"). It works fine when set to "Run only when user is logged on". Any thoughts? Can anyone else replicate this behavior in Task Scheduler, or make a task start properly with "whether user is logged on or not"? |
Thanks for the help, but I'm having an odd problem.. When I try to ssh in from my remote ubuntu laptop, it does not recognize my credentials. However, somehow it recognizes my windows user credentials, and when I ssh in I am dropped into a cmd shell - not bash.. I'm very confused, any help would be appreciated. EDIT: fixed my problem, someone had enabled an ssh daemon in windows already which was conflicting with the WLS one, so I moved it to a different port. They both work now. |
@rodrymbo I've been able to autostart sshd whether user is logged on or not but only by stopping lxssmanager first, i.e. point taskschd to a batch script with the following content: sc stop lxssmanager but (!) after my subsequent login, I cannot start bash unless I issue the same stop command. |
@hacnet I mean, it's software in development that has only been publicly available for 3 or 4 months (over which time it has advanced in leaps and bounds). I think you have to accept that there will be rough edges for some time into the future. (For example, you may remember testing Mozilla Firebird back a million years ago, and that took quite a while to reach maturity (before falling off a cliff and murdering the interface (and version numbering system) in recent years), and that project wasn't a quarter as ambitious as this one.) |
@fpqc while I appreciate that dev environments may have some rough edges, you don't start off the project by breaking the fundamental access method to the brand new shell you're announcing. |
@kabinpokhrel Either use vbscript or run.exe to start bash.exe as a hidden window, and have it launch with a script that starts your daemons. |
There are multiple solutions to having sshd (and other services/daemons) start when the user logs in. They stay running as long as you don't close the last bash.exe window (easiest if you use one of the tricks to hide it) or log out. Start them manually with one or more bash scripts (such as can be found in /etc/init.d) since upstart/systemd isn't running and the There seem to be no reasonable solutions (yet) to having sshd start before the user logs in (that is, just after the system boots, as if it were a real system service) or (probably) to allow a different user's WSL environment to run a script while the first user is still logged in. For that we need to wait until @benhillis and the other devs figure out (a) if they want to allow that and (b) what the best way is to provide the feature. They keep teasing us, but so far haven't committed one way or another. |
@rodrymbo There is a good solution imo for that. Create a second unprivileged user, set up WSL, and then start WSL for that user as a task on system startup. When you want to use WSL, connect to the sshd with PuTTY. |
SSH into "Bash on Ubuntu on Windows"Steps
|
Nice steps there @kabinpokhrel. Can we get some instructions on how to make the filesystem appear as a unix fs? Eg, I'd like to be able to: |
Note that Windows now runs an SSH server on port 22, so it may not be possible to get OpenSSH running on that port too. So you may have to change the You can tell if this is happening because you will need to log in using your Windows username and password (not your UNIX username and password) and when you do, you'll get a |
@ylluminate - Are you saying that the standard ssh server that is part of WSL doesn't do SCP for you? I'm pretty sure I've used SCP with WSL's ssh server and it looked like the FS layout you describe. Remember, the "other" ssh server is not the same as the ssh server within WSL. If you don't like how that other server behaves, you'd need to complain somewhere else. And, as @qris says, they can't both be running on the same port at the same time. But then no ssh server should listen on port 22 anyway. |
On the newest WSL, So |
@gdh1995 Even if you drop your last bash.exe Window? That has always been the problem. |
@fpqc you are right. Oh my god, the single |
@gdh1995 Yeah, that was always the problem. I didn't even know about the sshd -D thing. |
When I follow kabinpokhrel's steps, I am not able to login into my computer it always give me |
@Luindil - sounds like something odd is going on. You can try To confirm the password, you could try If the permission denied error is from something else, e.g. a file permission, knowing which file, might make solving the issue easier, though if you used If you don't get enough information with ssh -v, or from up to three of the -v's, you might need to look into ways to get sshd to put its log somewhere useful, like see if it puts its log to stdout if you run sshd in the foreground. I've still not gotten syslog (or systemd's journal) working... The |
@Luindil Make sure you disable the SSH related Windows Services that appear to be installed by default (namely, SSH Broker and SSH Proxy). Another option is to switch port. |
Thank you both for answering, I just figured out that it is because the port is already used by windows. When I use e.g. 23 as a port it works like a charm. @qris already mentioned it in an above post. |
@luindil isn't port 23 something super important like smtp? |
This methods are not working for starting a apache2 for example...cause it starts and get closed after start...? |
@sysworx -- there's more discussion of this approach here: https://wsl-forum.qztc.io/viewtopic.php?f=6&t=10 . Read through the comments; in particular, if you have a command that exits immediately, you have to somehow artificially add another command that doesn't exit immediately. |
I also had to enable the ssh port in Windows Firewall for all incoming connections to allow remote logins. |
Hi all, |
Gentlemen, We have for years been running sshd under cygwin. We use a cygrunserv process that runs as local administrator and map all registry settings. Cant we just get a local admin that starts the cron scheduler/services. |
@fenchu not yet. since distros are installed at the user rather than computer level, running as local admin will work weird. Running as your own user but with local admin permission could work but comes with its own problems. |
|
Steps to reproduce:
step 1:
start bash
step 2:
start sshd server
sudo /etc/init.d/ssh start
step 3:
connected to sshd server from xshell (a powerful terminal emulator which better than windows cmd line).
if you don't have xshell , use putty or any other client instead
step 4:
closed the bash cmd window , xshell connection is also closed.
Question:
how do I run sshd as a windows service ?
thanks !
The text was updated successfully, but these errors were encountered: