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

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

我正在嘗試讓我的家庭伺服器能夠透過網路進行 ssh 存取。

最初(在接觸任何設定檔之前,apt-get install openssh-server我能夠非常快速地(基本上立即)連接到本地 LAN。我這樣做了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但由於某種原因,登入需要很長時間。一旦我登錄,連接似乎非常快(應該是這樣,因為它在我的區域網路上)。奇怪的是,即使我不通過我的域名,也需要永遠登入。

接下來,我將連接埠 22 轉送到我的伺服器靜態 IP 位址並嘗試這樣做[email protected],但登入速度甚至更慢。連接到伺服器大約需要2分鐘。當我連接時,它會隨機凍結並且永遠不會工作,直到我通過關閉終端並重新連接來手動終止會話。有時它根本不會連接,它只是在我進入後掛起。ssh [email protected]

但如果我這樣做,ping example.com所有資料包似乎都會很快返回,平均 ping 時間為 10 毫秒。

我的配置設定是否搞砸了?我嘗試將所有內容恢復到先前的狀態並重新啟動 ssh 伺服器,但即使我這樣做,仍然需要很長時間才能登入username@localIP@

到目前為止,我的伺服器上只有一個使用者(顯然除了 root 之外),我手動將 rsa 金鑰 id_rsa.pub 從我的客戶端電腦複製到 file ~/.ssh/authorized_keys。我必須手動建立資料夾.ssh和文件,但它似乎可以工作並且不會提示我輸入密碼。

答案1

將以下內容新增至配置:

UseDNS no

這將導致 SSH 伺服器不是透過 DNS 解析登入嘗試。

相關內容