ssh.service startup failure; port 22 already in use

Brand new ubuntu installation:

$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 20.04.1 LTS
Release: 20.04
Codename: focal

ssh.service fails to startup due to port 22 already in use:

$ sudo service ssh status
● ssh.service - OpenBSD Secure Shell server Loaded: loaded (/lib/systemd/system/ssh.service; enabled; vendor preset: enabled) Active: failed (Result: exit-code) since Thu 2021-02-04 14:10:27 CST; 11min ago Docs: man:sshd(8) man:sshd_config(5) Process: 2041 ExecStartPre=/usr/sbin/sshd -t (code=exited, status=0/SUCCESS) Process: 2042 ExecStart=/usr/sbin/sshd -D $SSHD_OPTS (code=exited, status=255/EXCEPTION) Main PID: 2042 (code=exited, status=255/EXCEPTION)
Feb 04 14:10:27 ubu20a systemd[1]: Starting OpenBSD Secure Shell server...
Feb 04 14:10:27 ubu20a sshd[2042]: error: Bind to port 22 on 0.0.0.0 failed: Address already in use.
Feb 04 14:10:27 ubu20a sshd[2042]: error: Bind to port 22 on :: failed: Address already in use.
Feb 04 14:10:27 ubu20a sshd[2042]: fatal: Cannot bind any address.
Feb 04 14:10:27 ubu20a systemd[1]: ssh.service: Main process exited, code=exited, status=255/EXCEPTION
Feb 04 14:10:27 ubu20a systemd[1]: ssh.service: Failed with result 'exit-code'.
Feb 04 14:10:27 ubu20a systemd[1]: Failed to start OpenBSD Secure Shell server.

Only thing I see listening on port 22 is 1/init:

$ sudo netstat -tulpen
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State User Inode PID/Program name
tcp 0 0 127.0.0.53:53 0.0.0.0:* LISTEN 101 34051 556/systemd-resolve
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 0 37066 745/cupsd
tcp6 0 0 :::22 :::* LISTEN 0 34718 1/init
udp 0 0 0.0.0.0:5353 0.0.0.0:* 115 37068 743/avahi-daemon: r
udp 0 0 0.0.0.0:52172 0.0.0.0:* 115 37070 743/avahi-daemon: r
udp 0 0 127.0.0.53:53 0.0.0.0:* 101 34047 556/systemd-resolve
udp 0 0 0.0.0.0:631 0.0.0.0:* 0 39715 822/cups-browsed
udp6 0 0 :::5353 :::* 115 37069 743/avahi-daemon: r
udp6 0 0 :::39012 :::* 115 37071 743/avahi-daemon: r 

While I can get ssh.service to startup on an alternate port (eg, port 2020) I'd like to understand, and fix (if possible), this current issue with port 22.

NOTES:

  • linux sysadmin is not my strong point so if I'm missing something stupidly obvious ... go ahead and shoot the messenger
  • if the answer is RTFM then a link to which FMTR would be appreciated
  • I looked through the first dozen or so suggested 'duplicate' questions but didn't find anything applicable to this issue
2

4 Answers

A bit long for a comment ...

I took @steeldriver's suggestion and ran the suggested systemctl call:

$ systemctl -all list-sockets
LISTEN UNITS ACTIVATES
... snip ...
/run/udev/control systemd-udevd-control.socket systemd-udevd.service
/run/uuidd/request uuidd.socket uuidd.service
[::]:22 tinysshd.socket
audit 1 systemd-journald-audit.socket systemd-journald.service
kobject-uevent 1 systemd-udevd-kernel.socket systemd-udevd.service 

Of interest is the 3rd line up from the bottom ... something called tinysshd listening on port 22.

I couldn't find any explanation for what tinysshd does (ie, doesn't appear to be a system required service) so I removed tinysshd (sudo apt remove tinysshd) and was able to successfully start the ssh service:

$ sudo service ssh status
● ssh.service - OpenBSD Secure Shell server Loaded: loaded (/lib/systemd/system/ssh.service; enabled; vendor preset: enabled) Active: active (running) since Thu 2021-02-04 15:14:34 CST; 3s ago Docs: man:sshd(8) man:sshd_config(5) Process: 3887 ExecStartPre=/usr/sbin/sshd -t (code=exited, status=0/SUCCESS) Main PID: 3888 (sshd) Tasks: 1 (limit: 7032) Memory: 1.3M CGroup: / └─3888 sshd: /usr/sbin/sshd -D [listener] 0 of 10-100 startups
Feb 04 15:14:34 ubu20a systemd[1]: Starting OpenBSD Secure Shell server...
Feb 04 15:14:34 ubu20a sshd[3888]: Server listening on 0.0.0.0 port 22.
Feb 04 15:14:34 ubu20a sshd[3888]: Server listening on :: port 22.
Feb 04 15:14:34 ubu20a systemd[1]: Started OpenBSD Secure Shell server.

I can also see sshd listening on port 22:

$ sudo netstat -tulpen
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State User Inode PID/Program name
...snip ...
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 0 66119 3888/sshd: /usr/sbi
tcp6 0 0 :::22 :::* LISTEN 0 66121 3888/sshd: /usr/sbi
... snip ...

try to identify which app allocate port via this command:

 sudo lsof -i -P -n | grep LISTEN 

and make design what to do next

Another thought as I was having the same issue. When I enabled ssh on my router it automatically took port 22 for ssh to the router and that wasn't changable. I could never make ssh to a computer work until I realized make a port trigger so another port say for example 2222 went to 22 local of that computer and voila I had ssh.

I'm using WSL2 & Ubuntu 20.04.

I found the answer by kbulgrien at Unix StackExchange to be my issue: that systemd-user-sessions.service isn't being called automatically. The only way I have figured out to get it to run automatically is to add the line

ExecStartPost=systemctl start systemd-user-sessions.service

in the [Service] section of /lib/systemd/system/ssh.service . But I've yet found a way that will allow SSH Server to start automatically... :( I still have to start it manually via sudo systemctl start sshd.service

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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

You Might Also Like