什么是主机名:
就是本台机器的名字
作用:This command can get or set the host name or the NIS domain name. You can
also get the DNS domain or the FQDN (fully qualified domain name).
Unless you are using bind or NIS for host lookups you can change the
FQDN (Fully Qualified Domain Name) and the DNS domain name (which is
Unless you are using bind or NIS for host lookups you can change the
FQDN (Fully Qualified Domain Name) and the DNS domain name (which is
part of the FQDN) in the /etc/hosts file.
查看或设置主机名、nis域名。也可以查看dns域名或者fqdn(完整域名)。
如果你只用bind或者nid来绑定主机的话,你也可以在/etc/host里改变dns或者fqdn的域名。
例子:
1、查看主机名
[root@cs70-moban ~]# hostname
cs70-moban
cat /proc/sys/kernel/hostname也可以查看主机名
[root@cs70-moban sysconfig]# cat /proc/sys/kernel/hostname
newname
2、修改主机名
[root@cs70-moban sysconfig]# hostname newname
[root@cs70-moban sysconfig]# hostname
newname
但是这种修改字重启的时候会失效
如果要永久修改hostname,就修改/etc/sysconfig/network文件
hostname与/etc/hosts的关系
很过人一提到更改hostname首先就想到修改/etc/hosts文件,认为hostname的配置文件就是/etc/hosts。其实不是的。
hosts文件的作用相当如DNS,提供IP地址到hostname的对应。早期的互联网计算机少,单机hosts文件里足够存放所有联网计算机。 不过随着互联网的发展,这就远远不够了。于是就出现了分布式的DNS系统。由DNS服务器来提供类似的IP地址到域名的对应。具体可以man hosts。
Linux系统在向DNS服务器发出域名解析请求之前会查询/etc/hosts文件,如果里面有相应的记录,就会使用hosts里面的记录。/etc/hosts文件通常里面包含这一条记录
hosts文件格式是一行一条记录,分别是IP地址 hostname aliases,三者用空白字符分隔,aliases可选。
127.0.0.1到localhost这一条建议不要修改,因为很多应用程序会用到这个,比如sendmail,修改之后这些程序可能就无法正常运行。
修改hostname后,如果想要在本机上用newhostname来访问,就必须在/etc/hosts文件里添加一条newhostname的记录。比如我的eth0的IP是192.168.1.61,我将hosts文件修改如下:
# cat /etc/hosts
127.0.0.1 localhost.localdomain localhost
192.168.1.61 blog.infernor.net blog
这样,我就可以通过blog或者blog.infernor.net来访问本机。
从上面这些来看,/etc/hosts于设置hostname是没直接关系的,仅仅当你要在本机上用新的hostname来访问自己的时候才会用到/etc/hosts文件。两者没有必然的联系。