前言
Windows 10/11 自带了 OpenSSH Server,可以让你像在Linux系统上一样进行远程使用shell。本文将介绍如何配置 OpenSSH Server 以及如何使用密钥登陆并配置默认终端从cmd改为PowerShell。
安装 OpenSSH Server
打开
PowerShell以管理员身份运行。运行以下命令安装
OpenSSH Server:1Add-WindowsFeature -Name OpenSSH-Server当然,你可以通过
Windows 功能界面来安装OpenSSH Server,只需要勾选OpenSSH Server即可。运行以下命令启动 OpenSSH Server:
1Start-Service sshd运行以下命令设置 OpenSSH Server 开机自启:
1Set-Service -Name sshd -StartupType 'Automatic'运行以下命令检查 OpenSSH Server 是否已经启动:
1Get-Service sshd
配置 OpenSSH Server
打开 PowerShell 以管理员身份运行。
运行以下命令生成密钥对,如果已经有密钥对可以跳过这一步,密钥有这几种类型:
rsa,dsa,ecdsa,ed25519,这里使用ed25519:1ssh-keygen -t ed25519将你的将公钥添加到
~/.ssh/authorized_keys文件中。编辑
sshd_config文件1 2PasswordAuthentication no # 这一步是为了禁止密码登陆。确保安 全性。 PubkeyAuthentication yes # 启用密钥登陆特别注意!!与Linux下的OpenSSH Server不同,在Windows OpenSSH中,默认的授权密钥存放位置为 ProgramData\ssh\administrators_authorized_keys,推荐vscode 打开ProgramData\ssh\sshd_config,修改以下条目.注释掉默认授权 文件位置,确保以下条目被注释掉
1 2 3# Match Group administrators # AuthorizedKeysFile __PROGRAMDATA__/ssh/ administrators_authorized_keys 修改完成后保存并退出(注意修改sshd_config需要管理员权限)。在PowerShell(管理员)中重启sshd服务
1Restart-Service sshd设置默认shell为powershell
在PowerShell(管理员)中运行以下命令
1New-ItemProperty -Path "HKLM:\SOFTWARE\OpenSSH" -Name DefaultShell -Value "C:\Windows\System32\WindowsPowerShell\v1.0\powershell. exe" -PropertyType String -Force
连接
没啥说的了吧。你肯定会了