windows开启sshd服务linux密钥免密码远程windows

windows开启sshd服务linux密钥免密码远程windows
内容目录

效果

root@ubuntuserver1:~# ssh Server@192.168.0.245 
Microsoft Windows [版本 10.0.17763.3650]       
(c) 2018 Microsoft Corporation。保留所有权利。 

server@WIN-KUSKOPL1PVN C:\Users\Server>  

安装openssh

官方教程
https://learn.microsoft.com/zh-cn/windows-server/administration/openssh/openssh_install_firstuse?tabs=powershell&pivots=windows-server-2019
有图形化教程也有powershell教程,我用的powershell

安装客户端服务端

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

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

启动和添加防火墙

# Start the sshd service
Start-Service sshd

# OPTIONAL but recommended:
Set-Service -Name sshd -StartupType 'Automatic'

# Confirm the Firewall rule is configured. It should be created automatically by setup. Run the following to verify
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 22
} else {
    Write-Output "Firewall rule 'OpenSSH-Server-In-TCP' has been created and exists."
}

开机启动

# Set the sshd service to be started automatically.
Get-Service -Name sshd | Set-Service -StartupType Automatic

添加linux主机公钥到windows

复制公钥

#没有的话ssh-keygen --t rsa一路回车生成
cat .ssh/id_rsa.pub

添加公钥

C:\ProgramData\ssh\下新建administrators_authorized_keys添加上一步复制的公钥

修改权限

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

修改sshd配置

C:\ProgramData\ssh\sshd_config
修改这两个配置允许密钥登录和使用 PROGRAMDATA/ssh/administrators_authorized_keys

PubkeyAuthentication yes
AuthorizedKeysFile  __PROGRAMDATA__/ssh/administrators_authorized_keys

重启sshd生效

powershell

Restart-Service sshd

Comments

No comments yet. Why don’t you start the discussion?

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注