如何使用来自 PowerShell 的带有私钥的 ssh 连接到 linux 服务器?
How to connect to linux server using ssh with private key from PowerShell?
我已经使用 puttygen 生成了一个 ssh 密钥对,并且可以使用我的私钥从我的 Windows 10 工作站上的 putty 成功连接到我的 CentOS 服务器:
我想从 Windows PowerShell 连接到服务器,并使用 Install-Module posh-ssh
加载了 Posh-SSH 模块
我尝试创建一个新的 ssh 会话:
$Credential = Get-Credential
$KeyFile = 'C:\Users\mark\Documents\ssh\privkey.ppk'
$sesh = New-SSHSession -ComputerName neon.localdomain -Credential $credential -Keyfile $KeyFile
我为 Get-Credential
输入了 root 密码和空白密码,但出现此错误:
New-SSHSession : Invalid private key file.
我试图通过读取 privkey 文件并将其转换为 base64 编码来将其转换为字符串,但我得到了同样的错误:
$privkeyString = Get-Content $KeyFile
$Bytes = [Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes($privkeyString))
$sesh = New-SSHSession -ComputerName neon.localdomain -Credential $credential -KeyString $Bytes
我也试过这个但是得到了同样的错误:
$sesh = New-SSHSession -ComputerName neon.localdomain -Credential $credential -KeyString $privkeystring
关于如何使用带有私钥文件的 PowerShell 连接到 linux 服务器有什么想法吗?
New-SSHSession
无法识别 PuTTY 的密钥格式(遗憾的是 Gallery 和项目页面都没有提到这一点,但我在 PowerShellMagazine article). You need the private key in the OpenSSH format. You can convert the private key 中用 PuTTYgen 找到了它:
- 单击文件 → 加载私钥。
- 如果密钥受密码保护,请输入密码。
- 单击转换 → 导出 OpenSSH 密钥。
- 输入导出密钥的文件名(不要覆盖 PPK 文件)并单击 保存。
- 退出 PuTTYgen。
运行 New-SSHSession
使用新的密钥文件:
$computer = 'neon.localdomain'
$username = 'foo'
$keyfile = 'C:\path\to\priv_openssh.key'
$sess = New-SSHSession -Computer $computer -Credential $username -Keyfile $keyfile
现在可以使用与 Linux 相同的方法。
ssh 用户名@domain.com -p 22 -i C:\Users\user.ssh\private_key_id_rsa

我已经使用 puttygen 生成了一个 ssh 密钥对,并且可以使用我的私钥从我的 Windows 10 工作站上的 putty 成功连接到我的 CentOS 服务器:
我想从 Windows PowerShell 连接到服务器,并使用 Install-Module posh-ssh
我尝试创建一个新的 ssh 会话:
$Credential = Get-Credential
$KeyFile = 'C:\Users\mark\Documents\ssh\privkey.ppk'
$sesh = New-SSHSession -ComputerName neon.localdomain -Credential $credential -Keyfile $KeyFile
我为 Get-Credential
输入了 root 密码和空白密码,但出现此错误:
New-SSHSession : Invalid private key file.
我试图通过读取 privkey 文件并将其转换为 base64 编码来将其转换为字符串,但我得到了同样的错误:
$privkeyString = Get-Content $KeyFile
$Bytes = [Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes($privkeyString))
$sesh = New-SSHSession -ComputerName neon.localdomain -Credential $credential -KeyString $Bytes
我也试过这个但是得到了同样的错误:
$sesh = New-SSHSession -ComputerName neon.localdomain -Credential $credential -KeyString $privkeystring
关于如何使用带有私钥文件的 PowerShell 连接到 linux 服务器有什么想法吗?
New-SSHSession
无法识别 PuTTY 的密钥格式(遗憾的是 Gallery 和项目页面都没有提到这一点,但我在 PowerShellMagazine article). You need the private key in the OpenSSH format. You can convert the private key 中用 PuTTYgen 找到了它:
- 单击文件 → 加载私钥。
- 如果密钥受密码保护,请输入密码。
- 单击转换 → 导出 OpenSSH 密钥。
- 输入导出密钥的文件名(不要覆盖 PPK 文件)并单击 保存。
- 退出 PuTTYgen。
运行 New-SSHSession
使用新的密钥文件:
$computer = 'neon.localdomain'
$username = 'foo'
$keyfile = 'C:\path\to\priv_openssh.key'
$sess = New-SSHSession -Computer $computer -Credential $username -Keyfile $keyfile
现在可以使用与 Linux 相同的方法。
ssh 用户名@domain.com -p 22 -i C:\Users\user.ssh\private_key_id_rsa
