Skip to content
Search

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.

IP  호스트명    Alias
192.168.0.1 www.uzuro.com   uzuro
192.168.0.2 ftp.uzuro.com   ftp

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.

$ vi /etc/sysconfig/network

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.

NETWORKING=yes
NETWORKING_IPV6=no
HOSTNAME=centos70

CentOS 7

From 7 on, edit /etc/hostname, as on Debian-based systems.

$ hostnamectl set-hostname centos7

Check the HOSTNAME as follows.

$ hostname

Favorite site