When connecting to an SSH service, sometimes you have to wait a long time before the console becomes available for executing commands. The causes of delays are various, and in this guide, we will look at some reasons and their solutions.
These solutions are generally not related to each other, so it's better to make adjustments and check the operation for each one separately — this way, it will be easier to identify the actual cause.
The commands provided work for Debian/Ubuntu. For CentOS/Alma/Rocky, they might differ slightly. For example, the service might not be called "ssh," but "sshd" — keep this in mind when executing the commands. Otherwise, everything is the same.
1. The most common reason: waiting for the DNS server
Open the SSH service configuration file:
nano /etc/ssh/sshd_config
Disable the UseDNS option so it will not try to get a reverse lookup (PTR) for the IP:
UseDNS no
After making the changes, restart the service:
service ssh restart
2. Connection timeout is not specified
Open the SSH service configuration file:
nano /etc/ssh/sshd_config
Set the parameter as follows:
LoginGraceTime 10s
After making the changes, restart the service:
service ssh restart
3. Long delay related to the systemd-logind service
In this case, the auth.log log usually contains events like:
pam_systemd(sshd:session): Failed to create session: Connection timed out
Check the status of the service:
systemctl status systemd-logind
If necessary, restart it with the restart command.
If the delay persists, open the file using vi or nano:
/etc/pam.d/common-session
Comment out the line with a # symbol:
#session optional pam_systemd.so
Then, restart the service:
systemctl restart systemd-logind
Try again, but remember that with this setup, the console may not work fully, so this option should be considered as a temporary solution.