使用shell/python获取hostname/fqdn释疑
一直以来被Linux的hostname和fqdn(Fully Qualified Domain Name)困惑了好久,今天专门抽时间把它们的使用细节弄清了。
一、设置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
使用shell/python获取hostname/fqdn释疑的更多相关文章
- 使用shell/python获取hostname/fqdn释疑(转)
一直以来被Linux的hostname和fqdn(Fully Qualified Domain Name)困惑了好久,今天专门抽时间把它们的使用细节弄清了. 一.设置hostname/fqdn 在Li ...
- shell,python获取当前路径(脚本的当前路径) (aso项目记录)
一.shell获取脚本当前路径 cur_dir=$(cd "$(dirname "$0")"; pwd) #获取当前脚本的绝对路径,参数$0是当前脚本对象 等 ...
- 基于Python Shell获取hostname和fqdn释疑
一直以来被Linux的hostname和fqdn(Fully Qualified Domain Name)困惑了好久,今天专门抽时间把它们的使用细节弄清了. 一.设置hostname/fqdn 在Li ...
- Shell Python 日期和时间戳的互相转换
一.初衷: 很多时候,时间的存储都是时间戳格式,如果需要展示就要转化成标准格式日期.也许会需要date和timestamp互转. 二.方法: 1.Shell下对date和timestamp的互转,是通 ...
- shell中获取本机ip地址
shell中获取本机ip地址 方法一: /sbin/ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr ...
- Linux下Python获取IP地址
<lnmp一键安装包>中需要获取ip地址,有2种情况:如果服务器只有私网地址没有公网地址,这个时候获取的IP(即私网地址)不能用来判断服务器的位置,于是取其网关地址用来判断服务器在国内还是 ...
- python 获取代码宿主机名 ip
1.获取hostname 相同代码 不同宿主机 日志名 互异 且 可识别宿主机 分布式爬虫 https://docs.python.org/3.6/library/socket.html#socket ...
- Linux 项目 shell 自动获取报告本机IP (1) | 通过shell 自动获取报告本机IP
由于电脑设置静态IP经常出现链接不上网络,动态IP又非常不方便,故有了这个想法并实现 原理: Linux,包含PC机器,树莓派等,通过shell 自动获取报告本机IP | 通过 Mutt+Msmtp ...
- bat wmic python 获取进程的所在路径
bat wmic python 获取进程的所在路径 doc: wmic process where name="process-name" get executablepath w ...
随机推荐
- echarts之tooltip-showContent
当trigger:为'axis'时 tooltip : { trigger: 'axis', showContent:false } 当trigger:为'item'时 tooltip : { tri ...
- NPM 使用淘宝镜像
--registry https://registry.npm.taobao.org
- python 爬虫(二)
python 爬虫 Advanced HTML Parsing 1. 通过属性查找标签:基本上在每一个网站上都有stylesheets,针对于不同的标签会有不同的css类于之向对应在我们看到的标签可能 ...
- PHP CI框架 result()详解
该方法执行成功返回一个对象数组,失败则返回一个空数组. 一般情况下,我们使用下面的方法遍历结果,代码就像这样: $query = $this->db->query("要执行的 S ...
- CentOS 搭建 nginx + tomcat
安装nginx yum install nginx 修改 nginx.conf, (/etc/nginx/nginx.conf), 网上有人做人所有配置项目的详解. #nu For more info ...
- 大神的Blog挂了,从Bing快照里复制过来的备份
UWidget封装SWidget到UMG 2015年8月30日0 为了使用UMG中的一些高级或便利特性,需要将制作好的Slate控件封装到UWidget中去. 当前UE4版本4.8.3. 将Slate ...
- 分布式大数据高并发的web开发框架
一.引言 通常我们认为静态网页html的网站速度是最快的,但是自从有了动态网页之后,很多交互数据都从数据库查询而来,数据也是经常变化的,除了一些新闻资讯类的网站,使用html静态化来提高访问速度是不太 ...
- HTML5 学习笔记(三)——本地存储
目录 一.HTML4客户端存储 1.1.提交表单发送到服务器的信息 1.2.客户端本地存储概要 二.localStorage 2.1.添加 2.2.取值 2.3.修改 2.4.删除 2.5.跨页面与跨 ...
- 在安装AndroidStudio时产生的问题
JDK安装完毕,环境变量也配置完了,AndroidStudio安装完了,其中SDK使用了自己下载好的包. 但是第一次打开AndroidStudio的时候出现了错误: java.lang.Runtime ...
- 【Redis】:Jedis 使用
Redis 支持很多语言, 例如C#,RUBY,JAVA 等, Jedis是redis的java版本的客户端实现 一个简单的Jedis使用 依赖第三方包jedis-2.7.2.jar commons- ...