惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

大猫的无限游戏
大猫的无限游戏
J
Java Code Geeks
小众软件
小众软件
D
Docker
腾讯CDC
H
Hackread – Cybersecurity News, Data Breaches, AI and More
V
V2EX
博客园 - 叶小钗
GbyAI
GbyAI
Microsoft Azure Blog
Microsoft Azure Blog
Stack Overflow Blog
Stack Overflow Blog
B
Blog RSS Feed
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 【当耐特】
IT之家
IT之家
博客园 - 司徒正美
M
MIT News - Artificial intelligence
T
The Blog of Author Tim Ferriss
The GitHub Blog
The GitHub Blog
罗磊的独立博客
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
L
LangChain Blog
阮一峰的网络日志
阮一峰的网络日志
C
Check Point Blog

Allen Hua 的网络博客

由于 Linux 桌面没有一个好用的天气程序就写了一个跨平台的CheckitoutWeather 给 caesium-image-compressor 图片压缩程序构建了 Linux AppImage v2.8.5 最新版 debian13(debian trixie)安装了nvidia闭源驱动后从x11切换到wayland的方法 山间摩旅追风,偶遇一场绚烂晚霞 最近使用debian系统的一些心得 机械革命无界14Pro笔记本debian forky成功驱动内置扬声器和麦克风 记录 typecho 1.2.0 升级到 1.3.0 过程 开发了一款openwrt插件:文本剪贴板 解决机械革命笔记本内屏高刷240Hz闪屏问题 使用 Java 写了一个局域网端口扫描器 - Allen Hua 的网络博客 openwrt使用外置根extroot机制扩展根分区大小 - Allen Hua 的网络博客 将机场ss节点批量转换成ss字符串链接批量添加到passwall - Allen Hua 的网络博客 给机械革命钛钽plus换屏:NY2换成NZ2 - Allen Hua 的网络博客 记录一次pve宿主机和上面的debian虚机无故down机事件 - Allen Hua 的网络博客 给图床部署cdn腾讯云的edgeone并排查Cache-Control max-age 3600的问题 - Allen Hua 的网络博客 从高山草甸到徽派古村:武功山反穿与皖浙赣自驾行记 - Allen Hua 的网络博客 增程器就是充电宝?别被忽悠了 - Allen Hua 的网络博客 浦口龙虎巷扫街,记录人间真实 博客图片压缩方案更新|AVIF|WebP|MozJPEG|标准JPEG Windows 电脑使用 Obs Studio 录制各个网站视频/桌面画面教程 为typecho博客添加latex支持 新能源汽车之纯电车使用交流慢充和直流快充的充电损耗对比 2025年3月更新全国5A景区名录 2025最新查看小米/红米手机电池健康度和循环次数方法 完美解决 seafile FILE_SERVER_ROOT 配置导致的内网外网不能同时访问和上传下载的问题 纯css实现typecho博客文章文字spoiler剧透效果 我对Typecho Facile主题的一些修改,图片懒加载优化,样式定制 2024年8月我的宜昌 - 重庆 - 川西小环线自驾旅行分享 2022年打卡南京市区人防工程纳凉点 2024年带着A7C2+腾龙28-200 再次来到红山森林动物园
ssh 高阶用法 ProxyCommand 通过多层跳板机连接到服务器
Allen Hua · 2021-05-24 · via Allen Hua 的网络博客
阅读量:7396

warning: 这篇文章距离上次修改已过1826天,其中的内容可能已经有所变动。

现同事伙伴连接测试环境上的数据库通过如下命令

ssh -o "ProxyCommand ssh -p 5022 xxx@xxxx -W %h:%p" -p 22 yyy@yyyy

然后通过 mysql 命令连接上数据库

mysql -h zzz -u zzzz -p

mysql 的 -h 参数的意思是 -h, --host=name Connect to host. 然后再根据终端提示输入密码后回车进入到数据库命令行终端。

很明显这样有些麻烦。传统的 navicat 新建连接时只能通过一层跳板机,但是通过 ProxyCommand 的服务肯定无法使用 navicat 自带的这个功能了。

20210524154654.png

navicat 创建连接使用 ssh 通道

解决办法就是创建 ssh 客户端这边的 config,然后在本机创建一个代理,navicat 连接到这个代理服务,这个代理服务器转发请求给实际的数据库服务器。

首先来了解一下 opensshProxyCommand

Specifies the command to use to connect to the server. The command string extends to the end of the line, and is executed using the user's shell ‘exec’ directive to avoid a lingering shell process.
Arguments to ProxyCommand accept the tokens described in the TOKENS section. The command can be basically anything, and should read from its standard input and write to its standard output. It should eventually connect an sshd(8) server running on some machine, or execute sshd -i somewhere. Host key management will be done using the Hostname of the host being connected (defaulting to the name typed by the user). Setting the command to none disables this option entirely. Note that CheckHostIP is not available for connects with a proxy command.

This directive is useful in conjunction with nc(1) and its proxy support. For example, the following directive would connect via an HTTP proxy at 192.0.2.0:
ProxyCommand /usr/bin/nc -X connect -x 192.0.2.0:8080 %h %p

openssh 客户端配置文档来自: https://man.openbsd.org/ssh_config

一、在本机创建 config 文件

配置上以下内容

Host abc
    Hostname yyyy
    Port 22
    User yyy
    ProxyCommand ssh -p 5022 xxx@xxxx -W %h:%p
    IdentityFile /c/Users/YOUR_USERNAME/.ssh/ID_RSA_FILENAME

参数说明

  • Host: 后面定义唯一标识符,依据喜好随意定义
  • Hostname: IP or domain name
  • Port: 端口
  • User: 登录用户
  • ProxyCommand: ProxyCommand 命令,要写全
  • IdentityFile: 认证文件,写上对应私钥的文件路径,如果用的是默认的 id_rsa 文件那么这一行可以省略

保存退出后,windows 下可以通过 git bash 测试: ssh abc,如果成功登录上那么继续

二、在本机创建一个 ssh 客户端代理

ssh -vNL 3367:REAL_IP:3306 DATABASE_USERNAME@abc

使用这句命令 后台运行 nohup ssh -vNL 3367:REAL_IP:3306 DATABASE_USERNAME@abc >> /c/Users/YOUR_USERNAME/.ssh/ssh.log 2>&1 &

参数说明:

  • -v: Verbose mode. Causes ssh to print debugging messages about its progress. This is helpful in debugging connection, authentication, and configuration problems. Multiple -v options increase the verbosity. The maximum is 3.
  • -N: Do not execute a remote command. This is useful for just forwarding ports.
  • -L: Specifies that connections to the given TCP port or Unix socket on the local (client) host are to be forwarded to the given host and port, or Unix socket, on the remote side. This works by allocating a socket to listen to either a TCP port on the local side, optionally bound to the specified bind_address, or to a Unix socket. Whenever a connection is made to the local port or socket, the connection is forwarded over the secure channel, and a connection is made to either host port hostport, or the Unix socket remote_socket, from the remote machine.

    Port forwardings can also be specified in the configuration file. Only the superuser can forward privileged ports. IPv6 addresses can be specified by enclosing the address in square brackets.

    By default, the local port is bound in accordance with the GatewayPorts setting. However, an explicit bind_address may be used to bind the connection to a specific address. The bind_address of “localhost” indicates that the listening port be bound for local use only, while an empty address or ‘*’ indicates that the port should be available from all interfaces.

  • 3367: 3367是你本机开启的端口,等一会 navicat 要绑定这个端口
  • REAL_IP: 这个是 mysql -h 参数指定的 host 的 IP
  • 3306: mysql -h 指定的 host 中安装的 mysql 服务的端口,这里 3306 刚好是 mysql 默认端口
  • DATABASE_USERNAME: 数据库用户名
  • abc: 刚刚 c:\users\YOUR_USERNAME\.ssh\config 中配置的 Host 名

上述一些参数含义描述见官方文档: https://man.openbsd.org/ssh

执行了这条命令后,当前 git bash 终端会保持这个进程,你可以实时观察它的日志。下面打开 navicat 连接本地的这个 ssh 服务

三、navicat 连接数据库,通过跳板机连接数据库服务

20210524161925.png

  • 连接名随意定义
  • 主机名或 IP 地址:localhost
  • 端口:3367
  • 用户名:数据库用户名
  • 密码:数据库密码
  • 切换到右边第四个 tab,SSH,确保没有勾上使用SSH通道,因为此时没有通过这边的ssh通道

连接成功!现在可以通过 gui 操作、查询数据库了。

end.