9

How do you run an SSH daemon on a different port other than the standard 22?

I screwed up something with my SSH configuration on a cloud VM, so that now SSH always prompts me for a password. My early attempts to fix this locked me out, forcing me to umount the disk, mount it inside a working VM, and fix the files to a known working state. However, it's still forcing me to enter a password.

I'd like to update my /etc/ssh/sshd_config and restart the main ssh daemon, which I can test against, while still having another one running, using the old configuration, that I can use to revert the configuration in case the first one locks me out. How would I do this, specifically on Ubuntu?

  • 3
    Note that stopping sshd does not kill any established connections so you won't be locked out if you have an existing session opened prior to making changes. – Grisha Levit Jan 18 '17 at 0:02
  • Snapshot the virtual machine then if you need to revert the changes you can do it by rebooting from the image. – A E Jan 18 '17 at 13:06
  • 2
    "configuration on a cloud VM" - Then just use the console they give you - no 2nd SSH needed. The console is there specifically for when you lock yourself out. – SnakeDoc Jan 18 '17 at 17:10
17

You didn't mention an Ubuntu version, so I am not sure which init system you are running.

If systemd, you may have a file /etc/systemd/system/sshd.service. Which you could make a copy of as /etc/systemd/system/sshd_alt.service. Then adjust the ExecStart line and add a -p 22000 or something. After that do systemctl enable sshd_alt and systemctl start sshd_alt. You could also point at a completely different configuration file.

You could also just manually start a copy in screen or something if this is just a one time thing. Just start screen and do something like /usr/sbin/sshd -D -p 22200. You will start a spare sshd daemon until you can re-attach to that screen and kill the process.

Or like @EEAA said. Simply don't disconnect, use a second session to test that you can reconnect.

11

You can start an sshd on an alternate port with this command:

sshd -p 12345

Replace 12345 by your favorite port number. If sshd is not in your PATH, you may need to use the full pathname in the command, e.g. /usr/sbin/sshd.

  • 1
    Regardless of path, you need /usr/sbin/sshd, otherwise you just get error: sshd re-exec requires execution with an absolute path – Lenne Jan 18 '17 at 16:21
  • 1
    /usr/sbin/sshd -p12345 -f path_to_working_cfg.file – Lenne Jan 18 '17 at 16:22
  • @Lenne: I've never hit that issue. – R.. GitHub STOP HELPING ICE Jan 18 '17 at 16:23
  • @R.. This comes from OpenSSH v 3.9 and forwards. See unix.stackexchange.com/a/109383/21109 for more info. – Jenny D Jan 19 '17 at 12:56
  • How ridiculous. They could just assume the one in the path is right if it was executed without an absolute path, and/or use /proc/self/exe if available. – R.. GitHub STOP HELPING ICE Jan 19 '17 at 13:24
9

Just leave one SSH session active and use another to make your configuration changes. Your original session will remain connected and available to revert changes if your config changes break something.

  • When doing this I'll open a spare session, just in case something happens to the first one before I have the new configuration working. It's such simple insurance against having to rescue the VM when you accidentally close your shell because your had the wrong window focussed when you hit cmd-w or ctrl-f4. – StvnW Jan 18 '17 at 0:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.