Linux:Hosts
lastmod 2015-06-13created 2015-01-226 edits
Long ago, before DNS existed, every server's /etc/hosts file had domain and IP address pairs registered directly in the format below, so that the IP address for a domain could be found. Because this was tedious work that required editing the file by hand every time, today a DNS server built with bind is used instead.
$ cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
It is usually configured as follows.
When running a DNS server, the /etc/hosts file is read first by default. If the requested domain is registered there, the DNS request is not sent to the name server, and the connection goes to the address registered there. So, as a kind of trick, you can register and use any IP and domain name you want.
For reference, on Windows the file is at C:\Windows\System32\drivers\etc\hosts.
Change hostname
To change your computer's hostname, edit the file as follows.
Edit the value of HOSTNAME in it, then reboot the system with init 6.
CentOS 6
In versions before CentOS 7, edit /etc/sysconfig/network as follows.
CentOS 7
From 7 on, edit /etc/hostname, as on Debian-based systems.
Check the HOSTNAME as follows.
Favorite site
- LINUX /etc/hosts configuration
- Doly's CentOS7 Lecture 17 6. CentOS Network 3 - Setting the hostname
- Zetawiki: Changing the Linux hostname