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/config
gci 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 TD
A[遇到凭证错误] --> 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"错误可以得到解决。对于持续性问题,建议按网络层→认证层→应用层的顺序进行分层排查。