Change the hostname of centos-valid permanently

Source: Internet
Author: User

Change the hostname of centos-valid permanently
Procedure

Uname-a (or hostname) can see the hostname. The modification steps are as follows:

  1. uname -aView hostname
  2. hostname newnameModify to make the hostname take effect immediately.
  3. vi /etc/hostsChange the original hostname to newname.
  4. vi /etc/sysconfig/networkModify the original hostname
    Newname, reboot also takes effect after restart

Reboot restart,uname -aCheck again. Done!

Resolution takes effect temporarily

The hostname of the Linux operating system is a kernel variable.hostnameCommand to view the Host Name of the local machine.

[root@entel2 ~]# hostnameentel2

Or directlycat /proc/sys/kernel/hostnameView.

[root@entel2 ~]# cat /proc/sys/kernel/hostnameentel2
#hostname#cat /proc/sys/kernel/hostname

The above two outputs are the same.

Modify the hostname of the Linux system at runtime, that is, you can set the hostname of the system without restarting the hostname command.

#hostname newname

Newname is the new hostname to be set, which takes effect immediately after running, but changes will be lost after the system is restarted. To change the hostname of the system permanently, modify the relevant settings file.

Change the Linux hostname permanently

In 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 )."

This file is not found in RedHat, but by/etc/rc. d/rc. the sysinit script is responsible for setting the system's hostname. It reads the/etc/sysconfig/network text file, and the RedHat hostname is set in this file.

Therefore, if you want to permanently modify the RedHat hostname, modifyIn the/etc/sysconfig/network file, change the HOSTNAME line to HOSTNAME = NEWNAME, where NEWNAME is the hostname you want to set.

The configuration file of the Debian release hostname is/etc/hostname.

After the configuration file is repaired, the system restarts and reads the configuration file to set a new hostname.

Relationship between hostname and/etc/hosts

Once mentioned, the first thought of modifying the hostname is to modify the/etc/hosts file. The configuration file of hostname is/etc/hosts. Actually not.

The hosts file acts like DNS and provides the corresponding IP address to the hostname.

There were few computers on the Internet in the early days, and the hosts file on a single machine was enough to store all the computers on the Internet.

However, with the development of the Internet, this is far from enough. As a result, a distributed DNS system emerged. The DNS server provides a similar IP address to the corresponding domain name. Man hosts is supported.

The Linux system will query the/etc/hosts file before sending a domain name resolution request to the DNS server. If there is a corresponding record in it, the record in hosts will be used. The/etc/hosts file usually contains this record.

127.0.0.1    localhost.localdomain   localhost

The hosts file format is one record per line, which isThe IP address hostname aliases, which is separated by white spaces. Optional.

We recommend that you do not modify the configuration from 127.0.0.1 to localhost, because many applications use this configuration, such as sendmail. After modification, these programs may not run properly.

After the hostname is modified, if you want to access the host with newhostname, you must add a newhostname record in the/etc/hosts file. For example, if my eth0 IP address is 192.168.123.201, modify the hosts file as follows:

#hostname xiaogongjiang# cat /etc/hosts127.0.0.1 localhost.localdomain localhost192.168.123.201 xiaogongjiang blog

In this way, I can access the local machine through blog or xiaogongjiang.

From the above, it doesn't matter whether/etc/hosts sets the hostname directly, the/etc/hosts file is used only when you want to access your host with a new hostname. There is no bound relationship between the two.

RHEL has another problem.

When I started the test, I only modified/etc/hosts and added192.168.123.201 xiaogongjiang blogAnd/etc/sysconfig/network is the same, that is, HOSTNAME = localhost. localdomain. After I restarted the system, I found that the hostname was changed to xiaogongjiang. In this case, I think/etc/hosts is the configuration file of hostname. Later/etc/rc.d/rc.sysinitThis startup script found the problem.

The rc. sysinit file sets the hostname from the very beginning.

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

The hostname value in/etc/sysconfig/network is used. However, there is another section about setting the hostname

ipaddr=if [ "$HOSTNAME" = "localhost" -o "$HOSTNAME" = "localhost.localdomain" ]; thenipaddr=$(ip addr show to 0/0 scope global | awk '/[[:space:]]inet/ { print gensub("/.*","","g",$2) }')if [ -n "$ipaddr" ]; theneval $(ipcalc -h $ipaddr 2>/dev/null)hostname ${HOSTNAME}fifi

The script checks whether the hostname is localhost or localhost. localdomain. If yes, it uses the hostname corresponding to the interface IP address to reset the system hostname. The problem is that my/etc/sysconfig/network default hostname is localhost. localdomain, eth0 IP is 192.168.123.201, and/etc/hosts contains records of 192.168.123.201. Therefore, the hostname is replaced with the record 192.168.123.201.

It is estimated that this is why many people mistakenly think of/etc/hosts as the hostname configuration file.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.