Linux – SSH daemon not starting

linuxssh

Hi i am not able start the sshd on redhat 6.9. Here is the logs

When i try restarting service i get following error logs.

/var/log/messages

Mar  5 06:49:31 c2t26451 init: sshd main process (19225) terminated with status 255
Mar  5 06:49:31 c2t26451 init: sshd main process ended, respawning

/var/log/secure

Mar  5 07:37:54 c2t26451 sshd[31840]: error: Bind to port 22 on 0.0.0.0 failed: Address already in use.
Mar  5 07:37:54 c2t26451 sshd[31840]: fatal: Cannot bind any address.

Here is the service status and sshd process

[ ~]$ sudo service sshd status
openssh-daemon is stopped
[ ~]$ ps -ef|grep ssh
root     22036 28601  0 05:26 ?        00:00:00 sshd: testuser [priv]
testuser 22042 22036  0 05:26 ?        00:00:00 sshd: testuser@pts/0
testuser 22166 22043  0 05:29 pts/0    00:00:00 grep ssh
root     28601     1  0 Mar05 ?        00:00:00 /usr/sbin/sshd -D

Though one process is already running and i am wondering if i kill it i would be locked out of server permanently. Also i am not sure if i reboot the server how the effect would.

Best Answer

The reason you don't see it as a service, is that sshd is running as a regular process instead of service/daemon. From man sshd:

 -D      When this option is specified, sshd will not detach and does not become a daemon.  This
         allows easy monitoring of sshd.

You can check if any other network services are using the same port by issuing netstat command:

netstat -lntup
or
netstat -lntup | grep ':22'

What you could do is create another sshd config file with a different name and different port number. Remember to open that port in iptables. Start the other instance with new config specified:

sudo sshd -D -f my_new_config

This will start sshd the same way, as a process. Connect via ssh on your new port number and kill the other session - make sure you're killing the right one! Also, keep the first session opened just in case.

Now start your default sshd as a service, not a daemon and enable it to start with system boot.

service sshd start
chkconfig --add sshd
chkconfig --level 2345 sshd on

Now, as per your further comments: It seems, that something is restarting sshd when killed. Might be its own config file or sshd itself, if I understand correctly (again, referring to man sshd):

sshd
     rereads its configuration file when it receives a hangup signal, SIGHUP, by executing itself with the
     name and options it was started with, e.g. /usr/sbin/sshd

Questions to OP:

Would you mind posting your sshd config file, preferably with all sensitive details "censored"? Is it a fresh OS install or a server that you "inherited"?

Update:

I've ran a simple test, because that -D option got me thinking: it should not detach and run in the background.

cp sshd_config test_sshd    # copied config, changed port number
/usr/sbin/sshd -D -f test_sshd
ps -ef | grep sshd
root      1842  1718  0 12:03 pts/5    00:00:00 /usr/sbin/sshd -D -f test_sshd

If I kill this process from another session or Ctrl-c in the session where i ran /usr/sbin/sshd -D -f test_sshd, normal behaviour is this process closes and does not restart.

kill 1769   # or Ctrl-c
ps aux | grep 'sshd -D'
root      1848  0.0  0.0 103324   880 pts/6    S+   12:08   0:00 grep sshd -D

This would suggest that you have some other process, script, watchdog running that is restarting sshd as a regular process when it fails, gets killed or exits in other kind of way. Please look into your cron or anacron entries as root user, check running processes for screen or tmux.

crontab -l
cat /etc/anacrontab
ps -ef | grep -e 'screen' -e 'tmux'