Skip to content

How to run sshd as a windows service ? #612

Closed

Description

@imjakey

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 !

Activity

  1. fpqc commented on Jul 1, 2016

    @fpqc

    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.

  2. therealkenc commented on Jul 1, 2016

    @therealkenc

    That is not even remotely accurate. There are no annoying problems with running sshd as root (sudo service ssh start); and even if there were, you can run sshd on an unprivileged port no different than urxvtd. For people uninterested in inviting all of X11 to the party, PuTTY and a dozen other SSH clients are lighter weight and make more sense. I use the Chrome Secure Shell Extension. Indeed, X clients have been explicitly identified as low priority for the WSL project, but do run, happily, as a side effect of coincidental socket and pty improvements.

  3. fpqc commented on Jul 2, 2016

    @fpqc

    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

    run.exe bash.exe -c "urxvtd -q -o -f ; bash"

    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
    bash.exe -c urxvtc

    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?

  4. aseering commented on Jul 2, 2016

    @aseering
    Contributor

    Or you could modify your sudoers file to not require a password for that command.

  5. fpqc commented on Jul 2, 2016

    @fpqc

    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.

  6. aseering commented on Jul 2, 2016

    @aseering
    Contributor

    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.

  7. fpqc commented on Jul 3, 2016

    @fpqc

    therealkenc Adam Seering (@aseering), David Manouchehri (@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?

  8. Manouchehri commented on Jul 3, 2016

    @Manouchehri
    Contributor

    Can a normal user can ever bind on a system port on Windows?

  9. fpqc commented on Jul 3, 2016

    @fpqc

    David Manouchehri (@Manouchehri) I think so. Bittorrent clients can, and they can even make UPnP requests, I think.

  10. Manouchehri commented on Jul 3, 2016

    @Manouchehri
    Contributor

    BitTorrent (shouldn't) run on system ports (0-1024). Most clients bind on a UDP port in the dynamic port range.

  11. aseering commented on Jul 3, 2016

    @aseering
    Contributor

    fpqc -- could you run on a higher port? The problem is that the default sshd port is in the system-port range, as David Manouchehri (@Manouchehri) said. I often bind servers to port 2222, or some other higher-numbered port, to work around that limitation.

  12. therealkenc commented on Jul 3, 2016

    @therealkenc

    fpqc, I'm not running WSL in terminal with Windows admin privileges. It binds to 22 fine with sudo service ssh start or however you feel like starting the daemon. You can also start it on 2222 like Adam Seering (@aseering) says, but there's a minor issue with perms on /dev/tty so you still have to run it as root.

  13. Manouchehri commented on Jul 3, 2016

    @Manouchehri
    Contributor

    therealkenc Permission issues on the Windows or Linux side? /dev/tty shows as owned by the user to me.

    ubuntu@DESKTOP-3RQO5S5:~$ ls -l /dev/tty
    crw------- 1 ubuntu tty 4, 1 Jul  2 22:23 /dev/tty
  14. fpqc commented on Jul 3, 2016

    @fpqc

    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.

    fpqc@FPQC-PC:~|⇒  ssh -vvv 127.0.0.1 
    OpenSSH_6.6.1, OpenSSL 1.0.1f 6 Jan 2014
    debug1: Reading configuration data /etc/ssh/ssh_config
    debug1: /etc/ssh/ssh_config line 19: Applying options for *
    debug2: ssh_connect: needpriv 0
    debug1: Connecting to 127.0.0.1 [127.0.0.1] port 22.
    debug1: Connection established.
    debug3: Incorrect RSA1 identifier
    debug3: Could not load "/home/fpqc/.ssh/id_rsa" as a RSA1 public key
    debug1: identity file /home/fpqc/.ssh/id_rsa type 1
    debug1: identity file /home/fpqc/.ssh/id_rsa-cert type -1
    debug1: identity file /home/fpqc/.ssh/id_dsa type -1
    debug1: identity file /home/fpqc/.ssh/id_dsa-cert type -1
    debug1: identity file /home/fpqc/.ssh/id_ecdsa type -1
    debug1: identity file /home/fpqc/.ssh/id_ecdsa-cert type -1
    debug1: identity file /home/fpqc/.ssh/id_ed25519 type -1
    debug1: identity file /home/fpqc/.ssh/id_ed25519-cert type -1
    debug1: Enabling compatibility mode for protocol 2.0
    debug1: Local version string SSH-2.0-OpenSSH_6.6.1p1 Ubuntu-2ubuntu2.7
    debug1: Remote protocol version 2.0, remote software version OpenSSH_6.6.1p1 Ubuntu-2ubuntu2.7
    debug1: match: OpenSSH_6.6.1p1 Ubuntu-2ubuntu2.7 pat OpenSSH_6.6.1* compat 0x04000000
    debug2: fd 3 setting O_NONBLOCK
    debug3: load_hostkeys: loading entries for host "127.0.0.1" from file "/home/fpqc/.ssh/known_hosts"
    debug3: load_hostkeys: loaded 0 keys
    debug1: SSH2_MSG_KEXINIT sent
    Connection closed by 127.0.0.1
    
  15. Manouchehri commented on Jul 3, 2016

    @Manouchehri
    Contributor

    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 +++
  16. 61 remaining items

  17. gdh1995 commented on Nov 13, 2016

    @gdh1995

    fpqc you are right.

    Oh my god, the single sshd process exits at once if I exit bash.exe.

  18. fpqc commented on Nov 13, 2016

    @fpqc

    Dahan Gong (@gdh1995) Yeah, that was always the problem. I didn't even know about the sshd -D thing.

  19. CPrescher commented on Dec 1, 2016

    @CPrescher

    When I follow kabinpokhrel's steps, I am not able to login into my computer it always give me
    "Permission denied, please try again." Even though I am 100% sure that the passowrd is correct, I created new Users and it still did not work. What is the reason for this?

  20. rodrymbo commented on Dec 1, 2016

    @rodrymbo

    @Luindil - sounds like something odd is going on. You can try ssh -v -p [portnumber] [username]@localhost from a different bash.exe window to see more info, maybe it will tell you at what point it is failing. (Spelling everything out on the command line helps check assumptions, and -v adds information that might be helpful.)

    To confirm the password, you could try su - [username] which will ask for the password, unless you are root. Or, it should be asking for the password when you use sudo (until you added the NOPASSWD: setting with visudo). (You do know it is not necessarily the password you use to log in to Windows, right?) To reset the password, you can use lxrun /setdefaultuser root before logging in, then the passwd command, then /setdefaultuser back again. Or set up public keys and authenticate that way.

    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 sudo to start it that shouldn't be a problem.

    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 service command isn't really supported completely (currently), so you could bypass that and just run the bash script sudo /etc/init.d/ssh {start|stop|restart}. Service usually works to start that bash script, but by going directly to the script you bypass one possible source of errors. You could also make a copy of the script and add command line options (e.g. logging?) to troubleshoot.

  21. lypanov commented on Dec 7, 2016

    @lypanov

    @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.

  22. CPrescher commented on Dec 7, 2016

    @CPrescher

    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. Chris Wilson (@qris) already mentioned it in an above post.

  23. fpqc commented on Dec 7, 2016

    @fpqc

    @luindil isn't port 23 something super important like smtp?

  24. aseering commented on Jan 4, 2017

    @aseering
    Contributor

    Sys-worX.net (@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.

  25. repalash commented on Sep 28, 2017

    @repalash

    I also had to enable the ssh port in Windows Firewall for all incoming connections to allow remote logins.

  26. rickywu commented on Nov 14, 2017

    @rickywu

    Hi all,
    I found this tool is very useful and can run ssh server without any window in background

    https://github.com/131/dispatcher

  27. fenchu commented on Dec 5, 2017

    @fenchu

    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.

  28. fpqc commented on Dec 6, 2017

    @fpqc

    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.

  29. therealkenc commented on May 24, 2018

    @therealkenc

    sshd and other processes not attached to a tty now persist in WSL as of April Update.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions