關於hostname和fqdn的區別和獲取及設置


http://cuidehua.blog.51cto.com/5449828/1788240

標簽: hostname  fqdn  salt
原創作品,允許轉載,轉載時請務必以超鏈接形式標明文章  原始出處 、作者信息和本聲明。否則將追究法律責任。 http://cuidehua.blog.51cto.com/5449828/1788240

很多應用軟件會涉及到fqdn

如salt獲取到的fqnd和host可能會不一樣,兩者有何區別?

 

   THE FQDN

       You can’t change the FQDN (as returned by hostname --fqdn) or the DNS domain name (as returned by dnsdomainname) with this command.  The  FQDN  of

       the system is the name that the resolver(3) returns for the host name.

 

       Technically:  The  FQDN  is the name gethostbyname(2) returns for the host name returned by gethostname(2).  The DNS domain name is the part after

       the first dot.

 

       Therefore it depends on the configuration (usually in /etc/host.conf) how you can change it. Usually (if the hosts file is parsed  before  DNS  or

       NIS) you can change it in /etc/hosts.

 

       If  a machine has multiple network interfaces/addresses or is used in a mobile environment, then it may either have multiple FQDNs/domain names or

       none at all. Therefore avoid using hostname --fqdn, hostname --domain and dnsdomainname.  hostname --ip-address is subject to the same limitations

       so it should be avoided as well.

 

 

FQDN = 簡短的名字 + 域   

 

如: hostname返回的結果是   dev-qa001  域為test.com  則fqdn為  dev-qa001.test.com

 

-f, --fqdn, --long

              Display the FQDN (Fully Qualified Domain Name). A FQDN consists of a short host name and the DNS domain name. Unless you are using  bind  or

              NIS for host lookups you can change the FQDN and the DNS domain name (which is part of the FQDN) in the /etc/hosts file. See the warnings in

              section THE FQDN above, and avoid using this option; use hostname --all-fqdnsinstead.

 

 

返回hostname的命令

1
hostname

返回域的命令 

1
dnsdomainname

 

返回fqdn的命令

1
hostname  -f

 

1
hostname  -f - v

可以查看解析的詳細過程

 

 

有時候這兩個還不一樣

hostname --all-fqdns  和 hostname -f

 

python中socket的庫 socket.getfqdn() 這個方法是調用 hostname --all-fqdsn哦! salt中可能會有坑(注意  minion端的/etc/hosts文件)

 

 

如何設置fqdn

 

優先的方法,方法一 設置/etc/hosts

[root@CentOS-6-121 ~]# cat /etc/hosts | grep -i "Centos"

10.59.74.121  CentOS-6-121.test.com CentOS-6-121

 

第一列為IP  第二列為fqnd 第三列為主機名  (位置不能錯誤,否則結果不一致)

1
2
3
4
5
6
[root@CentOS-6-121 ~] # hostname  
CentOS-6-121
[root@CentOS-6-121 ~] # hostname -f
CentOS-6-121. test .com
[root@CentOS-6-121 ~] # dnsdomainname 
test .com

 

 

 

方法二:你配置了dns 通過dns查找

[root@CentOS-6-121 ~]# cat /etc/resolv.conf 

search  a.pa.com

 

 

但是你的dns一定要能解析你的fqdn,返回返回失效(你去除了hosts中的配置后)

1
2
3
4
5
6
7
[root@CentOS-6-121 ~] # hostname
CentOS-6-121
[root@CentOS-6-121 ~] # dnsdomainname 
dnsdomainname: Unknown host
[root@CentOS-6-121 ~] # hostname -f
hostname : Unknown host
[root@CentOS-6-121 ~] #

 

 

 

如果能解析,dnsdomainname 為你指定的search (可以指定多個,有一個成功能解析即可)

 

 

 

三: salt獲取fqdn的時候 經驗

 

通過minion端的salt-call -g  才是最准確的 和 python中的socket.getfqdn()

 

通過master端 salt "dev-qa008" grains.item fqdn  獲取到的可能不一致

 

本文出自 “燕子李三” 博客,請務必保留此出處http://cuidehua.blog.51cto.com/5449828/1788240


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM