/bin/hostname $HOSTNAME /etc/hosts文件关系



1./bin/hostname

   hostname 调用 gethostname()库函数,而gethostname() 库函数调用uname() 获得其nodename内容,故hostname命令最后取的是uname -n 的值。   

hostname will print the name of the system as returned by the gethostname(2) function

The GNU C library does not employ the gethostname() system call; instead, it
       implements gethostname() as a library function that calls uname(2) and copies
       up to len bytes from the returned nodename field into name.  Having performed
       the copy, the function then checks if the length of the nodename was greater
       than or equal to len, and if it is, then the function returns -1 with errno
       set to ENAMETOOLONG; in this case, a terminating null byte is not included in
       the returned name.


2.$HOSTNAME

    其值在/etc/rc.sysinit 启动脚本中中调用HOSTNAME=$(/bin/hostname)设置,如hostname为空,则设置HOSTNAME=localhost

#!/bin/bash
#
# /etc/rc.d/rc.sysinit - run once at boot time
#
# Taken in part from Miquel van Smoorenburg's bcheckrc.
#

HOSTNAME=$(/bin/hostname)

set -m

if [ -f /etc/sysconfig/network ]; then
    . /etc/sysconfig/network
fi
if [ -z "$HOSTNAME" -o "$HOSTNAME" = "(none)" ]; then
    HOSTNAME=localhost
fi


3. hostname 的永久生效方法是在/etc/sysconfig/network  文件中设置

[root@andes.com ~]#cat /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=andes.com

4.  /etc/hosts 其作用是局域网中:IP到主机名的对应关系,系统在域名解析时先查找/etc/hosts文件,再调用DNS域名服务。

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

5. 内存中hostname

   /proc/sys/kernel/hostname 

[root@andes.com ~]#hostname
andes.com
[root@andes.com ~]#echo $HOSTNAME
andes.com
[root@andes.com ~]#uname -n
andes.com
[root@andes.com ~]#cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.59.133 andes.com andes
[root@andes.com ~]#cat /etc/sys
sysconfig/          sysctl.conf         system-release      system-release-cpe  
[root@andes.com ~]#cat /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=andes.com
[root@andes.com ~]#cat /proc/sys/kernel/hostname 
andes.com
[root@andes.com ~]#

相关系统调用信息:

#include <unistd.h>
int gethostname(char *name, size_t len);
 #include <sys/utsname.h>
int uname(struct utsname *buf);
DESCRIPTION         top

       uname() returns system information in the structure pointed to by buf.  The
       utsname struct is defined in <sys/utsname.h>:

           struct utsname {
               char sysname[];    /* Operating system name (e.g., "Linux") */
               char nodename[];   /* Name within "some implementation-defined
                                     network" */
               char release[];    /* OS release (e.g., "2.6.28") */
               char version[];    /* OS version */
               char machine[];    /* Hardware identifier */
           #ifdef _GNU_SOURCE
               char domainname[]; /* NIS or YP domain name */
           #endif
           };



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值