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

如何测试Java SSH客户端JSch?本地测试遇连接拒绝问题求助

Troubleshooting JSch "Connection refused: connect" Error for Local Testing

Hey there! Let's work through this JSch connection issue together—since you're testing locally, this should be fixable with a few straightforward checks and tweaks.

1. First, Make Sure Your Local SSH Server is Running

This is the most common cause of the "connection refused" error when testing locally:

  • Windows: You need to enable the OpenSSH Server feature first. Head to Settings > Apps > Optional features > Add a feature, search for "OpenSSH Server", install it, then open services.msc, find "OpenSSH SSH Server", set its startup type to Automatic, and start the service.
  • macOS/Linux: SSH servers are usually pre-installed, but double-check if it's running. Use sudo systemctl status sshd (Linux) or sudo launchctl list | grep sshd (macOS) to verify. If it's stopped, start it with sudo systemctl start sshd (Linux) or sudo launchctl load -w /System/Library/LaunchDaemons/ssh.plist (macOS).

2. Fix Your Connection Details in the Code

Your current code uses placeholders like "username" and "host ip"—swap these for local-specific values:

  • user: Use your actual local machine username (run whoami in terminal to get this)
  • host: Replace with "localhost" or "127.0.0.1" (both point directly to your own machine)
  • port: 22 is the default SSH port, but double-check your SSH server config (usually /etc/ssh/sshd_config on Linux/macOS, or Windows SSH settings) to confirm it's not using a different port.

3. Verify Your Private Key Setup

  • Path correctness: On Linux/macOS, the private key path should be ~/.ssh/id_rsa (note the tilde, which points to your home folder—using just .ssh/id_rsa will look in your Java program's current working directory, not your home). On Windows, it's typically C:\Users\YourUsername\.ssh\id_rsa.
  • Permissions: SSH rejects keys with overly open permissions. On Linux/macOS, run chmod 600 ~/.ssh/id_rsa to lock down access. On Windows, right-click the file > Properties > Security > Advanced, and ensure only your user account has access to it.

4. Test the SSH Connection Manually First

Before debugging code, test a local SSH connection via terminal to rule out server issues:

ssh your-local-username@localhost -p 22

If this works, the problem is in your code; if not, fix the SSH server or config first.

5. Revised Local Test Code

Here's a tweaked version of your code with local-friendly values and basic error handling to help you debug:

import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;

public class LocalJSchTest {
    public static void main(String[] arg) {
        try {
            JSch jsch = new JSch();
            // Auto-get your local username
            String user = System.getProperty("user.name");
            String host = "localhost";
            int port = 22;
            // Auto-point to your home directory's private key
            String privateKey = System.getProperty("user.home") + "/.ssh/id_rsa";

            jsch.addIdentity(privateKey);
            System.out.println("Connecting to " + host + ":" + port + " as " + user);

            Session session = jsch.getSession(user, host, port);
            // Disable strict host checking for local testing (don't do this in production!)
            session.setConfig("StrictHostKeyChecking", "no");
            session.connect();

            System.out.println("Successfully connected!");
            // Add your SSH operations here (e.g., run commands, transfer files)

            session.disconnect();
        } catch (Exception e) {
            System.out.println("Connection failed: " + e.getMessage());
            e.printStackTrace();
        }
    }
}

Quick Final Check

If you still get the error, make sure your local firewall isn't blocking port 22—temporarily disable it for testing to confirm. And remember, for production code, never disable StrictHostKeyChecking; instead, add your local host key to the known_hosts file first.

内容的提问来源于stack exchange,提问作者Aleksandar Grujic

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 06:36:28