当前在研究goploy自动化开源部署工具,该工具部署在linux机器上,而要部署服务的目标服务器有一部分是windows server服务器,goploy自动化部署,使用rsync部署方式,底层依赖于ssh远程连接目标服务器,所以,要实现自动化部署,必须先实现ssh远程连接目标windows server服务器。下面将依次说明具体步骤。
在开始之前,计算机必须满足以下要求:
若要验证环境,请打开提升的 PowerShell 会话并执行以下操作:
键入 winver.exe ,然后按 Enter 查看 Windows 设备的版本详细信息。
运行 $PSVersionTable.PSVersion。 验证主要版本至少为 5,次要版本至少为 1。 详细了解如何在 Windows 上安装 PowerShell。
运行下面的命令(PowerShell)。 当你是内置管理员组的成员时,输出将显示 True。
(New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
可以使用 Windows Server 2019 和 Windows 10 设备上的 Windows 设置安装这两个 OpenSSH 组件。
若要安装 OpenSSH 组件:
找到“OpenSSH 客户端”,然后选择“安装”
找到“OpenSSH Server”,然后选择“安装”
备注:
安装 OpenSSH 服务器将创建并启用一个名为 OpenSSH-Server-In-TCP 的防火墙规则。 这允许端口 22 上的入站 SSH 流量。 如果未启用此规则且未打开此端口,那么连接将被拒绝或重置。
Shell或PowerShell中依照下面格式“ssh domain\username@servername”输入命令,样例如下:
ssh [email protected]
正常输入密码即可连接上,然后就可以在控制台输入命令运行了。
参考链接:
适用于 Windows 的 OpenSSH 入门
下面是摘抄的ssh连接的全部内容,如果只是关注linux使用ssh连接windows服务器,则只需要关注“部署公钥”相关的部分。
PowerShell脚本说明:
# By default the ssh-agent service is disabled. Configure it to start automatically.
# Make sure you're running as an Administrator.
Get-Service ssh-agent | Set-Service -StartupType Automatic
# Start the service
Start-Service ssh-agent
# This should return a status of Running
Get-Service ssh-agent
# Now load your key files into ssh-agent
ssh-add $env:USERPROFILE\.ssh\id_ed25519
将密钥添加到客户端上的 ssh-agent 后,ssh-agent 会自动检索本地私钥并将其传递给 SSH 客户端。
要使用上面创建的用户密钥,必须将公钥 (.ssh\id_ed25519.pub) 的内容作为文本文件放在服务器上。 文件的名称和位置取决于用户帐户是本地管理员组的成员还是标准用户帐户。 以下部分涵盖标准和管理用户。特别提醒,下面的PowerShell脚本,都是需要在其他windows机器的PowerShell终端上执行,实现远程配置功能。
公钥 (.ssh\id_ed25519.pub) 的内容需放置在服务器上的一个名为 authorized_keys 的文本文件中,该文件位于 C:\Users\username.ssh\。 可以使用 OpenSSH scp 安全文件传输实用工具或使用 PowerShell 将密钥写入文件来复制公钥。
以下示例将公钥复制到服务器(其中“username”替换为你的用户名)。 最初,你需要使用服务器的用户帐户的密码。
PowerShell脚本说明:
# Get the public key file generated previously on your client
$authorizedKey = Get-Content -Path $env:USERPROFILE\.ssh\id_ed25519.pub
# Generate the PowerShell to be run remote that will copy the public key file generated previously on your client to the authorized_keys file on your server
$remotePowershell = "powershell New-Item -Force -ItemType Directory -Path $env:USERPROFILE\.ssh; Add-Content -Force -Path $env:USERPROFILE\.ssh\authorized_keys -Value '$authorizedKey'"
# Connect to your server and run the PowerShell using the $remotePowerShell variable
ssh username@[email protected] $remotePowershell
公钥 (.ssh\id_ed25519.pub) 的内容需放置在服务器上的一个名为 administrators_authorized_keys 的文本文件中,该文件位于 C:\ProgramData\ssh\。 可以使用 OpenSSH scp 安全文件传输实用工具或使用 PowerShell 将密钥写入文件来复制公钥。 此文件上的 ACL 需要配置为仅允许访问管理员和系统。
以下示例将公钥复制到服务器并配置 ACL(其中“username”替换为你的用户名)。 最初,你需要使用服务器的用户帐户的密码。
备注:此示例演示了创建 administrators_authorized_keys 文件的步骤。 这仅适用于管理员帐户,并且在用户配置文件位置内的必须是用户而不是每用户文件。
PowerShell脚本说明:
# Get the public key file generated previously on your client
$authorizedKey = Get-Content -Path $env:USERPROFILE\.ssh\id_ed25519.pub
# Generate the PowerShell to be run remote that will copy the public key file generated previously on your client to the authorized_keys file on your server
$remotePowershell = "powershell Add-Content -Force -Path $env:ProgramData\ssh\administrators_authorized_keys -Value '$authorizedKey';icacls.exe ""$env:ProgramData\ssh\administrators_authorized_keys"" /inheritance:r /grant ""Administrators:F"" /grant ""SYSTEM:F"""
# Connect to your server and run the PowerShell using the $remotePowerShell variable
ssh username@[email protected] $remotePowershell
PowerShell实际样例如下:
# Get the public key file generated previously on your client
$authorizedKey = Get-Content -Path C:\Users\tom\.ssh\id_ed25519.pub
# Generate the PowerShell to be run remote that will copy the public key file generated previously on your client to the authorized_keys file on your server
$remotePowershell = "powershell Add-Content -Force -Path $env:ProgramData\ssh\administrators_authorized_keys -Value '$authorizedKey';icacls.exe ""$env:ProgramData\ssh\administrators_authorized_keys"" /inheritance:r /grant ""Administrators:F"" /grant ""SYSTEM:F"""
# Connect to your server and run the PowerShell using the $remotePowerShell variable
ssh [email protected] $remotePowershell
参考链接:
OpenSSH for Windows 中基于密钥的身份验证
上述步骤完成后,在配置好私钥的Wndwos或Linux机器上都可以使用下面的命令ssh远程连接Windows服务器,且不需要输入密码:
ssh [email protected]