當 ~/.ssh/config 主機名稱和 /etc/hosts 發生衝突時,優先考慮什麼?

當 ~/.ssh/config 主機名稱和 /etc/hosts 發生衝突時,優先考慮什麼?

如果一台主機的定義如下/etc/hosts

192.168.0.100   server

1 的定義~/.ssh/config如下:

 Host    server
         HostName    192.168.0.101

然後您 ssh 進入伺服器:ssh server

這樣的衝突該如何解決呢?我想一個比另一個有更高的優先權。

答案1

如果你這樣做,ssh server伺服器部分可能是一個真實的主機名稱或一些 ssh 內部「暱稱」。 ssh 首先在 .ssh/config 中尋找一些暱稱,如果它在那裡找到配置,它將使用它。如果它沒有找到配置,它會假定一個真實的主機名稱並嘗試透過 /etc/host 和 dns 解析它。

答案2

該文件~/.ssh/config與 無關/etc/hosts。相反,它是一個配置文件,ssh如果存在的話可以使用。

您可以看到,ssh在執行其他操作之前,使用詳細開關 , -vto引用了該檔案ssh

~/.ssh/config 中的主機條目

這裡,我的檔案中有~/.ssh/config一個名為「skinner」的伺服器條目。我透過包含 3-v的開關來啟用調試等級 3。

例子
$ ssh -vvv skinner 
OpenSSH_6.2p2, OpenSSL 1.0.1e-fips 11 Feb 2013
debug1: Reading configuration data /home/saml/.ssh/config
debug1: /home/saml/.ssh/config line 8: Applying options for *
debug1: /home/saml/.ssh/config line 35: Applying options for skinner
debug1: /home/saml/.ssh/config line 55: Applying options for *
debug3: cipher ok: arcfour [arcfour,blowfish-cbc]
debug3: cipher ok: blowfish-cbc [arcfour,blowfish-cbc]
debug3: ciphers ok: [arcfour,blowfish-cbc]
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 50: Applying options for *
debug1: auto-mux: Trying existing master
...

在上面您可以看到正在ssh使用這個定義,甚至沒有諮詢系統的名稱解析設施。

~/.ssh/config 中沒有主機條目

如果檔案中沒有對應的項目~/.ssh/configssh則將查詢系統的 DNS 解析以了解如何連線到已指定的主機名稱。

例子
$ ssh -vvv skinner
OpenSSH_6.2p2, OpenSSL 1.0.1e-fips 11 Feb 2013
debug1: Reading configuration data /home/saml/.ssh/config
debug1: /home/saml/.ssh/config line 8: Applying options for *
debug1: /home/saml/.ssh/config line 55: Applying options for *
debug3: cipher ok: arcfour [arcfour,blowfish-cbc]
debug3: cipher ok: blowfish-cbc [arcfour,blowfish-cbc]
debug3: ciphers ok: [arcfour,blowfish-cbc]
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 50: Applying options for *
debug1: auto-mux: Trying existing master
debug1: Control socket "/home/saml/.ssh/master-saml@skinner:22" does not exist
debug2: ssh_connect: needpriv 0
debug1: Connecting to skinner [192.168.1.3] port 22.
debug1: Connection established.

在這裡您可以看到正在ssh諮詢系統以尋找主機名稱「skinner」的 IP 位址。

筆記:您可以使用getent來尋找系統上的主機名稱:

$ getent hosts skinner
192.168.1.3     skinner.dom.net

答案3

一般來說,對於一般的 Unix 軟體,使用者特定的設定(在本例中為~/.ssh/config)將覆蓋系統範圍的設定(在本例中為/etc/hosts);因此,中的設定~/.ssh/config將具有更高的優先權。

相關內容