iT邦幫忙

1

在 Windows Server 2019 上的 OpenSSH 伺服器連線時,密碼認證可以使用,但公鑰認證失敗

  • 分享至 

  • xImage

我打算用 SSH 連到我的 Windows Server 2019,以便使用 GitLab CI/CD 部署我的網站,因此我安裝了 OpenSSH 伺服器,如果使用密碼登錄SSH,一切都正常使用。
但當我嘗試使用公鑰認證時,SSH無法正常連線並且會有錯誤訊息。
我該怎麼解決這個問題?

ssh: connect to host x.x.x.x port 22: Connection refused

如果我從 Powershell 中使用 Start-service sshd 或使用 services.msc 等 GUI 啟動服務,我無法連接,連接會關閉並顯示

debug3: recv - from CB ERROR:10054, io:0000018A0CABA2B0
debug3: send packet: type 1
debug3: send - WSASend() ERROR:10054, io:0000018A0CABA2B0

以下是在連接時使用 -vvv DEBUG輸出的關鍵LOG:

Authenticated to x.x.x.x ([x.x.x.x]:22).
debug1: channel 0: new [client-session]
debug3: ssh_session2_open: channel_new: 0
debug2: channel 0: send open
debug3: send packet: type 90
debug1: Requesting no-more-sessions@openssh.com
debug3: send packet: type 80
debug1: Entering interactive session.
debug1: pledge: network
debug1: ENABLE_VIRTUAL_TERMINAL_INPUT is supported. Reading the VTSequence from console
debug3: This windows OS supports conpty
debug1: ENABLE_VIRTUAL_TERMINAL_PROCESSING is supported. Console supports the ansi parsing
debug3: Successfully set console output code page from:65001 to 65001
debug3: Successfully set console input code page from:950 to 65001
debug3: recv - from CB ERROR:10054, io:000001AE1C4C53E0
debug3: send packet: type 1
debug3: send - WSASend() ERROR:10054, io:000001AE1C4C53E0
client_loop: send disconnect: Connection reset
debug3: Successfully set console output code page from 65001 to 65001
debug3: Successfully set console input code page from 65001 to 950

在伺服器端,事件檢視器也有一些錯誤LOG:

sshd: error: lookup_principal_name: User principal name lookup failed for user '_domain_\\_user_' (explicit: 5, implicit: 5)
sshd: error: get_user_token - unable to generate token on 2nd attempt for user _domain_\\_user_
sshd: error: unable to get security token for user _domain_\\_user_
sshd: fatal: fork of unprivileged child failed

我曾判斷這是由於 sshd 用戶的權限不足引起的,因此我嘗試將 sshd 加為管理員權限,但沒有幫助...
https://ithelp.ithome.com.tw/upload/images/20230111/20152569azti9dXgqW.png

透過

net localgroup administrators

也確認SSHD有加入管理員權限

Alias name     administrators
Comment        Administrators have complete and unrestricted access to the computer/domain

Members

-------------------------------------------------------------------------------
James
...(略)
sshd
The command completed successfully.

但如果我手動執行 C:\Windows\System32\OpenSSH\sshd.exe 啟動服務,一切都很正常...
大家有什麼idea嗎?


PS. 症狀和網路上有一篇2021年的問題一模一樣
https://learn.microsoft.com/en-us/answers/questions/644144/openssh-bug-can39t-ssh-when-running-34sshd34-as-a.html

但至今無人解答~~~

2023/1/13補充
https://github.com/PowerShell/Win32-OpenSSH/issues/1235
按照這篇說的

erinlah-ms commented on Jul 14, 2021
@NoMoreFood I'm hitting this as well on latest windows SSH. User account is Microsoft's Azure AD joined machine, but machine is workgroup (not domain joined / remote).

Is there any way to enable login of this sort of user, or do I need to create a local user to SSH in as instead?

於是我創了一個本地用戶來使用 SSH
可以連
但我有網域的帳號還是不行~~

看更多先前的討論...收起先前的討論...
阿摔 iT邦新手 4 級 ‧ 2023-01-11 14:26:47 檢舉
2019我記得預設的local policy有擋遠端指令執行 可以往這方向看看
JamesDoge iT邦高手 1 級 ‧ 2023-01-13 14:26:40 檢舉
我在2019 伺服器上使用「本機安全性原則」應用程式來實現。
我在「本機原則」->「組織原則」->「管理模板」->「系統」->「進階系統設定」裡,找不到關於「遠端指令執行」的項目~~感謝你的回答
re.Zero iT邦研究生 5 級 ‧ 2023-01-13 20:15:37 檢舉
好奇問下,你是參考 [這個](https://learn.microsoft.com/zh-tw/windows-server/administration/openssh/openssh_keymanagement) 建立所需環境的?(因為不知道你的環境狀態或建立流程,所以好奇問下~)
另,Markdown 語法有「引用文字」,不要每個都用「程式碼」啦~(我知道大家都懶得換用……)
JamesDoge iT邦高手 1 級 ‧ 2023-01-13 20:22:16 檢舉
HI~ 我是按照這篇建立的 https://ithelp.ithome.com.tw/articles/10310597
另外 改了「引用文字」
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

0
re.Zero
iT邦研究生 5 級 ‧ 2023-01-14 15:52:36
最佳解答

這篇不算回答,是因為「發問」的「討論」內不能用 Markdown 語法才用「回答」發文提問。

引用之前的討論(前言):

re.Zero iT邦新手 3 級 ‧ 2023-01-13 20:15:37編輯
好奇問下,你是參考 這個 建立所需環境的?(因為不知道你的環境狀態或建立流程,所以好奇問下~)
另,Markdown 語法有「引用文字」,不要每個都用「程式碼」啦~(我知道大家都懶得換用……)

JamesDoge iT邦新手 2 級 ‧ 2023-01-13 20:22:16
HI~ 我是按照這篇建立的 https://ithelp.ithome.com.tw/articles/10310597
另外 改了「引用文字」

Hi~ 我的主要疑問是在於你的 SSH-Key-based 環境。
你給的 在 Windows Server 2019 安裝 Open-SSH 無錯版筆記 沒有 Key-based 環境敘述;頂多只有你解決 username-password 環境的敘述。

另,我注意到在 在 Windows Server 2019 安裝 Open-SSH 無錯版筆記 有註解掉 AuthorizedKeysFile

#AuthorizedKeysFile PROGRAMDATA /ssh/administrators_authorized_keys

這有點詭異,你想要 Key-based 環境卻不設定搜索路徑……
(雖說我知道你是想要解決 username-password 異常,但我不知道微軟的 OpenSSH 在這設定下會怎樣運作,我只碰過 Linux 的 sshd ~)

我建議你看完 Windows 的 OpenSSH 與該頁面左側列表內與 OpenSSH 相關內容,
(如果你的英文 ok,建議看英文版的,內容有落差;英文版的更新時間較新。)
還有 sshd_config — OpenSSH daemon configuration file 了解詳細的組態資訊。
另,感覺你需要看下 AuthenticationMethods — sshd_configOpenSSH Server configuration for Windows Server and Windows 部分。
(畢竟微軟的 OpenSSH 實作與 openssh.com 的很可能不一樣~)

JamesDoge iT邦高手 1 級 ‧ 2023-01-25 11:06:27 檢舉

感謝回答~

我要發表回答

立即登入回答