4

I find that I can put different hostnames in following three files. Can anyone help explain when and how to use each of them? It is a CentOS7 box.

/etc/hostname # run command hostname - I suspect this is the actual hostname

/etc/hosts #for 127.0.0.1 only?

/etc/sysconfig/network 
1
  • These man pages help to understand these files: man 5 hosts, man 5 hostname and man 5 networks Dec 25, 2019 at 18:30

1 Answer 1

7

/etc/hosts is for defining your own domain names for certain IP addresses. It commonly has this entry:

127.0.0.1 localhost

That means that localhost, when used in a browser or other program that accesses the internet, should be directed to 127.0.0.1. It's like having your own local DNS server (that nobody else can use). You can also add more than one name:

127.0.0.1   local localhost

And it doesn't need to be just 127.0.0.1. You can put any number of names to whatever IP addresses you want.


/etc/hostname is how you specify your computer’s hostname. That is used for many things such as in Bluetooth-enabled computers, that’s the name other devices see. There are a lot of other uses; I recommend you take a look at that Server Fault post I linked. As mentioned by ivanivan, your hostname should be resolvable, so that should be in /etc/hosts next to localhost.


The /etc/sysconfig/network file is used to specify information about the desired network configuration.

(https://www.centos.org/docs/5/html/5.2/Deployment_Guide/s2-sysconfig-network.html)

I’ve actually never seen this file before (I run Debian, not CentOS), but it appears to be basically a broader form of /etc/hostname that lets you set more options. It appears that it is also newer than /etc/hostname, but /etc/hostname is still there to support older software.

3
  • 2
    Have I said something wrong that I deserve a downvote? A comment would help me a lot more if I did.
    – zondo
    Apr 6, 2017 at 1:27
  • I was wondering the same thing. I think this answer is fairly (but if I'm a pedant, not exactly) correct and explains the differences nicely.
    – davidgo
    Apr 6, 2017 at 1:35
  • And whatever is in /etc/hostname should be resolvable so put it in /etc/hosts with a reference to 127.0.0.1
    – ivanivan
    Apr 6, 2017 at 2:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .