SSH密钥登录EXSI

一. 前言

小型工控机-软路由配置拾遗 | Lian (kyouichirou.github.io)

在上篇文章中, 介绍了各种远程连接的方式, 在这里还有一个坑要填, 那就是在iPhone上无法ssh连接exsi.

二. 解决

os: ios 14
terminal: PortX (尝试了多个不同的终端还是无法明文密码登录)

SSH access may fail post upgrade to ESXi 7.0 Update 3d (88055) (vmware.com)

不知道是哪边的锅, 看了下vm的文档, 可能是不同版本的ssh使用加密方式上的差异导致.

按照上文的密钥配置登录发现根本没办法登录.

Allowing SSH access to ESXi/ESX hosts with public/private key authentication (1002866)

比较坑的是其文档提供的还是一般的配置方式.

[root@localhost:~] cd ~/.ssh
[root@localhost:/.ssh] ls
id_rsa       id_rsa.pub   known_hosts

打开这个目录, 默认下就存在三个文件.

一开始以为是需要使用exsi生成的密钥来配置, 但是

[root@localhost:/.ssh] cat id_rsa.pub
[root@localhost:/.ssh]

[root@localhost:~] ssh-keygen -t rsa
-sh: ssh-keygen: not found

# ./ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key

这三个文件实际上是空的, 同时ssh-keygen(1) - OpenBSD manual pages命令也不是直接使用, 实际的写法有些差异? SSH connection after upgrading to ESXi 5.0 fails with the auth.log error: Could not load host key (vmware.com)

ssh-copy-id walle@192.168.2.1

/etc/init.d/SSH restart

reboot

写入本地主机的公钥, 也无法免密码登录, 重启exsi后, 创建的授权authorized_keys也会丢失, 这个位置因该是无法写入信息的, 只是个保留空文件, 也许是exsi版本的问题.

继续翻查vm的文档, ESXi 加密密钥生成 (vmware.com)

在这个文档下:

# SSH 公钥位于以下位置:

/etc/ssh/ssh_host_rsa_key.pub

/etc/ssh/ssh_host_ecdsa_key.pub

# SSH 私钥位于以下位置:

/etc/ssh/ssh_host_rsa_key

/etc/ssh/ssh_host_ecdsa_key

找到这个位置出现密钥的所在的文件夹, 找到其中的配置文件:

cat sshd_config

hostkey /etc/ssh/ssh_host_rsa_key
hostkey /etc/ssh/ssh_host_ecdsa_key
compression no
authorizedkeysfile /etc/ssh/keys-%u/authorized_keys

打开文件可以看到这几个关键信息. 即授权文件的路径: /etc/ssh/keys-%u/authorized_keys

这几个密钥文件也是已经存在内容的, 而不是空文件.

[root@localhost:/etc/ssh] cat ssh_host_rsa_key.pub >> ./keys-root/authorized_keys

这里直接将公钥写入授权文件中, 将私钥下载到本地主机.

@Lian ➜ ~\Desktop ( base 3.9.12)  ssh -i "C:\Users\Lian\Downloads\ssh_host_rsa_key" root@192.168.2.1
The time and date of this login have been sent to the system logs.

WARNING:
   All commands run on the ESXi shell are logged and may be included in
   support bundles. Do not provide passwords directly on the command line.
   Most tools can prompt for secrets or accept them from standard input.

VMware offers powerful and supported automation tools. Please
see https://developer.vmware.com for details.

The ESXi Shell can be disabled by an administrative user. See the
vSphere Security documentation for more information.
[root@localhost:~] cd ~/.ssh
[root@localhost:/.ssh] ls
id_rsa       id_rsapub   known_hosts
[root@localhost:/.ssh] cat id_rsa.pub
[root@localhost:/.ssh]

可以看到可以正常连接上.

将私钥文件发送到iPhone上, 使用PortX(免费的终端app)添加上去即可.

img

三. SSH连接Windows

补充这部分的内容, 由于微软为ios开发的远程桌面要求15以上版本, 以及远程桌面在手机上的体验不是很好, 还是命令行更为便捷.

img

找到ssh server, 安装.

默认下ssh server不启动.

img

启用即可.

登录方式和其他的没什么区别, 没什么好说的, 包括win11的微软账号, 也是像远程桌面一样.

但是密钥登录方式的配置相对不一样, 详情见 Key-based authentication in OpenSSH for Windows | Microsoft Learn

由于连接上之后, 使用的是cmd环境, 不是powershell(不知道能不能改成powershell环境...懒得捣鼓了), 使用上多有不便, 鉴于也只是用来简单控制, 如音乐播放控制, 关机等, 就不深究其中的使用细节了.

foobar2000.exe /play
foobar2000.exe /next

例如支持命令行操作的foobar, 但是需要注意的是当ssh断开, foobar也会被自动关闭..

img