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

SSH Jump and local command

· 52CE 我爱测

I added this in my ~/.ssh/config to help avoid stupid mistakes: Host *.prod-domain.com LocalCommand print "WARNING: PROD" && print "continue ?" && read PermitLocalCommand yes Which makes ssh print a warning and a prompt when I tr…

问题原文

原帖链接 · Antoine · CC BY-SA 4.0

I added this in my ~/.ssh/config to help avoid stupid mistakes:

Host *.prod-domain.com
    LocalCommand print "WARNING: PROD" && print "continue ?" && read
    PermitLocalCommand yes

Which makes ssh print a warning and a prompt when I try to connect to a host under prod-domain.com.

Now, most hosts do not expose ssh publically, so we have to go through a gateway. I used to do

ssh -J gateway.prod-domain.com target.prod-domain.com

But with the local command enabled, ssh fails with:

Bad packet length 1231976033.
ssh_dispatch_run_fatal: Connection to UNKNOWN port 65535: message authentication code incorrect

Connecting directly (e.g. ssh gateway.prod-domain.com) still works fine, and connecting with a jump works if I comment the local command.

Are local commands and ssh jumps incompatible ? Is it documented somewhere, and is there a way to make it work (like disabling the local command when "jumping"), or did I maybe hit a bug ?

已采纳解答

原帖链接 · Lacek · CC BY-SA 4.0

As the ssh_config manpage says:

The command is run synchronously and does not have access to the session of the ssh(1) that spawned it. It should not be used for interactive commands.

Your problem is with the read statement, it messes up the negotiation process of SSH performed in the tunnel created via gateway.prod-domain.com.

You can configure your client so only the "endpoints" give you the warning, the gateway doesn't, by having an empty entry in your ~/.ssh/config for the gateway, like this:

Host gateway.prod-domain.com gateway
    HostName gateway.prod-domain.com

Host *.prod-domain.com
    ProxyJump gateway
    LocalCommand print "WARNING: PROD" && print "continue ?" && read
    PermitLocalCommand yes

This way, what you tried to do will work, just be sure not to use the "prod-domain" servers as a jump proxy (except for the gateway, of course). Or, to avoid interactive commands altogether, you could use something like this:

Host *.prod-domain.com
    LocalCommand echo -e "\x1b[30;41mWARNING: You are on a PRODUCTIVE system!\x1b0m"
    PermitLocalCommand yes

This way, while you can't prevent the session from establishing, you will be given a big red warning.

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

资料来源与适用范围

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

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

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