winrm登錄失敗,指定的憑據被服務器拒絕。
異常提示:the specified credentials were rejected by the server
在windows power shell執行
set-executionpolicy remotesigned
winrm quickconfig
winrm set winrm/config/service/auth '@{Basic="true"}'
winrm set winrm/config/service '@{AllowUnencrypted="true"}'
其他信息參考
查看是否啟動
winrm e winrm/config/listener快速配置:
winrm quickconfig默認是 Kerberos 認證方式,如果設備添加了域,則本地用戶無法訪問。
所以需要做以下配置:允許未加密密碼:
winrm s winrm/config/Client '@{AllowUnencrypted="true"}'開啟Basic認證
winrm set winrm/config/service/auth ’@{Basic="true"}‘
winrm set winrm/config/service ‘@{AllowUnencrypted="true"}’查看配置
winrm get winrm/configgci wsman::localhost\client\trustedhosts在客戶端添加信任
Set-Item WSMan:localhost\client\trustedhosts -value 192.168.0.49 -Force
遠程連接
Enter-PSSession 192.168.0.49 -Credential: win-uvp18pocoal\administrator -Authentication: Basic
Windows遠程管理憑證被拒絕的深度解決方案
當出現"the specified credentials were rejected by the server"錯誤時,表明目標服務器拒絕了您提供的憑據。這是WS-Management(WinRM)服務的常見問題,以下是系統化的解決方案:
🧩 根本原因診斷矩陣
原因類別 | 具體問題 | 驗證方法 |
---|---|---|
賬戶問題 | 1. 用戶名/密碼錯誤 2. 賬戶被鎖定 3. 權限不足 | net user [username] /domain Get-ADUser -Identity [username] |
認證策略 | 1. Basic認證禁用 2. CredSSP未啟用 3. Kerberos策略問題 | winrm get winrm/config Get-Item WSMan:\localhost\Client\Auth\* |
網絡配置 | 1. 目標主機解析錯誤 2. 雙跳問題 3. 防火墻阻止 | nslookup [target] Test-NetConnection [target] -Port 5985 |
系統策略 | 1. UAC限制 2. LSA保護 3. 用戶權限分配 | Get-ItemProperty HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System |
🔧 12步系統化解決方案
1. 基礎憑證驗證
# 在本地驗證憑據有效性
$cred = Get-Credential
Invoke-Command -ComputerName localhost -ScriptBlock { whoami } -Credential $cred
2. 賬戶權限升級
# 嘗試使用管理員賬戶連接(需在目標主機操作)
net localgroup administrators [username] /add
3. WinRM認證協議配置
# 啟用所有認證協議(臨時測試)
Set-Item WSMan:\localhost\Service\Auth\Basic -Value $true
Set-Item WSMan:\localhost\Service\Auth\Kerberos -Value $true
Set-Item WSMan:\localhost\Service\Auth\CredSSP -Value $true# 永久配置(需管理員權限)
winrm set winrm/config/service/auth '@{Basic="true"}'
4. CredSSP配置(解決雙跳問題)
# 在客戶端啟用CredSSP
Enable-WSManCredSSP -Role Client -DelegateComputer * -Force# 在服務器端啟用CredSSP
Invoke-Command -ComputerName $target -ScriptBlock {Enable-WSManCredSSP -Role Server -Force
}
5. Kerberos SPN驗證
# 檢查SPN注冊(需在域控制器執行)
setspn -L [計算機名]# 注冊SPN(若缺失)
setspn -A HOST/[完整計算機名] [計算機名]
setspn -A HOST/[NETBIOS名] [計算機名]
6. 用戶賬戶控制(UAC)調整
# 修改遠程UAC限制
New-ItemProperty -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System `-Name LocalAccountTokenFilterPolicy -Value 1 -PropertyType DWORD -Force
7. PowerShell執行策略調整
# 設置執行策略為RemoteSigned
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser -Force
8. HTTPS證書配置(5986端口)
# 創建自簽名證書(需要管理員權限)
$cert = New-SelfSignedCertificate -DnsName $env:COMPUTERNAME -CertStoreLocation Cert:\LocalMachine\My
winrm create winrm/config/Listener?Address=*+Transport=HTTPS @{Hostname=$env:COMPUTERNAME;CertificateThumbprint=$cert.Thumbprint}
9. 防火墻規則驗證
# 啟用5985端口防火墻規則
New-NetFirewallRule -Name "WinRM-HTTP" -DisplayName "WinRM (HTTP-In)" `-Protocol TCP -LocalPort 5985 -Action Allow -Enabled True
10. 組策略覆蓋檢查
# 強制刷新組策略
gpupdate /force
11. 目標主機委托配置
# 配置計算機賬戶委托(需域管理員)
Set-ADComputer -Identity $targetServer `-TrustedForDelegation $true `-Add @{"msDS-AllowedToDelegateTo"=@("WSMAN/$targetServer","WSMAN/$targetServer.$domain")}
12. 替代連接方法測試
# 使用SSH替代(Windows 10/Server 2019+)
Connect-PSSession -HostName $targetServer -SSHTransport -Credential $cred
📊 排錯工作流
graph TDA[遇到憑證錯誤] --> B{目標系統可達?}B -->|No| C[檢查網絡/DNS]B -->|Yes| D{基礎認證通過?}D -->|No| E[重置本地憑證緩存]D -->|Yes| F{遠程連接測試}F -->|失敗| G[檢查WinRM服務狀態]F -->|成功| H[檢查應用層配置]G --> I{winrm服務正常?}I -->|No| J[重建WinRM監聽器]I -->|Yes| K[驗證認證協議]
🧪 高級診斷命令集
# 完整WinRM配置導出
winrm get winrm/config -format:pretty > winrm_config.xml# Kerberos票據檢查
klist purge # 清除舊票據
kinit [username] # 獲取新票據
klist # 驗證票據# 網絡層追蹤
Test-WSMan -ComputerName $targetServer -UseSSL -SessionOption (New-PSSessionOption -IncludePortInSPN)# 詳細事件日志檢查
Get-WinEvent -LogName 'Microsoft-Windows-WinRM/Operational' -MaxEvents 50 | Where-Object { $_.Id -eq 35 } # 認證失敗事件
💼 企業級最佳實踐
安全組策略配置
# 創建專用管理組
New-ADGroup -Name "WinRM_Admins" -GroupScope Global# 配置受限管理權限
Set-PSSessionConfiguration -Name Microsoft.PowerShell -ShowSecurityDescriptorUI
- 添加
WinRM_Admins
組 - 授予
讀取
和執行
權限 - 拒絕
完全控制
權限
JEA(Just Enough Administration)配置
# 創建JEA端點
New-PSSessionConfigurationFile -Path .\RestrictedEndpoint.pssc `-SessionType RestrictedRemoteServer `-RunAsVirtualAccount `-RoleDefinitions @{ 'CONTOSO\DatabaseAdmins' = @{ RoleCapabilities = 'DatabaseAdministration' } }# 注冊端點
Register-PSSessionConfiguration -Name 'RestrictedDBAdmin' `-Path .\RestrictedEndpoint.pssc -Force
?? 關鍵注意事項
-
生產環境安全警告:
- 避免長期開啟Basic認證
- 強制使用5986(HTTPS)端口
- 實施IP白名單限制
-
域環境特殊要求:
# 配置Kerberos委派 Set-Item WSMan:\localhost\Client\TrustedHosts -Value "*.contoso.com" -Force
-
跨域認證方案:
# 建立域間信任 New-PSSessionOption -Authentication Negotiate -EnableNetworkAccess
-
憑證傳遞替代方案:
# 使用托管服務賬戶 Install-ADServiceAccount -Identity gMSA_Svc
通過系統化應用這些解決方案,絕大多數"The specified credentials were rejected by the server"錯誤可以得到解決。對于持續性問題,建議按網絡層→認證層→應用層的順序進行分層排查。