Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to modify the hostname of linux

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to modify the hostname of linux". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to modify the hostname of linux.

The hostname of the Linux operating system is a kernel variable, and you can view the native hostname through the hostname command. You can also view it directly by cat / proc/sys/kernel/hostname.

# hostname

# cat / proc/sys/kernel/hostname

The above two outputs have the same result.

Modify the hostname of the runtime Linux system, that is, there is no need to restart the system

The hostname command sets the hostname of the system

# hostname newname

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

Permanently change the hostname of Linux

There is a saying in man hostname, "The hostname 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 hostname, e.g. / etc/hostname)." There is no such file in RedHat, but the / etc/rc.d/rc.sysinit script is responsible for setting the hostname of the system. It reads the text file / etc/ sysconfig/network, where the hostname of RedHat is set.

So, if you want to permanently modify the hostname of RedHat, modify the / etc/sysconfig/network file and change the line HOSTNAME in it to HOSTNAME=NEWNAME, where NEWNAME is the hostname you want to set.

The configuration file for hostname for the Debian distribution is / etc/hostname.

After the configuration file is modified, the restart system will read the configuration file to set the new hostname.

The relationship between hostname and / etc/hosts

As soon as you mention changing hostname, you first think of modifying the / etc/hosts file and thinking that the configuration file for hostname is / etc/hosts. Actually, it's not.

The hosts file acts like DNS, providing a correspondence between the IP address and the hostname. In the early days, there were few Internet computers, and there was enough to hold all the networked computers in a single hosts file. However, with the development of the Internet, this is far from enough. Then came the distributed DNS system. The DNS server provides a similar IP address to domain name correspondence. You can man hosts for details.

The Linux system will query the / etc/hosts file before issuing a domain name resolution request to the DNS server, and if there is a corresponding record in it, it will use the record in hosts. 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 is the IP address hostname aliases, separated by white space characters, and aliases is optional.

127.0.0.1 to localhost is not recommended to change, because many applications will use this, such as sendmail, after the modification, these programs may not work properly.

After modifying the hostname, if you want to access it with newhostname on the local machine, you must add a record of newhostname in the / etc/hosts file. For example, the IP of my eth0 is 192.168.1.61, and I modify 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

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

Judging from the above, / etc/hosts is not directly related to setting up hostname, and the / etc/hosts file is only used when you want to access yourself with the new hostname on your local computer. There is no necessary connection between the two.

RHEL has one more problem.

When I started testing, I only modified / etc/hosts, adding 192.168.1.61 blog.infernor.net blog to it, while / etc/sysconfig/network left it as it was, that is, the HOSTNAME=localhost.localdomain inside. When I rebooted the system, I found that hostname had been changed to blog.infernor.net. If you look at it this way, you really think / etc/hosts is the configuration file for hostname. The problem was finally found in the startup script / etc/rc.d/rc.sysinit.

Hostname is set at the beginning of the rc.sysinit file.

If [- f / etc/sysconfig/network]; then

. / etc/sysconfig/network

Fi

If [- z "$HOSTNAME"-o "$HOSTNAME" = "(none)"]; then

HOSTNAME=localhost

Fi

The hostname value in / etc/sysconfig/network is indeed used. But there is also a paragraph about setting up hostname.

Ipaddr=

If ["$HOSTNAME" = "localhost"-o "$HOSTNAME" = "localhost.localdomain"]

; then

Ipaddr=$ (ipaddr show to 0 scope global 0 scope global | awk'/ [[: space:]] inet

/ {print gensub ("/. *", "," g ", $2)}')

If [- n "$ipaddr"]; then

Eval $(ipcalc-h $ipaddr 2 > / dev/null)

Hostname ${HOSTNAME}

Fi

Fi

The script determines whether the hostname is localhost or localhost.localdomain, and if so, the hostname corresponding to the interface IP address will be used to reset the system's hostname. That's the problem. The default hostname for my / etc/sysconfig/network is 192.168.1.61 for localhost.localdomain,eth0, while 192.168.1.61 is recorded in / etc/hosts. So I replaced hostname with the record 192.168.1.61.

I guess that's why many people mistake / etc/hosts for hostname's configuration file.

Hostname with option query

Hostname's-s-f-I and other options all use the / etc/hosts or DNS system, which is a little far from the hostname we are talking about, and is prone to misunderstandings. You can check the details on man hostname.

At this point, I believe you have a deeper understanding of "how to modify the hostname of linux". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report