Visual Studio基于密钥的SSH远程连接失败问题求助
Visual Studio(非VS Code)SSH密钥远程调试连接失败问题
问题描述
尝试在Visual Studio(非VS Code)中配置基于密钥的SSH远程调试,在「连接到远程系统」对话框添加新连接时第一步就失败。
已验证的信息:
- 私钥为PEM格式(以
-----BEGIN RSA PRIVATE KEY-----开头)。 - 终端执行
ssh -v -i id_rsa user@host_ip,可通过该PEM密钥成功SSH登录,无需密码。 - 目标主机可通过Visual Studio的密码SSH方式正常调试,说明远程调试功能整体可用。
点击连接按钮后出现「连接失败」错误,跨平台日志跟踪信息如下:
10:52:20.2991235 [Info, Thread 1] liblinux.RemoteSystemBase: Connecting over SSH to 192.168.20.2:22 10:52:20.4114885 [Info, Thread 36] liblinux.HostKeyVerifier: Connection failed. 10:52:20.4114885 [Info, Thread 36] liblinux.HostKeyVerifier: System.Security.Cryptography.CryptographicException: Specified padding mode is not valid for this algorithm. at System.Security.Cryptography.RSACryptoServiceProvider.SignHash(Byte[] hash, HashAlgorithmName hashAlgorithm, RSASignaturePadding padding) at System.Security.Cryptography.RSA.SignData(Byte[] data, Int32 offset, Int32 count, HashAlgorithmName hashAlgorithm, RSASignaturePadding padding) at System.Security.Cryptography.RSA.SignData(Byte[] data, HashAlgorithmName hashAlgorithm, RSASignaturePadding padding) at Renci.SshNet.Security.KeyHostAlgorithm.Sign(Byte[] data) at Renci.SshNet.PrivateKeyAuthenticationMethod.Authenticate(Session session) at Renci.SshNet.ClientAuthentication.TryAuthenticate(ISession session, AuthenticationState authenticationState, String[] allowedAuthenticationMethods, SshAuthenticationException& authenticationException) at Renci.SshNet.ClientAuthentication.Authenticate(IConnectionInfoInternal connectionInfo, ISession session) at Renci.SshNet.ConnectionInfo.Authenticate(ISession session, IServiceFactory serviceFactory) at Renci.SshNet.Session.<ConnectAsync>d__180.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Renci.SshNet.BaseClient.<CreateAndConnectSessionAsync>d__56.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Renci.SshNet.BaseClient.<ConnectAsync>d__36.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at liblinux.HostKeyVerifier.<ConnectAsync>d__24.MoveNext() 10:52:20.4114885 [Error, Thread 36] liblinux.RemoteSystemBase: Connection failure over SSH to 192.168.20.2:22 10:52:20.4114885 [Error, Thread 36] liblinux.RemoteSystemBase: System.Security.Cryptography.CryptographicException: Specified padding mode is not valid for this algorithm. at System.Security.Cryptography.RSACryptoServiceProvider.SignHash(Byte[] hash, HashAlgorithmName hashAlgorithm, RSASignaturePadding padding) at System.Security.Cryptography.RSA.SignData(Byte[] data, Int32 offset, Int32 count, HashAlgorithmName hashAlgorithm, RSASignaturePadding padding) at System.Security.Cryptography.RSA.SignData(Byte[] data, HashAlgorithmName hashAlgorithm, RSASignaturePadding padding) at Renci.SshNet.Security.KeyHostAlgorithm.Sign(Byte[] data) at Renci.SshNet.PrivateKeyAuthenticationMethod.Authenticate(Session session) at Renci.SshNet.ClientAuthentication.TryAuthenticate(ISession session, AuthenticationState authenticationState, String[] allowedAuthenticationMethods, SshAuthenticationException& authenticationException) at Renci.SshNet.ClientAuthentication.Authenticate(IConnectionInfoInternal connectionInfo, ISession session) at Renci.SshNet.ConnectionInfo.Authenticate(ISession session, IServiceFactory serviceFactory) at Renci.SshNet.Session.<ConnectAsync>d__180.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Renci.SshNet.BaseClient.<CreateAndConnectSessionAsync>d__56.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Renci.SshNet.BaseClient.<ConnectAsync>d__36.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at liblinux.HostKeyVerifier.<ConnectAsync>d__24.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at liblinux.HostKeyVerifier.<ConnectAsync>d__24.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at liblinux.RemoteSystemBase.<ConnectCoreAsync>d__68.MoveNext()
解决方案
1. 重新生成兼容的RSA密钥
Visual Studio依赖的Renci.SshNet库对密钥签名填充模式要求严格,终端SSH客户端兼容性更强。重新生成适配的RSA密钥:
ssh-keygen -t rsa -b 2048 -m PEM -f id_rsa_vs
-m PEM指定输出格式为PEM- 将生成的公钥
id_rsa_vs.pub添加到远程主机的~/.ssh/authorized_keys文件中,私钥id_rsa_vs用于Visual Studio连接。
2. 转换现有密钥格式
若不想重新生成,可通过ssh-keygen转换现有密钥为兼容格式:
ssh-keygen -p -m PEM -f id_rsa
执行后按提示操作(可留空密码),转换完成后再尝试在Visual Studio中使用该密钥。
3. 更新Visual Studio版本
部分旧版本Visual Studio的Renci.SshNet库存在加密算法兼容bug,更新到最新稳定版可能解决该问题。
4. 检查密钥权限
确保本地私钥文件权限为600(Windows下右键设置权限,仅当前用户可读写);远程主机~/.ssh目录权限为700,authorized_keys文件权限为600,权限异常也会导致连接失败。
内容的提问来源于stack exchange,提问作者jackhab
相关产品推荐
相关产品推荐

