【windows配置sshd服务实现远程ssh连接】

windows配置sshd服务实现远程ssh连接

官方文档说明:OpenSSH for Windows 概述

windows环境要求

适用于 Windows Server 2022、Windows Server 2019、Windows 10(内部版本 1809 及更高版本)

背景

同一局域网下,用于实现 Linux 服务器远程 ssh 连接 windows 服务器

操作步骤

1. 安装openssh-server

(1)以管理员方式运行powershell
在这里插入图片描述
(2)执行以下命令,安装 OpenSSH 的 Client 和 Server

Get-WindowsCapability -Online | Where-Object Name -like 'OpenSSH*'

# Install the OpenSSH Client by output name
Add-WindowsCapability -Online -Name OpenSSH.Client~~~~0.0.1.0

# Install the OpenSSH Server by output name
Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0

2. 启停sshd生成配置

(1)启动ssh服务

Start-Service sshd

(2)查看是否生成配置

notepad $env:ProgramData\ssh\sshd_config

(3)关闭ssh服务

Stop-Service sshd

3. 设置默认shell

(1)以下提升的 PowerShell 命令将默认 shell 设置为powershell.exe

New-ItemProperty -Path "HKLM:\SOFTWARE\OpenSSH" -Name DefaultShell -Value "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -PropertyType String -Force

4. 验证是否配置防火墙规则

(1)运行以下命令,验证 SSHD 安装过程是否自动配置了防火墙规则

if (!(Get-NetFirewallRule -Name "OpenSSH-Server-In-TCP" -ErrorAction SilentlyContinue | Select-Object Name, Enabled)) {
    Write-Output "Firewall Rule 'OpenSSH-Server-In-TCP' does not exist, creating it..."
    New-NetFirewallRule -Name 'OpenSSH-Server-In-TCP' -DisplayName 'OpenSSH Server (sshd)' -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 32200
} else {
    Write-Output "Firewall rule 'OpenSSH-Server-In-TCP' has been created and exists."
}

5. 查看当前操作用户是否在管理员列表

(1)输出显示True您何时是内置管理员组的成员

(New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)

6. 调整ssh配置

(1)可按实际需要功能调整,此处不做调整,ssh端口为默认的 22

7. Linux机器生成私钥(也可以参考官方文档使用密码认证方式)

注意: 此处在需要远程连接windows服务器的linux系统机器操作

# 创建测试目录
white@GIH-D-26397:~$ mkdir test

# 指定test目录下生成一对秘钥,按 Enter 即可
white@GIH-D-26397:~$ ssh-keygen -t rsa -b 2048 -f test/id_rsa

(1)上述命令会在 test 目录下生成两个文件

white@GIH-D-26397:~$ ls -l test/
total 4
-rw------- 1 white white 1823 Jan  2 16:02 id_rsa
-rw-r--r-- 1 white white  399 Jan  2 16:02 id_rsa.pub
  • id_rsa:私钥,用于稍后远程 ssh 连接 windows 使用
  • id_rsa.pub:公钥,用于添加到windows机器ssh目录下 administrators_authorized_keys 中,用于私钥连接进行认证

8. windows机器新增认证key文件

注意: windows 机器powershell中执行对应命令
(1)创建administrators_authorized_keys

New-Item $env:ProgramData\ssh\administrators_authorized_keys

(2)编辑文件,仅添加上述 id_rsa.pub 公钥文件内容,保存即可

notepad $env:ProgramData\ssh\administrators_authorized_keys

(3)设置权限

icacls.exe "C:\ProgramData\ssh\administrators_authorized_keys" /inheritance:r /grant "Administrators:F" /grant "SYSTEM:F"

9. 启动sshd服务并配置开机自启

(1)启动

Start-Service sshd

(2)检查ssh端口是否启动

Get-NetTCPConnection | Where-Object { $_.LocalPort -eq 22 }

(3)配置sshd开机自启

Set-Service -Name sshd -StartupType 'Automatic'

10.Linux机器尝试ssh连接

(1)检查linux机器到windows的端口连通性,通表示正常

white@GIH-D-26397:~$ telnet 10.226.50.145 22

(2)${ip}为windows机器ip如果成功输出windows机器主机名,表示配置成功

white@GIH-D-26397:~$ ssh -i test/id_rsa -p 22 administrator@${ip} "powershell.exe -Command 'hostname'"

附录

如果需要卸载,请参考官方文档 卸载 Windows 版 OpenSSH 执行对应powershell命令即可

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值