hostname command to view the hostname of this machine,
#hostname
Hostname command to set the hostname of the system
#hostname newname
NewName is the new hostname to be set, which takes effect immediately after the run (restarting the shell), but changes are lost after the system restarts, and if you want to permanently change the system's hostname, modify the associated configuration file.
Redhat hostname, modify/etc/sysconfig/network file, will be inside the hostname this line to modify into Hostname=newname, where NEWNAME is the hostname you want to set.
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
A lot of people mention change hostname first think of modifying/etc/hosts file, think 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.
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.
Like what:
#hostname Server1.localdomain
# cat/etc/hosts
127.0.0.1 localhost.localdomain localhost
192.168.2.5 Server1.localdomain
This way, I can access the machine via Server1 or Server1.localdomain.
Also note that 192.168.2.5 must be accessible, otherwise it will take a long time to start at starting Sendmai this step
Hostname in Linux