【问题标题】:Is there a way to use a previously specified ssh-config entry in specifying `Hostname`?有没有办法在指定“主机名”时使用先前指定的 ssh-config 条目?
【发布时间】:2021-12-31 05:52:50
【问题描述】:

编辑:我看到过这种方法有效的情况和无效的情况,我不确定我何时/为什么这样做。

假设我有一个足够复杂的条目,我在其中指定多个参数以获取thathost

Host thathost
    ControlMaster auto
    ServerAliveInterval 8
    ConnectTimeout 8
    Hostname 192.168.5.1
    User mememe
    ProxyJump thejumpbox

我想通过添加或覆盖某些配置来重新使用此定义来创建提供不同功能的附加条目。专门指定一个备用端口(不,我不想在命令行上使用它)。

理想情况下,我可以做类似的事情

Host theirhost
    Hostname thathost
    User themthem

Host my-remote-serialport
    Hostname thathost
    RequestTTY yes
    RemoteCommand some-script-that-launches-kermit-on-a-specific-dev-tty

Host my-remote-serialport
    Hostname thathost
    Port 3004

我只希望根据另一个现有主机指定一个主机,我不想修改我的 Host 条目以匹配某些模式“技巧”。

显然我可以使用ProxyCommand ssh -q nc thathost...ProxyJump thathost +Hostname localhost,然后是所有其他覆盖(端口覆盖将传递给nc)但这既丑陋又浪费(额外的会话)-请不要这样回答。

对我来说,这是ssh-config缺少的功能,但也许我看起来不够努力。

【问题讨论】:

    标签: ssh openssh ssh-config


    【解决方案1】:

    不能按照您要求的方式使用,例如重用hostname 定义,但是提供的ssh 解决方案可以解决更多问题。

    一个主机规则可以跨越多个主机。

    Host thathost theirhost my-remote-serialport
        ControlMaster auto
        ServerAliveInterval 8
        ConnectTimeout 8
        Hostname 192.168.5.1
        User mememe
        ProxyJump thejumpbox
    

    但显然这并不能解决您修改某些属性的问题。
    诀窍是,ssh 配置对属性使用 first wins 策略。

    在您的情况下,您只需在主配置前添加 修改

    Host theirhost
        Hostname thathost
        User themthem
    
    Host my-remote-serialport
        Hostname thathost
        Port 3004
    
    Host thathost theirhost my-remote-serialport
        ControlMaster auto
        ServerAliveInterval 8
        ConnectTimeout 8
        Hostname 192.168.5.1
        User mememe
        ProxyJump thejumpbox
    

    theirhost 在两个地方定义,属性HostnameUser 取自第一个定义,所有其他属性取自第二个定义。

    host 部分也接受通配符,例如具有多个反向 ssh 端点的跳转框:

    HOST my_1
       port 2001
    
    HOST my_2
       port 2002
    
    HOST my_3
       port 2003
    
    HOST my_*
       user pi
       hostname localhost
       ProxyJump thejumpbox
    

    【讨论】:

    • 感谢您阐明为什么它有时对我有用,有时对我不起作用(定义顺序)。更新整个配置对我来说可能不切实际(我们有数百个主机条目)。
    • 我熟悉 ssh-config 通配符(请参阅我的注释 I'm not looking to modify my Host entries to match some pattern "tricks")。最后一个块中的hostname localhost 暗示我在我的帖子中也调用了一个“浪费的”本地套接字连接,正如我所知(由我所知),但不是不受欢迎的解决方案。
    • @nhed 最后一个带有 localhost 的块是当您已经使用反向隧道时的示例,但这里可能是一个不好的示例。您不是在寻找模式技巧,但这些不是技巧,这是 openssh 解决此类情况的默认方式
    【解决方案2】:

    没有办法引用不同的块;但是,您可以包含具有 global 配置的特定配置文件。例如,创建一个供thathosttheirhostmy-remote-serialport 使用的文件。我们就叫它foo-config吧。

    ControlMaster auto
    ServerAliveInterval 8
    ConnectTimeout 8
    Hostname 192.168.5.1
    User mememe
    ProxyJump thejumpbox
    

    然后您可以根据需要使用Include 指令来读入:

    Host theirhost
        Include foo-config
        User themthem
    
    Host my-remote-serialport
        Include foo-config
        RequestTTY yes
        RemoteCommand some-script-that-launches-kermit-on-a-specific-dev-tty
    
    Host my-remote-serialport
        Include foo-config
        Port 3004
    

    但是,我怀疑这种方法很少需要,given by jeb 方法在大多数情况下可能就足够了。

    【讨论】:

    • 我们已经广泛使用Include 指令,并且大多数文件都在特定的测试台或站点中定义主机。分解这些站点或测试平台的特定文件将使事情更难找到。我确实喜欢有条件的Include(即在HostMatch 块内)并使用它,但可能没有我应该使用的那么多。是的,@jeb 在他们的第二个区块中的解决方案似乎是迄今为止最接近的(并且可能永远保存一个明确的 openssh 关键字,这可能是一个硬推销)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-21
    相关资源
    最近更新 更多