WinRM(Windows Remote Management)是微软开发的基于WS-Management协议的标准,用于通过HTTP/HTTPS实现远程管理Windows系统。以下是关键信息与配置指南:
核心特性
协议基础
默认端口:HTTP 5985 / HTTPS 5986
依赖服务:WinRM(服务名:winrm)
认证方式:
基础认证(Basic)
Kerberos(域环境)
证书(Certificate)
NTLM(旧版)
主要用途
PowerShell远程执行命令(Enter-PSSession)
自动化运维工具(Ansible、Chef)连接Windows
远程收集系统信息/日志
启用WinRM的步骤(Windows Server 2008及以上)
方法一:通过命令快速启用(管理员权限)
powershell
复制
下载
winrm quickconfig -quiet # 自动启用服务、防火墙例外(HTTP 5985)
方法二:手动配置
启动WinRM服务
powershell
复制
下载
net start winrm
设置服务自启
powershell
复制
下载
sc config winrm start=auto
防火墙开放端口
cmd
复制
下载
netsh advfirewall firewall add rule name="WinRM-HTTP" dir=in action=allow protocol=TCP localport=5985
进阶配置
1. 启用HTTPS(推荐)
powershell
复制
下载
# 生成自签名证书(管理员权限)
$cert = New-SelfSignedCertificate -DnsName $env:COMPUTERNAME -CertStoreLocation Cert:\LocalMachine\My
# 绑定证书到WinRM监听器
winrm create winrm/config/Listener?Address=*+Transport=HTTPS @{Hostname="$($env:COMPUTERNAME)"; CertificateThumbprint="$($cert.Thumbprint)"}
# 开放防火墙端口5986
netsh advfirewall firewall add rule name="WinRM-HTTPS" dir=in action=allow protocol=TCP localport=5986
2. 允许远程连接
powershell
复制
下载
winrm set winrm/config/service @{AllowRemoteAccess="true"}
3. 信任所有客户端(测试用,生产环境慎用!)
powershell
复制
下载
winrm set winrm/config/client @{TrustedHosts="*"}
连接测试
从另一台Windows机器发起连接:
powershell
复制
下载
# HTTP连接
Enter-PSSession -ComputerName <目标IP> -Credential <用户名> -UseSSL:$false
# HTTPS连接
Enter-PSSession -ComputerName <目标IP> -Credential <用户名> -UseSSL
安全加固建议
强制使用HTTPS
powershell
复制
下载
winrm set winrm/config/service @{EnableCompatibilityHttpListener="false"} # 禁用HTTP
限制访问IP
powershell
复制
下载
netsh advfirewall firewall set rule name="WinRM-HTTPS" new remoteip=192.168.1.0/24
启用证书认证
powershell
复制
下载
winrm set winrm/config/service/auth @{Certificate="true"}
定期更新证书:避免使用永久有效的自签名证书。
排错命令
问题场景命令检查WinRM状态winrm enumerate winrm/config/listener测试连通性Test-WSMan <目标IP> -UseSSL查看认证错误日志eventvwr → Windows日志/应用程序和服务日志/Microsoft/Windows/WinRM
注意:
Windows Server 2008默认未启用WinRM,需手动开启。
域环境中建议使用Kerberos认证提升安全性。
公网暴露5985/5986端口需配合VPN或IP白名单,否则易遭暴力破解。