Linux command hostname-Display or set host name

Source: Internet
Author: User
Tags fully qualified domain name

The hostname of the Linux command I used-show or set host name

This article link: http://codingstandards.iteye.com/blog/804648 (reproduced please specify the source)

Description of Use

The hostname command can be used to display or set the hostname (show or set the system's host name). The environment variable hostname also holds the current host name. After you use the hostname command to set the hostname, the system does not permanently save the new host name, after restarting the machine or the original host name. If you need to permanently modify the hostname, you need to modify the contents of both/etc/hosts and/etc/sysconfig/network. Some software that relies on the system hostname may have problems after modifying the host name, such as when synchronizing replication for MySQL, it is best to stop it before modifying the hostname (experience, I've been tossing it for a long time).

Common parameters

Displays the host name without parameters.

Format: hostname

Displays the IP address when you bring the-I parameter.

Format: hostname-i

Displays the full domain name (fqdn,fully qualified domain name) with the-f parameter.

Format: hostname-f

When you modify the host name, follow the new host name. (Note that this method takes effect immediately, but is not persisted, as described in the following example).

Format: hostname newhostname

Use example one to display the host name

[Email protected] ~]# hostname
Jfht
[Email protected] ~]# echo $HOSTNAME
Jfht
[[email protected] ~]# grep jfht/etc/hosts
127.0.0.1 jfht localhost.localdomain localhost

Note: The hosts of this machine have some problems, should be set up another line IP FQDN hostname, such as 192.168.1.191 jfht.xyz.com jfht.

[Email protected] ~]# hostname-i
127.0.0.1 <== The loopback address (loopback 127.0.0.1) is displayed because the/etc/hosts file is not set properly .

[Email protected] ~]# hostname-f
JFHT <== The FQDN cannot be displayed because the FQDN is not set in the/etc/hosts file.
[[email protected] ~]# grep jfht/etc/sysconfig/network
Hostname=jfht

[Email protected] ~]# cat/proc/sys/kernel/hostname
Jfht
[Email protected] ~]#

Example two modifying host names

Now demonstrate the conversion of the original hostname rhel55 to new55 and increase the FQDN setting.

[[email protected" ~]# hostname  
Rhel55
[[email protected] ~]# hostname-i  
127.0.0.1
[[email protected] ~]# hostname-f  
Rhel55
[[email protected] ~]# hostname new55  
[[email  Protected] ~]# echo $HOSTNAME  
rhel55
[[email protected ] ~]# hostname  
new55
[[email protected] ~]# vi/etc/hosts

Open the/etc/hosts and

127.0.0.1 rhel55 localhost.localdomain localhost

Switch

127.0.0.1 localhost.localdomain localhost

and add a line

192.168.6.55 new55.somedomain.com new55

[Email protected] ~]# vi/etc/sysconfig/network

Will

Hostname=rhel55

Change into

Hostname=new55

Now quit the current shell to log back in and see.

[[email protected" ~]# exit  
Logout

Last Login:sat nov  6 17:00:08 from 192.168.6.106
[[email protected] ~]# echo $HOSTNAME  
new55
[[email protected] ~]# hostname  
new55
[[email protected] ~]#  Hostname-i  
192.168.6.55
[[email protected] ~]#  Hostname-f  
new55.somedomain.com
[[email protected] ~]# 

reboot Linux and log in again to see that the hostname modification is in effect.  
[[email protected] ~]# reboot


Last Login:sat Nov 6 17:10:08 from 192.168.6.106
[Email protected] ~]# echo $HOSTNAME
New55
[Email protected] ~]# hostname
New55
[Email protected] ~]# hostname-i
192.168.6.55
[Email protected] ~]# hostname-f
New55.somedomain.com
[Email protected] ~]#

Here, summarize the method of modifying the hostname:

1. Stop the software that is associated with the host name, such as MySQL.

2. First use hostname newname to set the hostname.

3. In/etc/hosts, modify or add the IP FQDN newname line and check the original 127.0.0.1 row settings.

4. Modify the Hostname=newname in/etc/sysconfig/network.

5. Restart the machine and confirm that the modification is successful.

Problem thinking related information

"1" cnblogs [Linux.bash] Several configuration files and commands about host name

"2" Interactive encyclopedia FQDN

"3" Baidu space Linux How to modify hostname (ZZ)

"4" linuxsir.org Linux operating system hostname (hostname) simplified (v0.1b)

"5" Chinaunix how to modify the hostname of Linux

"6" Robinwu log Redhat Linux modifies host name (HOSTNAME)

Return to the complete list of Linux command series I have used

Linux command hostname-Display or set host name

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.