在 ssh 設定檔中,我可以使用“Host”作為參數嗎?如果沒有,我怎麼能讓 ssh 設定檔接受參數?

在 ssh 設定檔中,我可以使用“Host”作為參數嗎?如果沒有,我怎麼能讓 ssh 設定檔接受參數?

在 .ssh/config 檔案中

場景是我現在有兩個公用 IP(動態),一個連接到 AWS,一個連接到機架空間

兩者都需要不同的 pem 密鑰才能連接。但我想使用 ssh 設定檔來節省時間

Host <ip1> 
User root
Port 22
HostName %h
IdentityFile ~/.ssh/aws.pem

Host <ip2> 
User root
Port 22
HostName %h
IdentityFile ~/.ssh/rackspace.pem

從這個設定中,我無法區分要使用哪個 pem 檔案。我想使用主機作為參數的一部分來指定要連接到的伺服器,例如 ssh aws-ip1 sshrack-ip2

那麼設定檔可以區分使用哪個pem文件

有人知道怎麼做嗎?

答案1

好吧...這個問題很老了。但我可以想出一種在這種情況下有所幫助的方法。此處使用通配符(*) 和%h 參數。

我們假設 Amazon 的 IP 為 10.10.20.*,Rackspace IP 為 20.20.10.*。以下配置會有幫助:

Host 10.10.20.*
    Hostname %h
    User root
    IdentityFile ~/.ssh/aws.pem

Host 20.20.10.*
    Hostname %h
    User root
    IdentityFile ~/.ssh/rackspace.pem

然後調用 sayssh 10.10.20.5會調用類似的東西,這可能正是所需要的。ssh -i ~/.ssh/aws.pem [email protected]

答案2

“主機”部分是您可以使用的“快捷方式”:

Host aws-ip1
    HostName ip-or-name-to-ip1
    IdentityFile ~/.ssh/aws.pem

Host rack-ip2
    HostName ip-or-name-to-ip2
    IdentityFile ~/.ssh/rackspace.pem

然後你只需使用ssh aws-ip1ssh rack-ip2...

引自man ssh-config

Host    Restricts the following declarations (up to the next Host key-
        word) to be only for those hosts that match one of the patterns
        given after the keyword.

答案3

另一個選擇是使用普通的老式 shell 別名:

 alias ssh_rack="ssh -i .ssh/rackspace.pem"
 alias ssh_aws="ssh -i .ssh/aws.pem"

然後使用它們代替ssh.

相關內容