前言

士别三日当刮目相待

没想到这么多年过去了,Windows 也不再是以前那个离开了图形界面啥也不是的系统

Windows 10/11 和 Server 2019+ 已内置 OpenSSH Server,可以作为跳板连接到其他 Linux 服务器。

这种场景一般是办公室有长期开机的 Windows 电脑,然后通过虚拟组网的方式来远程操作

操作步骤

先说明一点,网上找到的大部分资料都是使用图形界面操作,然后 Windows 那个设置真的很垃圾,本身很卡,点多几下就卡死或者闪退。

在设置的可选功能里面添加 OpenSSH Server 组件,总是跑到一半就卡死不动了

在啃了官方文档之后,我完全用命令行的方式搞定了。不得不说这个 powershell 确实有点东西的

安装 OpenSSH Server

Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0

这个命令比在设置界面添加快多了

启动服务并设置为开机自启

Start-Service sshd
Set-Service -Name sshd -StartupType 'Automatic'

开放防火墙端口(22)

为了安全起见,后续建议修改一下其他端口

New-NetFirewallRule -Name sshd -DisplayName 'OpenSSH Server' -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 22

连接

这时候就已经搞定了,直接用用本地账号或 Microsoft 账户登录就行了,建议使用本地账户

使用本地用户 + 密码登录

ssh your_user@10.x.x.x

使用密钥登录

在 Linux 上生成密钥对(或使用已有)

ssh-keygen -t rsa -b 4096

将公钥内容添加到 Windows 的用户 .ssh\authorized_keys 里

C:\Users\<your_user>\.ssh\authorized_keys

如果是 Linux 的话,这里就完事了,可以直接用密钥免密码登录,但 Windows 有点小坑,接下来介绍一下。

权限

SSH 对配置文件的权限要求比较严格,在 Linux 下很简单,用 chmod 和 chown 命令就行了

但在 Windows 上就蒙圈了,似乎可以在图形界面配置权限,但好像很复杂

不过我还是找到了用命令行配置权限的方法

# 设置目录权限:仅用户本身可读写
icacls .ssh /inheritance:r
icacls .ssh /grant:r 用户名:F
icacls .ssh /remove "Authenticated Users" "Users" "Administrators" "System"
icacls .ssh /setowner 用户名 # 设置 authorized_keys 权限
cd .ssh
icacls authorized_keys /inheritance:r
icacls authorized_keys /grant:r 用户名:F
icacls authorized_keys /remove "Authenticated Users" "Users" "Administrators" "System"
icacls authorized_keys /setowner 用户名

配置

Windows 上的 sshd_config 路径在 C:\ProgramData\ssh\sshd_config

刚才说 Windows 上的 SSH 密钥登录有点小坑,这里看下这个配置文件

# This is the sshd server system-wide configuration file.  See
# sshd_config(5) for more information. # The strategy used for options in the default sshd_config shipped with
# OpenSSH is to specify options with their default value where
# possible, but leave them commented. Uncommented options override the
# default value. #Port 22
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress :: #HostKey __PROGRAMDATA__/ssh/ssh_host_rsa_key
#HostKey __PROGRAMDATA__/ssh/ssh_host_dsa_key
#HostKey __PROGRAMDATA__/ssh/ssh_host_ecdsa_key
#HostKey __PROGRAMDATA__/ssh/ssh_host_ed25519_key # Ciphers and keying
#RekeyLimit default none # Logging
#SyslogFacility AUTH
#LogLevel INFO # Authentication: #LoginGraceTime 2m
#PermitRootLogin prohibit-password
#StrictModes yes
#MaxAuthTries 6
#MaxSessions 10 #PubkeyAuthentication yes # The default is to check both .ssh/authorized_keys and .ssh/authorized_keys2
# but this is overridden so installations will only check .ssh/authorized_keys
AuthorizedKeysFile .ssh/authorized_keys #AuthorizedPrincipalsFile none # For this to work you will also need host keys in %programData%/ssh/ssh_known_hosts
#HostbasedAuthentication no
# Change to yes if you don't trust ~/.ssh/known_hosts for
# HostbasedAuthentication
#IgnoreUserKnownHosts no
# Don't read the user's ~/.rhosts and ~/.shosts files
#IgnoreRhosts yes # To disable tunneled clear text passwords, change to no here!
#PasswordAuthentication yes
#PermitEmptyPasswords no # GSSAPI options
#GSSAPIAuthentication no #AllowAgentForwarding yes
#AllowTcpForwarding yes
#GatewayPorts no
#PermitTTY yes
#PrintMotd yes
#PrintLastLog yes
#TCPKeepAlive yes
#UseLogin no
#PermitUserEnvironment no
#ClientAliveInterval 0
#ClientAliveCountMax 3
#UseDNS no
#PidFile /var/run/sshd.pid
#MaxStartups 10:30:100
#PermitTunnel no
#ChrootDirectory none
#VersionAddendum none # no default banner path
#Banner none # override default of no subsystems
Subsystem sftp sftp-server.exe # Example of overriding settings on a per-user basis
#Match User anoncvs
# AllowTcpForwarding no
# PermitTTY no
# ForceCommand cvs server Match Group administrators
AuthorizedKeysFile __PROGRAMDATA__/ssh/administrators_authorized_keys

首先把 PubkeyAuthentication yes 的注释去掉,虽然默认是启用 PubkeyAuthentication,但为了明确起见,建议取消注释,以免系统某些默认值变动导致失效。

然后还有个大坑 Match Group administrators

如果登录用户属于 administrators 组,就不会使用 ~/.ssh/authorized_keys,而是会强制用 C:\ProgramData\ssh\administrators_authorized_keys

一般 Windows 用户都是管理员,所以写入到 C:\Users\用户名\.ssh\authorized_keys 的配置是无效的

正确的方式是把公钥写入到 C:\ProgramData\ssh\administrators_authorized_keys

最好在配置下权限

cd 'C:\ProgramData\ssh'
New-Item -ItemType File -Path .\administrators_authorized_keys -Force
icacls .\administrators_authorized_keys /inheritance:r
icacls .\administrators_authorized_keys /grant:r "Administrators:F"

修改默认端口

默认是 22 端口,不太安全,别人一扫就出来了

端口范围科普

  • 1025 ~ 49151 是注册端口段(很多服务注册使用)
  • 49152 ~ 65535 是动态/私有端口段(推荐选这里

查看端口占用

在私有端口段随便选一个就行

不过可以先查看占用,比如:

netstat -aon | findstr ":60222"

添加防火墙规则

修改完记得添加规则

New-NetFirewallRule -Name "OpenSSH Custom Port" `
-DisplayName "OpenSSH Custom Port (60222)" `
-Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 60222

重启 SSH 服务

Restart-Service sshd

使用体验

Windows 的命令行体验还是比较有限的

启用了 SSH Server 之后,连上去居然是 CMD

不过要改成 PowerShell 也不难

这里贴一下网上找到的方法(未测试)

方法一,修改注册表

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

重启服务 Restart-Service sshd

方法二,为单个用户设置登录 shell

如果不想影响系统中其他用户(比如安全隔离),可以编辑 sshd_config,增加如下内容:

Match User 用户名
ForceCommand powershell.exe

方法三,临时切换

连上去之后直接输入 powershellpwsh (如果有安装 PowerShell 7 的话)

小结

搞定了,虽然是连上了 SSH,不过 Windows 的价值也就在于当跳板连 Linux 了…

参考资料

告别图形界面:Windows系统OpenSSH服务部署的更多相关文章

  1. windows系统dokuwiki安装部署设置 xampp环境配置

    简单记录一次安装dokuwiki的过程 dokuwiki下载 dokuwiki下载地址 https://download.dokuwiki.org/ 下载前有一些可选项目,版本.语言.插件,可以按照需 ...

  2. windows 上 OpenSSH 服务 启用秘钥登录(微软真心逆天)

    windows 上 OpenSSH 服务 启用秘钥登录(微软真心逆天) windows 安装 OpenSSH 服务 最近需要在windows 服务器上部署自动发布程序,那么就需要用到 scp 和 ss ...

  3. windows上OpenSSH服务安装及启动

    一.windows安装OpenSSH 1,下载openSSH windows版 GitHub下载链接 我安装的是64位版本 OpenSSH-Win64.zip 2,解压到C:\Program File ...

  4. centos7关闭图形界面启动系统

    手动敲那么多不累么?仅2条命令(好) 1,命令模式systemctl set-default multi-user.target 2,图形模式systemctl set-default graphic ...

  5. 吻逗死(windows)系统下自动部署脚本(for java spring*)及linux命令行工具

    转载请注明出处:https://www.cnblogs.com/funnyzpc/p/10051647.html (^^)(^^)自動部署腳本原本在上個公司就在使用,由於近期同事需要手動部署一個Spr ...

  6. windows系统IIS服务安装

    打开控制面板,win8可以使用快捷键win键+X打开列表   打开程序和功能   打开左上角启用或关闭windows功能   打开internet信息服务下拉单   按照下列图中进行对应项勾选 第一个 ...

  7. Windows安装OpenSSH服务

    一.背景 在做国盛通项目的时候,有两套并行测试环境,因为基本架构采用的是供应商提供的程序,需要将两套banner图做同步,因为图片数量多,进GitLab版本控制进行分支策略管理,进而同步两套环境,意义 ...

  8. windows系统添加服务命令

    管理员身份进入cmd sc create TestSvr binPath= D:\Program Files\test.exe start= auto

  9. 基于OSGI.Net的图形界面系统

    在2013年的十月份有幸接触了osgi.net和iopenworks的创始人,了解和学习的插件式开发,开始了后台数据的处理生涯. 第一个有图形界面的系统——智能农业的环境监测系统,其实在这个系统中所有 ...

  10. Windows系统下部署安装一个/多个Tomcat8

    首先从http://tomcat.apache.org/上下载Tomcat8.0压缩版的,解压到指定路径后即可.  第一:在Windows系统中安装部署单个Tomcat         对于这种情况, ...

随机推荐

  1. gorm中使用乐观锁

    乐观锁简介 乐观锁(又称乐观并发控制)是一种常见的数据库并发控制策略. 乐观并发控制多数用于数据竞争(data race)不大.冲突较少的环境中,这种环境中,偶尔回滚事务的成本会低于读取数据时锁定数据 ...

  2. 【抓包】Fidder Script自动修改包

    Fiddler Script的本质是用JScript.NET编写的一个脚本文件CustomRules.js 但是它的语法很像C#但又有些不一样,比如不能使用@符号 通过修改CustomRules.js ...

  3. 对象命名为何需要避免'-er'和'-or'后缀

    之前写过两篇关于软件工程中对象命名的文章:开发中对象命名的一点思考与对象命名怎么上手?从现实世界,但感觉还是没有说透, 在软件工程中,如果问我什么最重要,我的答案是对象命名.良好的命名能够反映系统的本 ...

  4. SpringBoot+微信支付-JSAPI{微信支付回调}

    引入微信支付SDK Maven: com.github.wechatpay-apiv3:wechatpay-java-core:0.2.12 Maven: com.github.wechatpay-a ...

  5. jupyterhub nginx proxy pass----ipv6转ipv4实现内网穿透

    jupyterhub 很多人应该已经对jupyter和notebook已经有所了解了.如果是多人共享服务器的话,就需要用到jupyter的多用户版本jupyterhub.jupyterhub架构如图所 ...

  6. 大模型提示词(Prompt)模板推荐

    只有提示词写得好,与大模型的互动才能更高效.提示词不仅仅是与AI对话的起点,更是驱动模型产生高质量输出的关键因素.本文将介绍大模型提示词的概念.意义,并分享一些实用的提示词模板,帮助AI玩家更好地利用 ...

  7. UML用例图-UML Use Case Diagram

    .wj_nav { display: inline-block; width: 100%; margin-top: 0; margin-bottom: 0.375rem } .wj_nav_1 { p ...

  8. 【Java】NIO

    1. Java NIO 简介 Java NIO(New IO)是从Java 1.4版本开始引入的一个新的IO API,可以替代标准的Java IO API. NIO与原来的IO有同样的作用和目的,但是 ...

  9. 创建的容器都是没有指定 volume的,为什么docker volume ls命令会看到很多volume列表?

    有那么一天,停止了世界所有的容器(在你电脑上),执行以下命令 docker volume ls docker volume ls | wc -l 哎呦,我去,怎么这么多啥玩意,再执行以下命令看占了我多 ...

  10. Debian 9 更换源

    Debian 全球镜像站 # 先备份源列表文件 mv /etc/apt/sources.list /etc/apt/sources.list.bak # 生成新的源列表文件(用的国内源镜像) echo ...