java连接跳板机linux,VSCode Remote ssh跳板机配置(linux环境)

本文介绍如何利用VSCode的remote-ssh插件通过跳板机实现对内网服务器的远程开发和调试,包括检查SSH服务、配置SSH文件、设置代理命令,以及避免双次密码输入的方法。

VSCode真的是一款非常优秀的编辑器,配合各种插件就成为了一款称手的轻量级IDE,相信很多同学都有在服务器上跑实验的需求,我也不例外。一开始,我采用MobaXterm作为连接远程服务器的工具,但如果想在线上对代码做修改就只能用文本编辑器,没有代码高亮、补全,同时也只能用命令行调试工具,体验就不是很好,VSCode的remote-ssh插件就很好地解决了我的痛点,一键连接服务器,远程开发、调试。

但是有些服务器是在内网,没有公网ip, 举例说,就是我使用的个人电脑A直接连实验室只有内网ip的服务器C是连不上的, 解决方法是先连接实验室内一个有公网ip的服务器B, 然后A连B, B连C, 这样就A间接连了C, B就叫做跳板机。

这里默认VSCode已经安装了remote-ssh插件如图所示:

85715e8fc2b8ff373adfdd1a08645be1.png

1.查看跳板机的ssh服务情况

使用如下命令查看ssh服务有没有运行

ps -ef | grep ssh

如果有,可以看到类似以下内容:

8a2fe2899a0486d692e39baadf25b1f6.png

这证明ssh已经在运行了,进程名为sshd,如果没有运行,可以通过以下命令运行之:

service sshd start

再看下这个ssh服务的网络连接情况:

netstat -ntlp

如果看到如下内容:

60c39c47f696c8181a120c69d0dbe0e0.png

这说明sshd已经能够正常工作了,且端口是22(这个很重要下文会用到), 如果利用客户端(SecurCRT,putty等)连接不上,尝试关闭防火墙试试: service iptables stop

2.配置VSCode的ssh文件

这里修改的文件是下图所示:

d3eab2b189f319bb03882a1af41199ac.png

然后进行如下的配置:

#跳板机名称,可以随便取

Host JumpMachine

#跳板机ip

HostName 1.1.1.1

#登入的用户名,可以不是root

User root

#上文提到的sshd服务端口

Port 22

#目标机名称,可以随便取

Host TargetMachine

#目标机ip

HostName 1.1.1.1

#登入的用户名,可以不是root

User root

#特别注意这里不需要设置端口了

#起跳转作用的代码,可以直接复制,注意JumpMachine需要与前面跳板机名称相同

ProxyCommand ssh -W %h:%p JumpMachine

配置完成后可以看到这样的效果:

73a1f4ca179bd19be7ebd0d51f462020.png

点击+号然后就可以进行远程登陆了,注意这样没有配置公钥的方法需要连续输入两次密码,一次是跳板机的密码,一次是目标机的密码。

后续有空还会更新免密登陆的相关操作步骤。

[21:09:51.015] Log Level: 2 [21:09:51.053] SSH Resolver called for "ssh-remote+192.168.77.25", attempt 1 [21:09:51.062] remote.SSH.useLocalServer = false [21:09:51.063] remote.SSH.useExecServer = true [21:09:51.064] remote.SSH.bindHost = {} [21:09:51.064] remote.SSH.showLoginTerminal = false [21:09:51.064] remote.SSH.remotePlatform = {"192.168.1.254":"linux","192.168.31.22":"linux"} [21:09:51.065] remote.SSH.path = [21:09:51.065] remote.SSH.configFile = [21:09:51.065] remote.SSH.useFlock = true [21:09:51.065] remote.SSH.lockfilesInTmp = false [21:09:51.065] remote.SSH.localServerDownload = auto [21:09:51.065] remote.SSH.remoteServerListenOnSocket = false [21:09:51.066] remote.SSH.defaultExtensions = [] [21:09:51.066] remote.SSH.defaultExtensionsIfInstalledLocally = [] [21:09:51.066] remote.SSH.loglevel = 2 [21:09:51.066] remote.SSH.enableDynamicForwarding = true [21:09:51.066] remote.SSH.enableRemoteCommand = false [21:09:51.066] remote.SSH.serverPickPortsFromRange = {} [21:09:51.066] remote.SSH.serverInstallPath = {} [21:09:51.066] remote.SSH.permitPtyAllocation = false [21:09:51.067] remote.SSH.preferredLocalPortRange = undefined [21:09:51.067] remote.SSH.useCurlAndWgetConfigurationFiles = false [21:09:51.067] remote.SSH.experimental.chat = true [21:09:51.067] remote.SSH.experimental.enhancedSessionLogs = true [21:09:51.067] remote.SSH.httpProxy = {"*":""} [21:09:51.067] remote.SSH.httpsProxy = {"*":""} [21:09:51.080] VS Code version: 1.98.2 [21:09:51.081] Remote-SSH version: remote-ssh@0.120.0 [21:09:51.081] win32 x64 [21:09:51.088] SSH Resolver called for host: 192.168.77.25 [21:09:51.088] Setting up SSH remote "192.168.77.25" [21:09:51.110] Using commit id "ddc367ed5c8936efe395cffeec279b04ffd7db78" and quality "stable" for server [21:09:51.110] Extensions to install: [21:09:51.124] Install and start server if needed [21:09:59.032] Checking ssh with "D:\python35\Scripts\ssh.exe -V" [21:09:59.036] Got error from ssh: spawn D:\python35\Scripts\ssh.exe ENOENT [21:09:59.036] Checking ssh with "D:\python35\ssh.exe -V" [21:09:59.037] Got error from ssh: spawn D:\python35\ssh.exe ENOENT [21:09:59.037] Checking ssh with "D:\CUDA_manager\CUDA126\bin\ssh.exe -V" [21:09:59.038] Got error from ssh: spawn D:\CUDA_manager\CUDA126\bin\ssh.exe ENOENT [21:09:59.039] Checking ssh with "D:\CUDA_manager\CUDA126\libnvvp\ssh.exe -V" [21:09:59.040] Got error from ssh: spawn D:\CUDA_manager\CUDA126\libnvvp\ssh.exe ENOENT [21:09:59.040] Checking ssh with "D:\anaconda3\Anaconda\ssh.exe -V" [21:09:59.041] Got error from ssh: spawn D:\anaconda3\Anaconda\ssh.exe ENOENT [21:09:59.041] Checking ssh with "D:\anaconda3\Anaconda\Scripts\ssh.exe -V" [21:09:59.042] Got error from ssh: spawn D:\anaconda3\Anaconda\Scripts\ssh.exe ENOENT [21:09:59.042] Checking ssh with "D:\VM\bin\ssh.exe -V" [21:09:59.043] Got error from ssh: spawn D:\VM\bin\ssh.exe ENOENT [21:09:59.043] Checking ssh with "C:\Program Files\Common Files\Oracle\Java\javapath\ssh.exe -V" [21:09:59.045] Got error from ssh: spawn C:\Program Files\Common Files\Oracle\Java\javapath\ssh.exe ENOENT [21:09:59.045] Checking ssh with "C:\Program Files (x86)\Common Files\Oracle\Java\javapath\ssh.exe -V" [21:09:59.047] Got error from ssh: spawn C:\Program Files (x86)\Common Files\Oracle\Java\javapath\ssh.exe ENOENT [21:09:59.047] Checking ssh with "C:\windows\system32\ssh.exe -V" [21:09:59.049] Got error from ssh: spawn C:\windows\system32\ssh.exe ENOENT [21:09:59.049] Checking ssh with "C:\windows\ssh.exe -V" [21:09:59.051] Got error from ssh: spawn C:\windows\ssh.exe ENOENT [21:09:59.051] Checking ssh with "C:\windows\System32\Wbem\ssh.exe -V" [21:09:59.052] Got error from ssh: spawn C:\windows\System32\Wbem\ssh.exe ENOENT [21:09:59.052] Checking ssh with "C:\windows\System32\WindowsPowerShell\v1.0\ssh.exe -V" [21:09:59.054] Got error from ssh: spawn C:\windows\System32\WindowsPowerShell\v1.0\ssh.exe ENOENT [21:09:59.054] Checking ssh with "C:\windows\System32\OpenSSH\ssh.exe -V" [21:09:59.113] > OpenSSH_for_Windows_9.5p1, LibreSSL 3.8.2 [21:09:59.121] Running script with connection command: "C:\windows\System32\OpenSSH\ssh.exe" -T -D 64068 "192.168.77.25" sh [21:09:59.125] Generated SSH command: 'type "C:\Users\LHR\AppData\Local\Temp\vscode-linux-multi-line-command-192.168.77.25-870985298.sh" | "C:\windows\System32\OpenSSH\ssh.exe" -T -D 64068 "192.168.77.25" sh' [21:09:59.126] Using connect timeout of 17 seconds [21:09:59.128] Terminal shell path: C:\WINDOWS\System32\cmd.exe [21:10:00.184] > 刘瑞浩@192.168.77.25's password: [21:10:00.185] Got some output, clearing connection timeout [21:10:00.186] Showing password prompt [21:10:03.147] Got password response [21:10:03.148] "install" wrote data to terminal: "****" [21:10:03.193] > [21:10:05.267] > Permission denied, please try again. > 刘瑞浩@192.168.77.25's password: [21:10:05.268] Showing password prompt [21:10:13.601] Got password response [21:10:13.601] "install" wrote data to terminal: "****" [21:10:13.635] > [21:10:15.682] > Permission denied, please try again. > 刘瑞浩@192.168.77.25's password: [21:10:15.683] Showing password prompt [21:10:21.533] Got password response [21:10:21.533] "install" wrote data to terminal: "****" [21:10:21.570] > [21:10:23.250] > \345\210\230\347\221\236\346\265\251@192.168.77.25: Permission denied (publickey,password). > 过程试图入的管道不存在。 [21:10:24.714] "install" terminal command done [21:10:24.715] Install terminal quit with output: 过程试图入的管道不存在。 [21:10:24.716] Received install output: 过程试图入的管道不存在。 [21:10:24.717] WARN: $PLATFORM is undefined in installation script output. Errors may be dropped. [21:10:24.717] Failed to parse remote port from server output [21:10:24.720] Resolver error: Error: at y.Create (c:\Users\LHR\.vscode\extensions\ms-vscode-remote.remote-ssh-0.120.0\out\extension.js:2:744751) at t.handleInstallOutput (c:\Users\LHR\.vscode\extensions\ms-vscode-remote.remote-ssh-0.120.0\out\extension.js:2:742832) at t.tryInstall (c:\Users\LHR\.vscode\extensions\ms-vscode-remote.remote-ssh-0.120.0\out\extension.js:2:865534) at async c:\Users\LHR\.vscode\extensions\ms-vscode-remote.remote-ssh-0.120.0\out\extension.js:2:824246 at async t.withShowDetailsEvent (c:\Users\LHR\.vscode\extensions\ms-vscode-remote.remote-ssh-0.120.0\out\extension.js:2:827501) at async A (c:\Users\LHR\.vscode\extensions\ms-vscode-remote.remote-ssh-0.120.0\out\extension.js:2:820760) at async t.resolve (c:\Users\LHR\.vscode\extensions\ms-vscode-remote.remote-ssh-0.120.0\out\extension.js:2:824898) at async c:\Users\LHR\.vscode\extensions\ms-vscode-remote.remote-ssh-0.120.0\out\extension.js:2:1113660 [21:10:24.733] ------ [21:10:24.733] No hints found in the recent session. VScode连接ssh失败
09-12
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值