Diagnose slow SSH and sudo caused by hostname lookups that hit a
timing-out DNS path. Collect before-and-after timing evidence
with time and getent, then restore fast
lookups by fixing resolver settings and reducing reverse-DNS
impact.
Users report that SSH logins and sudo prompts feel
slow on a server that otherwise looks healthy. The symptom
points to name resolution delays, usually caused by a resolver
timeout or reverse DNS lookups on inbound connections.
Treat this like a reliability issue. Capture timing evidence, remove the timeout path from the hot loop, and prove improvement by re-running the same measurements after each change.
time and
getent.
UseDNS no (policy permitting).
getent to test it.
dig.
UseDNS is enabled.
time getent hosts portal.lab.local
This is your baseline. If name service is slow, you will see
seconds of delay in the real time output.
10.0.2.50 portal.lab.local
real 0m2.01s
user 0m0.00s
sys 0m0.00s
resolvectl status
Find the DNS server in use for your active interface. In this lab, the configured resolver is incorrect and points to a server that does not respond.
Link 2 (eth0)
DNS Servers: 10.0.2.99
Current DNS Server: 10.0.2.99
dig @10.0.2.99 portal.lab.local
This isolates the dependency. If the configured DNS server cannot answer, the resolver stack will retry and your interactive workflows will pay the delay.
;; communications error to 10.0.2.99#53: timed out
;; communications error to 10.0.2.99#53: timed out
;; no servers could be reached
nmcli -t -f DEVICE,NAME con show --active | grep '^eth0:'
sudo nmcli con mod "<CONNECTION_NAME>" ipv4.dns "10.0.2.3"
sudo nmcli con up "<CONNECTION_NAME>"
This removes the timeout path by pointing name resolution at a working resolver. Re-apply the connection so the new DNS setting takes effect.
10.0.2.3.
resolvectl status
Link 2 (eth0)
DNS Servers: 10.0.2.3
Current DNS Server: 10.0.2.3
time getent hosts portal.lab.local
The lookup should return quickly now. If it does not, you still have a resolver/search/timeout issue to eliminate.
10.0.2.50 portal.lab.local
real 0m0.01s
user 0m0.00s
sys 0m0.00s
Changing SSH server behavior is an operational decision. Validate the security and auditing impact for your environment before making this permanent.
sudo grep -n '^[#[:space:]]*UseDNS' /etc/ssh/sshd_config
sudoedit /etc/ssh/sshd_config
sudo systemctl reload sshd
Ensure the configuration includes UseDNS no.
This prevents sshd from doing reverse DNS lookups on
connecting clients, which removes a common “SSH feels slow”
failure mode when DNS is unreliable.
# In /etc/ssh/sshd_config
UseDNS no
curl -I http://portal.lab.local
This confirms the hostname resolves and the application path is reachable after DNS changes.
HTTP/1.1 200 OK
The delay may be authentication (SSSD/LDAP), PAM modules, GSSAPI/Kerberos, or sshd settings unrelated to DNS. Check client/server logs and validate authentication paths independently of name resolution.
The host may not be using systemd-resolved. Inspect
/etc/resolv.conf and continue testing with
dig against the configured name servers.
Check multiple unreachable name servers, search domain
delays, or IPv6-related timeouts. Compare timings using
direct queries (for example dig @SERVER NAME)
and remove any server that cannot respond.
This lab is non-destructive unless you changed resolver or SSH settings. If you need to restore the “broken” state for repeat testing, point DNS back to the bad server only in a lab environment.
nmcli -t -f DEVICE,NAME con show --active | grep '^eth0:'
sudo nmcli con mod "<CONNECTION_NAME>" ipv4.dns "10.0.2.99"
sudo nmcli con up "<CONNECTION_NAME>"
time <command>
: Measures elapsed time for a command.
getent hosts <name>
: Resolves a hostname using the system NSS configuration.
resolvectl status
: Shows active resolver configuration (systemd-resolved).
dig @<server> <name>
: Queries a specific DNS server to test reachability and
response.
@<server>
: Sends the query to the specified DNS server.
nmcli con show --active
: Lists active NetworkManager connections.
-t
: Produces terse output (useful for scripting).
-f
: Selects output fields (for example
DEVICE,NAME
).
nmcli con mod "<con>" ipv4.dns "<server>"
: Sets DNS for a connection profile.
ipv4.dns
: Overrides DNS servers for the profile.
nmcli con up "<con>"
: Re-applies a NetworkManager connection profile.
sudoedit <file>
: Edits a file using your editor with elevated privileges.
/etc/ssh/sshd_config
: OpenSSH server configuration file.
systemctl reload sshd
: Reloads sshd configuration without dropping existing
sessions.
curl -I <url>
: Fetches HTTP response headers for quick connectivity
checks.
-I
: Requests headers only.