![更改 sshd_config 后 SSH 登录速度非常慢,即使在 LAN 上也是如此](https://linux22.com/image/1452591/%E6%9B%B4%E6%94%B9%20sshd_config%20%E5%90%8E%20SSH%20%E7%99%BB%E5%BD%95%E9%80%9F%E5%BA%A6%E9%9D%9E%E5%B8%B8%E6%85%A2%EF%BC%8C%E5%8D%B3%E4%BD%BF%E5%9C%A8%20LAN%20%E4%B8%8A%E4%B9%9F%E6%98%AF%E5%A6%82%E6%AD%A4.png)
我正在尝试让我的家庭服务器能够通过 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
和文件,但它似乎有效,并且不会提示我输入密码。