1. 暫時更改hostname
hostname your-hostname
以下將主機名修改爲XD-ORACLE
2. 修改/etc/sysconfig/network配置文件,重啓後永久生效
[root@localhost ~]# echo $HOSTNAME localhost.localdomain [root@localhost ~]# cp /etc/sysconfig/network /etc/sysconfig/network.bak.modifyHostname [root@localhost ~]# vi /etc/sysconfig/network NETWORKING=yes HOSTNAME=localhost.localdomain #修改如下 HOSTNAME=XD-ORACLE [root@localhost ~]# echo $HOSTNAME XD-ORACLE
3. 修改/etc/hosts本機的域名解析文件
[root@localhost ~]# cp /etc/hosts /etc/hosts.bak.modifyHostname [root@localhost ~]# vi /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 #修改如下 127.0.0.1 localhost localhost.localdomain XD-ORACLE ::1 localhost localhost.localdomain XJ-ORACLE
4. 相關內容
- Linux主機名僅依賴/etc/sysconfig/network配置文件。
- /etc/hosts爲本機的域名解析文件(類似於現在的DNS,只不過以前的域名不多都可以放進一個文件),修改主機名的時候建議同步修改。
-
再shell中直接敲
hostname
,可能會發現變量名未改變。這是因爲該變量來源於 /proc/sys/kernel/hostname(但是用戶不需要手動設置)。該文件會隨着系統啓動時會讀取/etc/sysconfig/network並賦值到該hostname變量上。所有下次啓動是hostname會自動刷新。 -
在
man hostname
可以發現又如下描述:The host name is usually set once at system startup in /etc/rc.d/rc.inet1 or /etc/init.d/boot (normally by reading the contents of a file which contains the host name, e.g. /etc/hostname).
-
本機系統是CentOS6.5 併爲發現上述啓動配置文件,但是我在另一個系統啓動文件裏面找到了一些描述:
[root@XD-ORACLE ~]# vi /etc/rc.d/rc.sysinit #!/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 #####省略##### # Use the network address to set the hostname of the client. This # must be done even if we have local storage. ipaddr= if [ "$HOSTNAME" = "localhost" -o "$HOSTNAME" = "localhost.localdomain" ]; then ipaddr=$(ip addr show to 0.0.0.0/0 scope global | awk '/[[:space:]]inet / { print gensub("/.*","","g",$2) }') for ip in $ipaddr ; do HOSTNAME= eval $(ipcalc -h $ip 2>/dev/null) [ -n "$HOSTNAME" ] && { hostname ${HOSTNAME} ; break; } done fi