How to speed up SSH login?

22,062

Solution 1

The number one reason I've seen for this is a configuration option in SSHD UseDNS this option (enabled by default) causes the server to perform DNS resolution on the incoming requests. A time consuming operation. I've seen logins go from one minute plus waiting for password prompt to under a few seconds. If you edit /etc/ssh/sshd_config on the server and add (if it's not there) at the bottom UseDNS no then restart the SSH daemon with service ssh restart you should see an improvement next time you connect.

Solution 2

For me, the reason number 2 (after the server-side UseDNS option) for long SSH session logins are client side attempts to connect using IPv6 (which, obviously, isn't set up correctly on my network - or almost any other network, for that matter).

See HOWTO: Speed up SSH login on Ubuntu forums.

The "solution" is to enable IPv4 only:

1) either for the given SSH client invocation:

ssh -4 login@hostname

2) or globally in ssh client conffiguration in /etc/ssh/ssh_config:

Host *
   AddressFamily inet

Of course, it would be more correct to set up IPv6 on your network properly, but who has the time for that :)

Solution 3

Try adding the next option with your ssh command:

-o "PreferredAuthentications=password"

(this prevents ssh from negotiating any other authentication method, and speeds up the password promtp!)

Solution 4

As you are using a rather old Ubuntu version, it might well be this bug: https://bugs.launchpad.net/ubuntu/+source/openssh/+bug/300151

Restarting dbus (/etc/init.d/dbus restart) might help.

Solution 5

Additionally, type this on the remote machine (as the user you would log in as) to suppress any MOTD messages:

touch ~/.hushlogin

Doesn't make as much a difference as turning off UseDNS but it might help on slower connections.

Share:
22,062

Related videos on Youtube

grokus
Author by

grokus

Updated on September 17, 2022

Comments

  • grokus
    grokus over 1 year

    From outside of my house, whenever I login to my Ubuntu server using SSH, it takes about 6 seconds for me to get the prompt for password, however when I login to my web hosting server it takes about 1 second. What can I do to speed this up?

    $ cat /etc/lsb-release
    DISTRIB_ID=Ubuntu
    DISTRIB_RELEASE=8.04
    DISTRIB_CODENAME=hardy
    DISTRIB_DESCRIPTION="Ubuntu 8.04.1"
    $ ssh -v
    OpenSSH_4.7p1 Debian-8ubuntu1.2, OpenSSL 0.9.8g 19 Oct 2007
    $ cat /proc/cpuinfo
    model name      : Dual-Core AMD Opteron(tm) Processor 1210
    cpu MHz         : 1000.000
    cache size      : 1024 KB
    $ cat /proc/meminfo
    MemTotal:      2074528 kB
    
  • vaquito
    vaquito over 13 years
    Better to fix your DNS configuration.
  • Mike
    Mike over 13 years
    -1: The MOTD is only displayed after a successful login. This will not cause the password prompt to appear any faster.
  • AntonioCS
    AntonioCS over 13 years
    This worked out great!! Thanks! The login time went from 20-30 secs to less than 5 secs :)
  • RusGraf
    RusGraf over 13 years
    What is the benefit of having UseDNS enabled?
  • TJ Ellis
    TJ Ellis over 13 years
    Why use passwords at all? Why not keys?
  • perseo22
    perseo22 over 13 years
    Easy: because you may not be allow to use keys! (sshd not configured to use them, bad user file permissions, etc.9
  • djeikyb
    djeikyb almost 13 years
    I have a 10.04 box upgraded from 9.04 and 9.10. Restarting dbus reduced time ssh remoteServer date from 8s to 0.5s.
  • sk29910
    sk29910 over 12 years
    True, but still a useful comment.
  • Adriano Rosa
    Adriano Rosa over 9 years
    Be aware! when you set the directive UserDNS after a Match directive you'll get an error: is not allowed within a Match block if you restart the sshd you'll not be able to ssh into your server. I did this mistake and I got locked out of my server. Always remember do not add any directive at the bottom of sshd_config if you have Match directive in it.