使用shell/python获取hostname/fqdn释疑
2016-01-24 00:42
525 查看
一直以来被Linux的hostname和fqdn(Fully Qualified Domain Name)困惑了好久,今天专门抽时间把它们的使用细节弄清了。
如果要设置fqdn的话,需要对/etc/hosts进行配置。
/etc/hosts配置文件的格式是:
即第一列为主机ip地址,第二列为主机fqdn地址,第三列以后为别名,可以省略,否则至少要包含hostname。
上述配置文件的配置项的第一行为localhost的配置,第二行为主机名florian配置fqdn=florian.test.com,ip=192.168.1.1。
至于fqdn的域名后缀,最好和文件/etc/sysconfig/network的HOSTNAME配置保持一致:
使用ping去测试hostname的ip映射是否成功。
也可以使用python命令获取hostname和fqdn。
我们发现使用ip作为hostname后,使用shell命令查询hostname和fqdn都是ip地址!!!这是因为DNS协议会解析hostname的内容,当发现其为ip地址时,则不会再去查询/etc/hosts文件。
再使用python查看一下,会发现python获取的fqdn竟然还是florian.test.com!!!
即便是刷新dns缓存也无济于事:
将/etc/hosts文件的第二行注释:
刷新dns缓存:
$ service nscd reload
我们发现fqdn恢复正常了。
之所以会有这样的行为,是因为python解析fqdn的逻辑和DNS并不完全一致,它会根据hostname查询对应的ip地址,然后在/etc/hosts内获取ip地址对应的配置行(第一行有效),然后解析fqdn列和alias列,并返回第一个包含字符'.'的对应列的值。
因此,使用ip设置hostname时,需要注意两点:
首先,将hostname设置为ip地址
其次,将/etc/hosts内包含该ip的配置项移除
为了保险起见,我们可以在/etc/hosts内尽可能靠前的位置添加如下配置:
这样,即便是之后有包含该ip的配置项也不会生效,python会优先解析第二行的配置项,并获取和ip地址完全一样的fqdn地址。当然,使用shell命令hostname获取fqdn也不会出错,因为hostname已经被设为ip地址形式了。
linux基础:设置FQDN和Hostname: http://www.chenshake.com/linux-foundation-set-fqdn-hostname/
Is it valid for a hostname to start with a digit?: http://serverfault.com/questions/638260/is-it-valid-for-a-hostname-to-start-with-a-digit
一、设置hostname/fqdn
在Linux系统内设置hostname很简单,如:$ hostname florian
如果要设置fqdn的话,需要对/etc/hosts进行配置。
$ cat /etc/hosts 127.0.0.1 localhost 192.168.1.1 florian.test.com florian
/etc/hosts配置文件的格式是:
ip fqdn [alias]...
即第一列为主机ip地址,第二列为主机fqdn地址,第三列以后为别名,可以省略,否则至少要包含hostname。
上述配置文件的配置项的第一行为localhost的配置,第二行为主机名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去测试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设置hostname带来的fqdn问题
以上描述了正常设置hostname和fqdn的方法,但是有时会使用ip地址直接作为hostname,此时会有些不同。$ hostname 192.168.1.1 $ hostname && hostname -f 192.168.1.1 192.168.1.1
我们发现使用ip作为hostname后,使用shell命令查询hostname和fqdn都是ip地址!!!这是因为DNS协议会解析hostname的内容,当发现其为ip地址时,则不会再去查询/etc/hosts文件。
再使用python查看一下,会发现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缓存也无济于事:
$ service nscd reload
将/etc/hosts文件的第二行注释:
cat /etc/hosts 127.0.0.1 localhost # 192.168.1.1 florian.test.com florian
刷新dns缓存:
$ service nscd reload
我们发现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() '192.168.1.1' >>> socket.getfqdn() '192.168.1.1'
之所以会有这样的行为,是因为python解析fqdn的逻辑和DNS并不完全一致,它会根据hostname查询对应的ip地址,然后在/etc/hosts内获取ip地址对应的配置行(第一行有效),然后解析fqdn列和alias列,并返回第一个包含字符'.'的对应列的值。
因此,使用ip设置hostname时,需要注意两点:
首先,将hostname设置为ip地址
其次,将/etc/hosts内包含该ip的配置项移除
为了保险起见,我们可以在/etc/hosts内尽可能靠前的位置添加如下配置:
cat /etc/hosts 127.0.0.1 localhost 192.168.1.1 192.168.1.1
这样,即便是之后有包含该ip的配置项也不会生效,python会优先解析第二行的配置项,并获取和ip地址完全一样的fqdn地址。当然,使用shell命令hostname获取fqdn也不会出错,因为hostname已经被设为ip地址形式了。
四、参考资料
Linux下配置FQDN: https://onebitbug.me/2014/06/25/settings-fqdn-in-linux/linux基础:设置FQDN和Hostname: http://www.chenshake.com/linux-foundation-set-fqdn-hostname/
Is it valid for a hostname to start with a digit?: http://serverfault.com/questions/638260/is-it-valid-for-a-hostname-to-start-with-a-digit
相关文章推荐
- Xshell配置ssh免密码登录-密钥公钥(Public key)与私钥(Private Key)登录
- Windows PowerShell基本语法及常用命令
- shell脚本通过expect脚本实现自动输入密码
- 绕过安全狗狗的WebShell for PHP
- 统计文件种类数+获取子shell返回值的其它方法
- linux shell 字符串操作(长度,查找,替换)详解
- Shell脚本中参数传递方法常用有8种
- Linux学习—xShell连接vmware虚拟机设置Ip
- iis7.5加fck解析漏洞后台拿shell
- iis7.5加fck解析漏洞后台拿shell
- DZ拿shell总结
- DZ拿shell总结
- PHPmyadmin拿shell总结
- PHPmyadmin拿shell总结
- win环境下使用sqlmap写shell + MYSQL提权(默认就是system权限)
- 通用型正方教务(通杀各版本)存在注入(不需登陆)+获得webshell+提权内网漫游
- win环境下使用sqlmap写shell + MYSQL提权(默认就是system权限)
- 通用型正方教务(通杀各版本)存在注入(不需登陆)+获得webshell+提权内网漫游
- [python]emlog相册插件getshell exploit
- Dz7.2 从获取uc key到getshell