The Linux operating system's hostname is a kernel variable that can be viewed by hostname commands to view the hostname of the machine. You can also view it directly cat/proc/sys/kernel/hostname.
#hostname
#cat/proc/sys/kernel/hostname
The above two output results are the same.
Modifies the hostname of the runtime Linux system, i.e. no need to restart the system
Hostname command to set the hostname of the system
#hostname newname
NewName is the new hostname to be set, it takes effect immediately after the system restarts, but if you want to permanently change the hostname of the system, modify the relevant settings file.
Permanently change the hostname of Linux
There is a saying in man hostname that "the host name is usually set once at system startup In/etc/rc.d/rc.inet1 Or/etc/init.d/boot (normal Ly by reading the contents of a file which contains the host name, e.g./etc/hostname). " Redhat does not have this file, but by/etc/rc.d/rc.sysinit this script is responsible for setting the system hostname, it reads/etc/sysconfig/network this text file, Redhat's hostname is set in this file.
Therefore, if you want to permanently modify the Redhat hostname, modify the/etc/sysconfig/network file, the hostname in the line to change into Hostname=newname, Where newname is the hostname you want to set up.
The hostname configuration file for the Debian distribution is/etc/hostname.
After the configuration file is repaired, a new hostname is read from the configuration file when the system is restarted.
The relationship between hostname and/etc/hosts
It's very extraordinary. A mention of change hostname first thought of modifying the/etc/hosts file, that hostname configuration file is/etc/hosts. Actually, it's not.
The Hosts file works quite like DNS, providing the IP address to hostname correspondence. The early Internet computers were few, and the stand-alone hosts file was sufficient to store all networked computers. But with the development of the Internet, this is far from enough. A distributed DNS system is then present. The DNS server provides a similar IP address to the corresponding domain name. Specific can man hosts.
The Linux system queries the/etc/hosts file before issuing a domain name resolution request to the DNS server, and if it has a corresponding record, it will use the records inside the hosts. /etc/hosts files usually contain this record
127.0.0.1 localhost.localdomain localhost
The Hosts file format is a row of records, the IP address hostname aliases, the three are separated by white space characters, aliases optional.
127.0.0.1 to localhost This recommendation does not change because many applications use this, such as SendMail, which may not work properly after the modification.
After modifying hostname, if you want to use Newhostname to access it on this computer, you must add a newhostname record in the/etc/hosts file. For example, my eth0 IP is 192.168.1.61, I will change the hosts file as follows:
#hostname blog.infernor.net
# cat/etc/hosts
127.0.0.1 localhost.localdomain localhost
192.168.1.61 blog.infernor.net Blog
This way, I can access the machine via blog or blog.infernor.net.
From the above,/etc/hosts is not directly related to setting up hostname, only when you want to use the new hostname on this machine to access your own time to use the/etc/hosts file. There is no definite connection between the two.
There is another problem with Rhel.
I started in the test, only modify/etc/hosts, add 192.168.1.61 blog.infernor.net Blog, and/etc/sysconfig/network maintain the original, that is, the hostname= inside Localhost.localdomain. I restarted the system unexpectedly found hostname to change into a blog.infernor.net. In this way, I really think/etc/hosts is a hostname configuration file. Finally, I found the problem in/etc/rc.d/rc.sysinit this startup script.
The Rc.sysinit file has been set up in the first hostname
if [-f/etc/sysconfig/network]; Then
. /etc/sysconfig/network
Fi
If [-Z] $HOSTNAME "-o" $HOSTNAME "=" (none) "]; Then
Hostname=localhost
Fi
It does use the hostname value in/etc/sysconfig/network. But there's a hostname in the back.
Ipaddr=
If ["$HOSTNAME" = "localhost"-o "$HOSTNAME" = "Localhost.localdomain"]
; Then
ipaddr=$ (IP addr Show to 0/0 scope Global | awk '/[[:space:]]inet
/{print gensub ("/.*", "", "G", $ $)} ')
If [-N "$ipaddr"]; Then
Eval $ (ipcalc-h $ipaddr 2>/dev/null)
Hostname ${hostname}
Fi
Fi
The script determines whether hostname is localhost or localhost.localdomain, and if so, the hostname of the system will be reset using the hostname corresponding to the interface IP address. The problem is here, my/etc/sysconfig/network default hostname is Localhost.localdomain,eth0 IP is 192.168.1.61, and/etc/ The hosts have 192.168.1.61 records. So he replaced the hostname with the 192.168.1.61 record.
It is also estimated that this is why many people mistakenly think of/etc/hosts as a hostname configuration file.
Hostname with option query
Hostname-s-f-I and so on the options are used to/etc/hosts or DNS system, and we discussed the hostname a bit far, but also prone to misunderstanding. Specific can be man hostname view.
The meaning and modification of Linux hostname