0%

windows系统ssh连接服务器(免密+私钥)

准备条件:

  • 私钥和公钥
  • openSSH 服务端和客户端安装

安装好openSSH后,在电脑开始搜索框输入关键词PowerShell,找到Windows PowerShell右键以管理身份运行

启用服务

1
net start sshd

添加防火墙开放22端口规则

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

查看ssdh现状

1
sshd -d

假如需要重启服务

1
2
Stop-Service sshd
Start-Service sshd

将准备好的公钥上传到服务器:

方式一:

1
ssh-copy-id -i ~/.ssh/id_rsa.pub shirley@192.168.235.22

方式二:将客户端id_rsa.pub中的内容贴到~/.ssh/authorized_keys 文件末尾

客户端请求链接:
(假如我的服务器:192.168.0.0,名字为name)

1
ssh name@192.168.0.0

或者

1
ssh -vvv name@192.168.0.0

指定私钥名字

1
ssh -vvv name@192.168.0.0 -i 路径\私钥名字(例如:E:\Users\admin\Desktop\crfbp_id_rsa)

直接yes走到通

附:错误记录,链接成功但请求被回拒

链接成功

1
2
3
debug3: ssh_connect_direct: entering
debug1: Connecting to 161.117.11.240 [161.117.11.240] port 22.
debug1: Connection established.
1
2
3
PS D:\Bepic_H5\h5-template> ssh -i ~/.ssh/id_rsa qianduan@161.117.11.240
kex_exchange_identification: Connection closed by remote host
Connection closed by 161.117.11.240 port 2

请求回拒链接失败:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
PS D:\Bepic_H5\h5-template> ssh -vvv qianduan@161.117.11.240
OpenSSH_for_Windows_8.6p1, LibreSSL 3.4.3
debug3: Failed to open file:C:/Users/admin/.ssh/config error:2
debug3: Failed to open file:C:/ProgramData/ssh/ssh_config error:2
debug2: resolve_canonicalize: hostname 161.117.11.240 is address
debug3: expanded UserKnownHostsFile '~/.ssh/known_hosts' -> 'C:\\Users\\admin/.ssh/known_hosts'
debug3: expanded UserKnownHostsFile '~/.ssh/known_hosts2' -> 'C:\\Users\\admin/.ssh/known_hosts2'
debug1: Authenticator provider $SSH_SK_PROVIDER did not resolve; disabling
debug3: ssh_connect_direct: entering
debug1: Connecting to 161.117.11.240 [161.117.11.240] port 22.
debug1: Connection established.
debug1: identity file C:\\Users\\admin/.ssh/id_rsa type 0
debug3: Failed to open file:C:/Users/admin/.ssh/id_rsa-cert error:2
debug3: Failed to open file:C:/Users/admin/.ssh/id_rsa-cert.pub error:2
debug3: failed to open file:C:/Users/admin/.ssh/id_rsa-cert error:2
debug1: identity file C:\\Users\\admin/.ssh/id_rsa-cert type -1
debug3: Failed to open file:C:/Users/admin/.ssh/id_dsa error:2
debug3: Failed to open file:C:/Users/admin/.ssh/id_dsa.pub error:2
debug3: failed to open file:C:/Users/admin/.ssh/id_dsa error:2
debug1: identity file C:\\Users\\admin/.ssh/id_dsa type -1
debug3: Failed to open file:C:/Users/admin/.ssh/id_dsa-cert error:2
debug3: Failed to open file:C:/Users/admin/.ssh/id_dsa-cert.pub error:2
debug3: failed to open file:C:/Users/admin/.ssh/id_dsa-cert error:2
debug1: identity file C:\\Users\\admin/.ssh/id_dsa-cert type -1
debug3: Failed to open file:C:/Users/admin/.ssh/id_ecdsa error:2
debug3: Failed to open file:C:/Users/admin/.ssh/id_ecdsa.pub error:2
debug3: failed to open file:C:/Users/admin/.ssh/id_ecdsa error:2
debug1: identity file C:\\Users\\admin/.ssh/id_ecdsa type -1
debug3: Failed to open file:C:/Users/admin/.ssh/id_ecdsa-cert error:2
debug3: Failed to open file:C:/Users/admin/.ssh/id_ecdsa-cert.pub error:2
debug3: failed to open file:C:/Users/admin/.ssh/id_ecdsa-cert error:2
debug1: identity file C:\\Users\\admin/.ssh/id_ecdsa-cert type -1
debug3: Failed to open file:C:/Users/admin/.ssh/id_ecdsa_sk error:2
debug3: Failed to open file:C:/Users/admin/.ssh/id_ecdsa_sk.pub error:2
debug3: failed to open file:C:/Users/admin/.ssh/id_ecdsa_sk error:2
debug1: identity file C:\\Users\\admin/.ssh/id_ecdsa_sk type -1
debug3: Failed to open file:C:/Users/admin/.ssh/id_ecdsa_sk-cert error:2
debug3: Failed to open file:C:/Users/admin/.ssh/id_ecdsa_sk-cert.pub error:2
debug3: failed to open file:C:/Users/admin/.ssh/id_ecdsa_sk-cert error:2
debug1: identity file C:\\Users\\admin/.ssh/id_ecdsa_sk-cert type -1
debug3: Failed to open file:C:/Users/admin/.ssh/id_ed25519 error:2
debug3: Failed to open file:C:/Users/admin/.ssh/id_ed25519.pub error:2
debug3: failed to open file:C:/Users/admin/.ssh/id_ed25519 error:2
debug1: identity file C:\\Users\\admin/.ssh/id_ed25519 type -1
debug3: Failed to open file:C:/Users/admin/.ssh/id_ed25519-cert error:2
debug3: Failed to open file:C:/Users/admin/.ssh/id_ed25519-cert.pub error:2
debug3: failed to open file:C:/Users/admin/.ssh/id_ed25519-cert error:2
debug1: identity file C:\\Users\\admin/.ssh/id_ed25519-cert type -1
debug3: Failed to open file:C:/Users/admin/.ssh/id_ed25519_sk error:2
debug3: Failed to open file:C:/Users/admin/.ssh/id_ed25519_sk.pub error:2
debug3: failed to open file:C:/Users/admin/.ssh/id_ed25519_sk error:2
debug1: identity file C:\\Users\\admin/.ssh/id_ed25519_sk type -1
debug3: Failed to open file:C:/Users/admin/.ssh/id_ed25519_sk-cert error:2
debug3: Failed to open file:C:/Users/admin/.ssh/id_ed25519_sk-cert.pub error:2
debug3: failed to open file:C:/Users/admin/.ssh/id_ed25519_sk-cert error:2
debug1: identity file C:\\Users\\admin/.ssh/id_ed25519_sk-cert type -1
debug3: Failed to open file:C:/Users/admin/.ssh/id_xmss error:2
debug3: Failed to open file:C:/Users/admin/.ssh/id_xmss.pub error:2
debug3: failed to open file:C:/Users/admin/.ssh/id_xmss error:2
debug1: identity file C:\\Users\\admin/.ssh/id_xmss type -1
debug3: Failed to open file:C:/Users/admin/.ssh/id_xmss-cert error:2
debug3: Failed to open file:C:/Users/admin/.ssh/id_xmss-cert.pub error:2
debug3: failed to open file:C:/Users/admin/.ssh/id_xmss-cert error:2
debug1: identity file C:\\Users\\admin/.ssh/id_xmss-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_for_Windows_8.6
kex_exchange_identification: Connection closed by remote host
Connection closed by 161.117.11.240 port 22

几乎找遍了所有问题的可能一直没解决,最后关掉vpn,关掉公司wifi,开启手机热点重启服务,发现公司网络问题。

功夫不负有心人:

1
2
3
Last login: Thu Mar  7 17:24:13 2024 from 223.160.228.62

Welcome to Alibaba Cloud Elastic Compute Service !

2024:04.09 问题记录: 回家用了家里的网络,发现使用了opensshTortoise客户端推送git项目时又失败了,然后我切换和手机热点并关闭已链接的wifi。对比了下网络属性,发现家里的网络和公司的网络没有ipv6,目前看来是这个问题导致的,不过我用指令指定ipv4推送也没有成功,也没有其他更好的解决办法。

-------------本文结束感谢您的阅读-------------