You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel 5.5中使用SSH密钥无法连接远程服务器的问题排查

Troubleshooting SSH Connection Issues with phpseclib 2.0 Stored Keys

Let’s walk through the most likely issues and fixes for your connection problem:

1. Verify Database Storage Integrity

First, rule out key corruption during storage or retrieval:

  • Check field type: Make sure your meta_value column is set to TEXT or LONGTEXT, not VARCHAR. A 2048-bit RSA private key is roughly 1700 characters long, which exceeds typical VARCHAR limits and will get truncated if you use that type.
  • Compare stored vs. generated keys: Pull the stored private key from your database and compare it directly to the original generated key. You can use nl2br() to visualize line breaks clearly:
    $storedKey = $server->meta()->where('meta_key','private_key')->first()->meta_value;
    echo "Stored key:\n" . nl2br($storedKey) . "\n\n";
    // Compare this output to the original $privateKey from your key generation code
    
    Look for missing characters, escaped line breaks (like \\n instead of \n), or truncated content — any of these would break key validation.

2. Test Key Loading Directly

Skip the Config setup and test loading the key with phpseclib directly to get specific error details instead of the generic "unable to connect" message:

use phpseclib\Crypt\RSA;
use phpseclib\Net\SSH2;

$rsa = new RSA();
$storedPrivateKey = $server->meta()->where('meta_key','private_key')->first()->meta_value;

// Attempt to load the key first
if (!$rsa->loadKey($storedPrivateKey)) {
    die("Failed to load private key — check for format corruption!");
}

// Try connecting directly with the loaded key
$ssh = new SSH2($server->ip);
if (!$ssh->login($server->username, $rsa)) {
    echo "Login failed: " . $ssh->getLastError(); // This gives precise failure reasons
} else {
    echo "Connection successful!";
}

This will tell you if the key itself is invalid, or if the issue lies elsewhere (like server-side restrictions).

3. Fix Line Break Consistency

phpseclib generates keys with \n line breaks, but database storage or retrieval might convert these to \r\n (especially on Windows environments). Normalize the line breaks when retrieving the key to match OpenSSH’s expectations:

$rawKey = $server->meta()->where('meta_key','private_key')->first()->meta_value;
$normalizedKey = str_replace(["\r\n", "\r"], "\n", $rawKey);
Config::set('remote.connections.production.keytext', $normalizedKey);

OpenSSH requires \n line breaks for key files, so this ensures compatibility between your stored key and the server.

4. Check Server-Side SSH Logs

If the above steps don’t resolve the issue, check the remote server’s SSH logs for exact failure details:

  • On Debian/Ubuntu: /var/log/auth.log
  • On CentOS/RHEL: /var/log/secure

Search for entries related to your client IP address — the logs will explicitly state if the key was rejected due to format issues, permission mismatches, or a mismatch with the authorized_keys file.


内容的提问来源于stack exchange,提问作者Adarsh Sojitra

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 06:45:40