一直以來(lái)被Linux的hostname和fqdn(Fully Qualified Domain Name)困惑了好久,今天專(zhuān)門(mén)抽時(shí)間把它們的使用細(xì)節(jié)弄清了。
一、設(shè)置hostname/fqdn
在Linux系統(tǒng)內(nèi)設(shè)置hostname很簡(jiǎn)單,如:
$ hostname florian
如果要設(shè)置fqdn的話,需要對(duì)/etc/hosts進(jìn)行配置。
$ cat /etc/hosts 127.0.0.1 localhost 192.168.1.1 florian.test.com florian
/etc/hosts配置文件的格式是:
ip fqdn [alias]...
即第一列為主機(jī)ip地址,第二列為主機(jī)fqdn地址,第三列以后為別名,可以省略,否則至少要包含hostname。
上述配置文件的配置項(xiàng)的第一行為localhost的配置,第二行為主機(jī)名florian配置fqdn=florian.test.com,ip=192.168.1.1。
至于fqdn的域名后綴,最好和文件/etc/sysconfig/network的HOSTNAME配置保持一致:
$ cat /etc/sysconfig/network NETWORKING=yes HOSTNAME=test.com
二、查看hostname/fqdn
配置完成后,可以使用shell命令查看hostname和fqdn:
$ hostname && hostname -f florian florian.test.com
使用ping去測(cè)試hostname的ip映射是否成功。
$ ping florian PING florian.test.com (192.168.1.1) 56(84) bytes of data. $ ping florian.test.com PING florian.test.com (192.168.1.1) 56(84) bytes of data.
也可以使用python命令獲取hostname和fqdn。
$ python Python 2.6.6 (r266:84292, Dec 7 2011, 20:48:22) [GCC 4.4.6 20110731 (Red Hat 4.4.6-3)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import socket >>> socket.gethostname() 'florian' >>> socket.getfqdn() 'florian.test.com'
三、使用ip設(shè)置hostname帶來(lái)的fqdn問(wèn)題
以上描述了正常設(shè)置hostname和fqdn的方法,但是有時(shí)會(huì)使用ip地址直接作為hostname,此時(shí)會(huì)有些不同。
$ hostname 192.168.1.1 $ hostname && hostname -f 192.168.1.1 192.168.1.1
我們發(fā)現(xiàn)使用ip作為hostname后,使用shell命令查詢hostname和fqdn都是ip地址!!!這是因?yàn)镈NS協(xié)議會(huì)解析hostname的內(nèi)容,當(dāng)發(fā)現(xiàn)其為ip地址時(shí),則不會(huì)再去查詢/etc/hosts文件。
再使用python查看一下,會(huì)發(fā)現(xiàn)python獲取的fqdn竟然還是florian.test.com!!!
$ python Python 2.6.6 (r266:84292, Dec 7 2011, 20:48:22) [GCC 4.4.6 20110731 (Red Hat 4.4.6-3)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import socket >>> socket.gethostname() '192.168.1.1' >>> socket.getfqdn() 'florian.test.com'
即便是刷新dns緩存也無(wú)濟(jì)于事:
$ service nscd reload
將/etc/hosts文件的第二行注釋?zhuān)?
cat /etc/hosts 127.0.0.1 localhost # 192.168.1.1 florian.test.com florian
刷新dns緩存:
$ service nscd reload
我們發(fā)現(xiàn)fqdn恢復(fù)正常了。
$ python Python 2.6.6 (r266:84292, Dec 7 2011, 20:48:22) [GCC 4.4.6 20110731 (Red Hat 4.4.6-3)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import socket >>> socket.gethostname() '192.168.1.1' >>> socket.getfqdn() '192.168.1.1'
之所以會(huì)有這樣的行為,是因?yàn)閜ython解析fqdn的邏輯和DNS并不完全一致,它會(huì)根據(jù)hostname查詢對(duì)應(yīng)的ip地址,然后在/etc/hosts內(nèi)獲取ip地址對(duì)應(yīng)的配置行(第一行有效),然后解析fqdn列和alias列,并返回第一個(gè)包含字符'.'的對(duì)應(yīng)列的值。
因此,使用ip設(shè)置hostname時(shí),需要注意兩點(diǎn):
?首先,將hostname設(shè)置為ip地址
?其次,將/etc/hosts內(nèi)包含該ip的配置項(xiàng)移除
為了保險(xiǎn)起見(jiàn),我們可以在/etc/hosts內(nèi)盡可能靠前的位置添加如下配置:
cat /etc/hosts 127.0.0.1 localhost 192.168.1.1 192.168.1.1
這樣,即便是之后有包含該ip的配置項(xiàng)也不會(huì)生效,python會(huì)優(yōu)先解析第二行的配置項(xiàng),并獲取和ip地址完全一樣的fqdn地址。當(dāng)然,使用shell命令hostname獲取fqdn也不會(huì)出錯(cuò),因?yàn)閔ostname已經(jīng)被設(shè)為ip地址形式了。
下面給大家介紹python shell 根據(jù)ip 獲取 hostname || 根據(jù)hostname 獲取 ip
利用 socket 模塊 里的 gethostbyname 函數(shù)
>>> import
socket
>>>
socket
.
gethostbyname
(
"www.baidu.com"
)
'61.135.169.125'
>>>
socket
.
gethostbyname
(
"rs.xidian.edu.cn"
)
'202.117.119.1'
- 1
- 2
- 3
- 4
- 5
方法2 利用 shell 中 hostname 命令
def getHostName(ip):
command
=
'java -jar %s %s "hostname > %s.hostname"'
%(
remoteCmdLoca
,
ip
,
ip
)
result
= subprocess.call(
command
,
shell
=
True
)
command
=
'%s -q -r -pw Sybase123 %s root@%s:/root'
% (
pscpLoca
,
pscpLoca
,
ip
)
result
= subprocess.call(
command
,
shell
=
True
)
command
=
'%s -q -r -pw Sybase123 root@%s:/root/%s.hostname %s'
%(
pscpLoca
,
ip
,
ip
,
sumAutoLoca
)
result
= subprocess.call(
command
,
shell
=
True
)
fileName = sumAutoLoca + ip +
'.hostname'
readFile =
open
(fileName,
'r'
) hostnameInfo = str(readFile.readline().strip(
'\n'
)) readFile.
close
() subprocess.call(
'rm '
+ fileName,
shell
=True) print
"=========%s hostname is %s========"
%(ip,hostnameInfo)
return
hostnameInfo
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
設(shè)計(jì)思路 ##
有時(shí)候socket不太穩(wěn)定, 具體原因帶查明
目的: 根據(jù) ip 獲取 hostname (適當(dāng)改進(jìn)也可逆轉(zhuǎn))
先設(shè)計(jì)了一個(gè)遠(yuǎn)程執(zhí)行 ssh 命令jar, 或者可以用plink, 鏈接enter link description here
利用subprocess.call命令在遠(yuǎn)程ip機(jī)器上執(zhí)行hostname > %s.hostname命令, 將hostname 信息輸出到文件
用pscp將本地的pscp文件復(fù)制到遠(yuǎn)程ip機(jī)器上 /root 目錄下(后來(lái)發(fā)現(xiàn)這步不需要)
然后利用本地的 pscp 將遠(yuǎn)程機(jī)器上帶有hostname的文本文件/root/%s.hostname 復(fù)制到本地
利用 python 的文本讀取功能讀取信息, 從中取出hostname字符串
再利用 rm 命令把遠(yuǎn)程機(jī)器和本地的文本文件都刪除
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
