/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 };
相关文章推荐
- Linux hostname主机名配置文件/etc/hosts详解 详细出处参考:http://www.jb51.net/LINUXjishu/77329.html
- Linux hostname主机名的配置文件/etc/hosts详细介绍
- Linux hostname主机名配置文件/etc/hosts详解
- Linux hostname主机名配置文件与文件 /etc/hosts解析(copy来的,原作者看到了别打我)
- 修改主机名(/etc/hostname和/etc/hosts区别)
- 小米手机中host文件位置【system/etc/hosts】
- Linux修改本机/etc/hosts的hostName
- 在/etc/hosts中定义Linux的hostname
- Linux hostname主机名配置文件/etc/hosts详解
- cman not started: Local host name resolves to 127.0.0.1; fix /etc/hosts before starting cluster
- ubuntu如何修改terminal终端的主机名(修改/etc/hostname文件)
- socket测试-get host name error和/etc/hosts配置
- 主机名 hostname /etc/hosts
- 修改/etc/hosts 文件,增加ip和hostname的映射关系
- Linux hostname主机名配置文件与文件 /etc/hosts解说[转]
- 修改主机名(/etc/hostname和/etc/hosts区别)
- hostname&&/etc/hosts
- /etc/hosts文件作用
- 自动设置信任关系并修改hosts文件ip
- Linux本地解析文件/etc/hosts说明【原创】