大网数据广告活动
服务器运维问答

SSH config file setup for port forwarding AND SSH access to bastion

· 52CE 我爱测

I have a publicly-accessible bastion host. I am presently able to SSH to the bastion with ssh bastion using this in my ~/.ssh/config : Host bastion IdentitiesOnly yes HostName bastion.foo.com User my-user Port 2222 PubKeyAuthenti…

问题原文

原帖链接 · Nick K9 · CC BY-SA 4.0

I have a publicly-accessible bastion host. I am presently able to SSH to the bastion with ssh bastion using this in my ~/.ssh/config:

Host bastion
    IdentitiesOnly yes
    HostName bastion.foo.com
    User my-user
    Port 2222
    PubKeyAuthentication yes
    IdentityFile ~/.ssh/bastion.pem
    ServerAliveInterval 30

Now I have a Redis instance inside the firewall which I would like to forward a local port to. This works:

ssh -L 6000:redis.private.foo.com:6379 bastion

But I want a config Host shortcut so I can just type ssh redis and have the tunnel set up. (I'm happy to devote a terminal tab to it, although I will try -N -f to see if I prefer that.) The host name won't change, and the tunnel needs to go via the bastion. I have tried this:

Host redis
    LocalForward 6000 redis.private.foo.com:6379
    ProxyJump bastion

This doesn't work, and fails with:

channel 0: open failed: connect failed: Temporary failure in name resolution
stdio forwarding failed
kex_exchange_identification: Connection closed by remote host
Connection closed by UNKNOWN port 65535

I have no idea why it's complaining about that MAXINT port number…

I don't want to have to duplicate all the HostName, IdentityFile, etc. for this special case of tunneling to the Redis server. How can I reuse those values but also have a Host configured for when I want the tunnel? This answer seems to say that I should be able to just add HostName redis.private.foo.com to the second Host and it should work, but I get the same error in that case. The only difference is that, with the HostName parameter, the connection fails after ~2min instead of 15s.

I have tested and the bastion host is able to connect to the internal host name on the Redis port.

已采纳解答

原帖链接 · Nick K9 · CC BY-SA 4.0

The solution was to use the ProxyCommand option. So I have these two hosts defined in my ~/.ssh/config:

Host bastion
    IdentitiesOnly yes
    HostName bastion.foo.com # External bastion hostname
    User my-user
    Port 2222
    PubKeyAuthentication yes
    IdentityFile ~/.ssh/bastion.pem
    ServerAliveInterval 30
Host redis
    LocalForward 6000 redis.internal:6379
    Hostname redis.internal # Internal Redis DNS hostname or IP
    ProxyCommand ssh bastion nc %h %p # NB: Connect using above host config

When I want to set up the Redis tunnel, I can just use ssh redis and it will re-use the config from the bastion host definition.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2022-07-22)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。