宜昌当阳网站开发,农业开发公司企业网站建设,百度指数的作用,网站网络如何在Linux机器上使用ssh远程连接Windows Server服务器 一、源起二、使用ssh远程连接Windows1.先决条件#xff08;1#xff09;至少运行 Windows Server 2019 或 Windows 10#xff08;内部版本 1809#xff09;的设备。#xff08;2#xff09;PowerShell 5.1 或更高版… 如何在Linux机器上使用ssh远程连接Windows Server服务器 一、源起二、使用ssh远程连接Windows1.先决条件1至少运行 Windows Server 2019 或 Windows 10内部版本 1809的设备。2PowerShell 5.1 或更高版本。3作为内置管理员组成员的帐户。 2.先决条件检查3.安装适用于 Windows 的 OpenSSH1打开“设置”选择“应用”然后选择“可选功能”。2扫描列表查看是否已安装 OpenSSH。 如果未安装请在页面顶部选择“添加功能”然后3设置完成后回到“应用”和“可选功能”并确认 OpenSSH 已列出。4打开“服务”桌面应用。 选择“开始”在搜索框中键入 services.msc 然后选择“服务”应用或按 ENTER。5在详细信息窗格中双击“OpenSSH SSH 服务器”。6在“常规”选项卡上的“启动类型”下拉菜单中选择“自动”。7若要启动服务请选择“启动”。 4.连接到 OpenSSH 服务器 三、OpenSSH for Windows 中基于密钥的身份验证1.部署私钥2.部署公钥1标准用户2管理用户 四、实际验证 一、源起 当前在研究goploy自动化开源部署工具该工具部署在linux机器上而要部署服务的目标服务器有一部分是windows server服务器goploy自动化部署使用rsync部署方式底层依赖于ssh远程连接目标服务器所以要实现自动化部署必须先实现ssh远程连接目标windows server服务器。下面将依次说明具体步骤。
二、使用ssh远程连接Windows
1.先决条件
在开始之前计算机必须满足以下要求
1至少运行 Windows Server 2019 或 Windows 10内部版本 1809的设备。
2PowerShell 5.1 或更高版本。
3作为内置管理员组成员的帐户。
2.先决条件检查
若要验证环境请打开提升的 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)
3.安装适用于 Windows 的 OpenSSH
可以使用 Windows Server 2019 和 Windows 10 设备上的 Windows 设置安装这两个 OpenSSH 组件。 若要安装 OpenSSH 组件
1打开“设置”选择“应用”然后选择“可选功能”。
2扫描列表查看是否已安装 OpenSSH。 如果未安装请在页面顶部选择“添加功能”然后
找到“OpenSSH 客户端”然后选择“安装”
找到“OpenSSH Server”然后选择“安装”3设置完成后回到“应用”和“可选功能”并确认 OpenSSH 已列出。
4打开“服务”桌面应用。 选择“开始”在搜索框中键入 services.msc 然后选择“服务”应用或按 ENTER。
5在详细信息窗格中双击“OpenSSH SSH 服务器”。
6在“常规”选项卡上的“启动类型”下拉菜单中选择“自动”。
7若要启动服务请选择“启动”。
备注 安装 OpenSSH 服务器将创建并启用一个名为 OpenSSH-Server-In-TCP 的防火墙规则。 这允许端口 22 上的入站 SSH 流量。 如果未启用此规则且未打开此端口那么连接将被拒绝或重置。
4.连接到 OpenSSH 服务器
Shell或PowerShell中依照下面格式“ssh domain\usernameservername”输入命令样例如下
ssh administrator192.168.13.142正常输入密码即可连接上然后就可以在控制台输入命令运行了。
参考链接 适用于 Windows 的 OpenSSH 入门
三、OpenSSH for Windows 中基于密钥的身份验证 下面是摘抄的ssh连接的全部内容如果只是关注linux使用ssh连接windows服务器则只需要关注“部署公钥”相关的部分。
1.部署私钥
PowerShell脚本说明
# By default the ssh-agent service is disabled. Configure it to start automatically.
# Make sure youre 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 客户端。
2.部署公钥 要使用上面创建的用户密钥必须将公钥 (.ssh\id_ed25519.pub) 的内容作为文本文件放在服务器上。 文件的名称和位置取决于用户帐户是本地管理员组的成员还是标准用户帐户。 以下部分涵盖标准和管理用户。特别提醒下面的PowerShell脚本都是需要在其他windows机器的PowerShell终端上执行实现远程配置功能。
1标准用户 公钥 (.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 usernamedomain1contoso.com $remotePowershell2管理用户
公钥 (.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 usernamedomain1contoso.com $remotePowershellPowerShell实际样例如下
# 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 administrator192.168.13.142 $remotePowershell参考链接 OpenSSH for Windows 中基于密钥的身份验证
四、实际验证
上述步骤完成后在配置好私钥的Wndwos或Linux机器上都可以使用下面的命令ssh远程连接Windows服务器且不需要输入密码
ssh administrator192.168.13.142