Debian Java远程连接常见场景及配置指南
若需通过命令行远程管理Debian系统上的Java应用,需先配置SSH服务:
sudo apt update && sudo apt install openssh-server
/etc/ssh/sshd_config文件,修改以下关键配置:
Port 22(保留默认端口或更换为非标准端口,如2222);PermitRootLogin no(禁止root直接登录,降低风险);PasswordAuthentication yes(允许密码认证,若需更安全可改为no并使用密钥);AllowUsers your_username(仅允许指定用户登录,替换为实际用户名)。sudo systemctl restart sshd
ssh-keygen -t rsa -b 4096 # 本地生成密钥
ssh-copy-id your_username@debian_server_ip # 复制公钥到服务器
ssh your_username@debian_server_ip -p 22
若需调试运行在Debian上的Java应用(如Spring Boot),需开启远程调试功能:
java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005 -jar your-application.jar
参数说明:
server=y:应用作为调试服务器等待连接;suspend=n:不暂停应用启动(设为y则需调试器连接后才启动);address=5005:调试端口(可根据需要修改)。Run -> Edit Configurations;+号选择Remote;Host(服务器IP)、Port(如5005);OK保存,点击调试按钮即可连接。Run -> Debug Configurations;Remote Java Application选择New;Name、Host、Port,选择对应项目;Debug开始调试。若需实现Java程序间的远程方法调用(RMI),需完成以下步骤:
java.rmi.Remote的接口,声明远程方法(需抛出RemoteException):import java.rmi.Remote;
import java.rmi.RemoteException;
public interface HelloService extends Remote {
String sayHello() throws RemoteException;
}
java.rmi.server.UnicastRemoteObject:import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class HelloServiceImpl extends UnicastRemoteObject implements HelloService {
protected HelloServiceImpl() throws RemoteException {
super();
}
@Override
public String sayHello() throws RemoteException {
return "Hello from Debian Java!";
}
}
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class Server {
public static void main(String[] args) {
try {
HelloService helloService = new HelloServiceImpl();
Registry registry = LocateRegistry.createRegistry(1099);
registry.bind("HelloService", helloService);
System.out.println("RMI Server ready");
} catch (Exception e) {
e.printStackTrace();
}
}
}
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class Client {
public static void main(String[] args) {
try {
Registry registry = LocateRegistry.getRegistry("debian_server_ip", 1099);
HelloService helloService = (HelloService) registry.lookup("HelloService");
System.out.println(helloService.sayHello());
} catch (Exception e) {
e.printStackTrace();
}
}
}
若需实现跨语言或Web端的远程调用,可使用Spring Boot构建RESTful服务:
start.spring.io)创建项目,添加Spring Web依赖。import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/hello")
public String sayHello() {
return "Hello from Debian Java REST API!";
}
}
./mvnw spring-boot:run
使用curl或Postman测试:curl http://debian_server_ip:8080/hello
ufw)限制端口访问,仅允许可信IP连接;