您的位置:首页 > 其它

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

2012-09-21 19:28 603 查看


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
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: