felix-cao / Blog

A little progress a day makes you a big success!
31 stars 4 forks source link

Linux 客户端 SSH 登录 Windows10 远程主机 #206

Open felix-cao opened 2 years ago

felix-cao commented 2 years ago

我们可以通过 Linux 客户端登录 Linux 远程主机, 这是很常见的,那是因为,Linux 默认安装了 OpenSSH 但是

不同于 LinuxMacOS, Windows 系统没有自带的 SSH 客户端, SSH 常常用于Linux用户的远程登录, 在 Windows 10 系统中已经自带了OpenSSH, 刚好公司的电脑就是 Windows 10 系统

ps: 曾尝试使用FreeSSHD.exe 来搭建 SSH 服务器, 但其私钥公钥需要借助 putty 中的 puttygen 去创建, 创建只能使用 putty 命令行工具去连接,走到最后一步的时候,显然这不是我想要的

关于 openSSH

一、Windows10 安装 openssh

Windows 10 系统中已经自带了OpenSSH,只不过没安装启用。

点击左下角 WIN 图标, 设置(Settings) -> 应用(Apps) -> 应用和功能(Apps & features) -> 可选功能(Optional features) -> 添加功能(Add a feature)

安装后,在 Type here to search 中输入 service 找到 OpenSSH SSH Server 右键点 start

在本机尝试一把 ssh zfcao@127.0.0.1, 是成功的。

参考 Install OpenSSH

二、修改shell

默认情况下的 shellWindows Command shell (cmd.exe) 程序, 下面设置为 Powershell

以管理员的的身份启动 Powershell, 输入如下命令:

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

参考 OpenSSH Server Configuration for Windows 10 1809 and Windows Server 2019

三、配置文件目录

服务器端的配置文件在 C:\ProgramData\ssh 目录中:

image

四、将公钥私钥对存储到家目录.ssh下

ssh-keygen 命令用来生成公钥认证使用的秘钥对,创建的秘钥一般都和 ssh 客户端的配置一起保存在用户家目录下的 .ssh 目录中(与 Linux 系统中类似),

下面是将其设置为 .ssh/authorized_keys:

手动的将 Linux 客户端的用户公钥复制到 Windows10 远程主机家目录下的 ~/.ssh/authorized_keys

image

至此,Linux 客户端可以完美通过公钥认证的方式登录到 Windows10 远程主机了。接下来就可以做 基于 Jenkins 的 pipeline 实现远程主机通信及发送邮件

Reference

felix-cao commented 2 years ago

Cannot access network drive from ssh session

https://github.com/PowerShell/Win32-OpenSSH/issues/139

felix-cao commented 2 years ago

Linux SSH 连接 Windows Server 2019

https://docs.microsoft.com/en-us/windows-server/administration/openssh/openssh_install_firstuse

felix-cao commented 2 years ago

SVN 命令行连接错误

一般首次使用 svn 时会出现这个问题 image

解决方案:使用图形界面工具 TortoiseSVN 登录拉去后,再用命令行工具

felix-cao commented 2 years ago

Run powershell from git bash

https://github.com/PowerShell/PSReadLine/issues/483#issuecomment-302081936

powershell.exe

image

Run git bash from powershell

& 'C:\Program Files\Git\bin\sh.exe' --login

image

felix-cao commented 2 years ago

如何在 gitbash 中运行 powershell 命令

powershell.exe -Command

powershell.exe -Command c:/THC/irrsvc/kill.bat
felix-cao commented 2 years ago

配置成功,但免密登录无效

1、查看详细日志

ssh username@host -v

2、修改配置文件 StrictModes 和 PubkeyAuthentication

window下配置文件位置C:\ProgramData\ssh\sshd_config ; linux 下是 /etc/ssh/sshd_config

StrictModes no
PubkeyAuthentication yes

参考:https://segmentfault.com/a/1190000023054090