linux命令之hostname

本文解释了主机名的概念及其用途,并介绍了如何查看和修改主机名。此外,还详细阐述了/etc/hosts文件的作用,以及它与主机名之间的关系。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

什么是主机名:

就是本台机器的名字

作用: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

   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

   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文件通常里面包含这一条记录

127.0.0.1 localhost.localdomain localhost

hosts文件格式是一行一条记录,分别是IP地址 hostname aliases,三者用空白字符分隔,aliases可选。

127.0.0.1到localhost这一条建议不要修改,因为很多应用程序会用到这个,比如sendmail,修改之后这些程序可能就无法正常运行。

修改hostname后,如果想要在本机上用newhostname来访问,就必须在/etc/hosts文件里添加一条newhostname的记录。比如我的eth0的IP是192.168.1.61,我将hosts文件修改如下:

#hostname blog.infernor.net
# 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文件。两者没有必然的联系。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值