转载:关于hostname和fqdn的区别和获取及设置

原文链接:https://blog.51cto.com/cuidehua/1788240

关于hostname和fqdn的区别和获取及设置

很多应用软件会涉及到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-fqdns instead.

 

 

返回hostname的命令

<span style="color:#333333"><span style="color:black"><code class="language-bash"><span style="color:#dd4a68">hostname</span></code></span></span>
 

返回域的命令 

<span style="color:#333333"><span style="color:black"><code class="language-bash">dnsdomainname</code></span></span>
 

 

返回fqdn的命令

<span style="color:#333333"><span style="color:black"><code class="language-bash"><span style="color:#dd4a68">hostname</span> -f</code></span></span>
 

 

<span style="color:#333333"><span style="color:black"><code class="language-bash"><span style="color:#dd4a68">hostname</span> -f -v</code></span></span>
 

可以查看解析的详细过程

 

 

有时候这两个还不一样

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 第三列为主机名  (位置不能错误,否则结果不一致)

<span style="color:#333333"><span style="color:black"><code class="language-bash"><span style="color:#999999">[</span>root@CentOS-6-121 ~<span style="color:#999999">]</span><span style="color:slategray"># hostname  </span>
CentOS-6-121
<span style="color:#999999">[</span>root@CentOS-6-121 ~<span style="color:#999999">]</span><span style="color:slategray"># hostname -f</span>
CentOS-6-121.test.com
<span style="color:#999999">[</span>root@CentOS-6-121 ~<span style="color:#999999">]</span><span style="color:slategray"># dnsdomainname </span>
test.com</code></span></span>
 

 

 

 

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

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

search  a.pa.com

 

 

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

<span style="color:#333333"><span style="color:black"><code class="language-bash"><span style="color:#999999">[</span>root@CentOS-6-121 ~<span style="color:#999999">]</span><span style="color:slategray"># hostname</span>
CentOS-6-121
<span style="color:#999999">[</span>root@CentOS-6-121 ~<span style="color:#999999">]</span><span style="color:slategray"># dnsdomainname </span>
dnsdomainname: Unknown host
<span style="color:#999999">[</span>root@CentOS-6-121 ~<span style="color:#999999">]</span><span style="color:slategray"># hostname -f</span>
hostname: Unknown host
<span style="color:#999999">[</span>root@CentOS-6-121 ~<span style="color:#999999">]</span><span style="color:slategray">#</span></code></span></span>
 

 

 

 

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

 

 

 

三: salt获取fqdn的时候 经验

 

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

 

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

 

©著作权归作者所有:来自51CTO博客作者cuizhiliang的原创作品,如需转载,请注明出处,否则将追究法律责任

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值