The Problem
Following messages are logged when restarting sshd service in a CentOS/RHEL 5/6 system.
Dec 14 00:15:19 geeklab sshd[9182]: Received signal 15; terminating. Dec 14 00:15:19 geeklab sshd[9274]: Server listening on :: port 22. Dec 14 00:15:19 geeklab sshd[9274]: error: Bind to port 22 on 0.0.0.0 failed: Address already in use.
The Solution
When IPv6 and IPv4 both are enabled simultaneously, IPv6 first binds on port 22 to all available IPv6 IPs (::) when sshd service is started. So when IPv4 IPs (0.0.0.0) try to bind on the same port, the system would log the above error message. That’s because the port is already being used by IPv6 IPs.
You can check the same thing in the netstat output as well.
# netstat -anp | grep sshd tcp 0 0 :::22 :::* LISTEN 9302/sshd
In order to resolve the issue, follow the steps outlined below:
1. Edit the sshd configuration file /etc/ssh/sshd_config and uncomment the below given line:
# vi /etc/ssh/sshd_config ListenAddress 0.0.0.0
2. Restart the sshd service again for the changes to take effect.
# service sshd restart
3. Logs similar to below should be logged in /var/log/messages file.
Dec 14 00:35:23 geeklab sshd[9274]: Received signal 15; terminating. Dec 14 00:35:23 geeklab sshd[9301]: Server listening on 0.0.0.0 port 22.
4. You may also check the netstat command output to confirm if sshd port 22 is listening to IPv4 Addresses only.
# netstat -anp | grep sshd tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 8977/sshd