Linux主機文件中的同一主機名可以使用多個IP地址嗎?


0

方案是,我有一台可以同時在本地(當我與該計算機位於同一網絡中)和在公共(當我在任何外部網絡中)都可以訪問的計算機。我希望我的主機文件看起來像這樣:

10.11.82.40 master.parallel.edu master
123.90.132.98 master.parallel.edu master

這樣,系統將首先嘗試第一個IP地址,如果不起作用,請嘗試下一個IP地址。這可能並且明智嗎?

0

Normally IP adress resolving is done via dedicated name services like dnsmasq, bind etc.

The local hosts file /etc/hosts is generally only used if you have a small internal network - listing all internal hosts and their respectives ip addresses; otherwise it should just contain your server's local name (and localhost).

One solution to your question could be to use your server's name in different subdomains, e.g. master.exernal.example.com and master.internal.example.com; now to address master from the external network you have to make external.example.com your primary search domain in /etc/resolv.conf:

# /etc/resolv.conf at external host
search external.example.com example.com
nameserver ns.example.com

# /etc/resolv.conf at internal host
search internal.example.com example.com
nameserver ns.example.com

(asssuming you have a nameserver at ns.example.com)

In each zone file for .external. and .internal.example.com the hostname points to the respective ip address

# zonefile external network
$ORIGIN external.example.com.
master IN A 123.90.132.98

# zonefile internal network
$ORIGIN internal.example.com.
master IN A 10.11.82.40

This way you can use curl http://master/ within each network without bothering with FullQualifiedDomainNames.