更改 sshd_config 后 SSH 登录速度非常慢,即使在 LAN 上也是如此

更改 sshd_config 后 SSH 登录速度非常慢,即使在 LAN 上也是如此

我正在尝试让我的家庭服务器能够通过 ssh 连接到互联网。

最初(在接触任何配置文件之前,只需这样做,apt-get install openssh-server我就可以非常快速地(基本上是即时地)在本地局域网上连接到。我这样做了ssh username@localIP,它立即提示我输入密码,然后我就可以登录了。

我将sshd_config文件编辑为以下内容(从默认值):

# Package generated configuration file
# See the sshd_config(5) manpage for details

# What ports, IPs and protocols we listen for
Port 22
# Use these options to restrict which interfaces/protocols sshd will bind to
#ListenAddress ::
#ListenAddress 0.0.0.0
Protocol 2
# HostKeys for protocol version 2
HostKey /etc/ssh/ssh_host_rsa_key
HostKey /etc/ssh/ssh_host_dsa_key
HostKey /etc/ssh/ssh_host_ecdsa_key
HostKey /etc/ssh/ssh_host_ed25519_key
#Privilege Separation is turned on for security
UsePrivilegeSeparation yes

# Lifetime and size of ephemeral version 1 server key
KeyRegenerationInterval 3600
ServerKeyBits 1024

# Logging
SyslogFacility AUTH
LogLevel VERBOSE

# Authentication:
LoginGraceTime 120
PermitRootLogin no
StrictModes yes

RSAAuthentication yes
PubkeyAuthentication yes
#AuthorizedKeysFile %h/.ssh/authorized_keys

# Don't read the user's ~/.rhosts and ~/.shosts files
IgnoreRhosts yes
# For this to work you will also need host keys in /etc/ssh_known_hosts
RhostsRSAAuthentication no
# similar for protocol version 2
HostbasedAuthentication no
# Uncomment if you don't trust ~/.ssh/known_hosts for RhostsRSAAuthentication
#IgnoreUserKnownHosts yes

# To enable empty passwords, change to yes (NOT RECOMMENDED)
PermitEmptyPasswords no

# Change to yes to enable challenge-response passwords (beware issues with
# some PAM modules and threads)
ChallengeResponseAuthentication no

# Change to no to disable tunnelled clear text passwords
PasswordAuthentication no

# Kerberos options
#KerberosAuthentication no
#KerberosGetAFSToken no
#KerberosOrLocalPasswd yes
#KerberosTicketCleanup yes

# GSSAPI options
#GSSAPIAuthentication no
#GSSAPICleanupCredentials yes

X11Forwarding no
X11DisplayOffset 10
PrintMotd no
PrintLastLog yes
TCPKeepAlive yes
#UseLogin no

#MaxStartups 10:30:60
Banner /etc/issue.net

# Allow client to pass locale environment variables
AcceptEnv LANG LC_*

Subsystem sftp /usr/lib/openssh/sftp-server

# Set this to 'yes' to enable PAM authentication, account processing,
# and session processing. If this is enabled, PAM authentication will
# be allowed through the ChallengeResponseAuthentication and
# PasswordAuthentication.  Depending on your PAM configuration,
# PAM authentication via ChallengeResponseAuthentication may bypass
# the setting of "PermitRootLogin without-password".
# If you just want the PAM account and session checks to run without
# PAM authentication, then enable this but set PasswordAuthentication
# and ChallengeResponseAuthentication to 'no'.
UsePAM yes

这些是我在上面的文件中更改的行的初始状态:

LogLevel INFO
PermitRootLogin without-password
#PasswordAuthentication yes
X11Forwarding yes
#Banner /etc/issue.net

我保存了这些,然后sudo service ssh restart退出并尝试登录,username@localIP但出于某种原因,登录需要很长时间。一旦我登录,连接似乎非常快(因为它在我的 LAN 上,所以应该如此)。奇怪的是,即使我不通过我的域名登录也需要很长时间。

接下来,我将端口 22 转发到我的服务器静态 IP 地址并尝试这样做[email protected],但登录速度甚至更慢。连接到服务器大约需要 2 分钟。当我连接时,它会随机冻结并且永远不会工作,直到我通过关闭终端并重新连接手动终止会话。有时它根本无法连接,只是在我输入后挂起。ssh [email protected]

但如果我这样做,ping example.com所有数据包似乎都会很快返回,平均 ping 时间为 10ms。

我的配置设置是不是有点混乱了?我尝试将所有内容恢复到以前的状态并重新启动 ssh 服务器,但即使我这样做,登录仍然需要很长时间username@localIP@

到目前为止,服务器上只有一个用户(显然除了 root 之外),我手动将 rsa 密钥 id_rsa.pub 从客户端计算机复制到文件~/.ssh/authorized_keys。我必须手动创建文件夹.ssh和文件,但它似乎有效,并且不会提示我输入密码。

答案1

将以下内容添加到配置中:

UseDNS no

这将导致 SSH 服务器不是通过 DNS 解析登录尝试。

相关内容