linux - Is modifying /etc/hostname enough to change the hostname?

08
2014-07
  • ravibhuva9955

    I was setting the hostname on my Linux machine with below steps:

    1. echo "ABCD" > /etc/hostname
    2. hostname -F /etc/hostname
    3. Exit from terminal or reboot system
    4. Now I can see the new hostname.

    Now my question is: What I did here, is that enough for setting the hostname or do I additionally have to change other configuration files, if any?

  • Answers
  • l0b0

    You don't need to do anything, since you've already changed the hostname by editing /etc/hostname. If you're trying to do something other than change the hostname as the host knows it, please elaborate.

    /etc/hosts just lists symbolic names for IPs, so that for example ping localhost does the same as ping 127.0.0.1 if you have a line starting with 127.0.0.1 and localhost somewhere else on the line (whitespace separated). You can of course add abcd to that line, but that has nothing to do with the hostname in /etc/hostname, other than that they coincidentally are the same string. You can't for example enter localhost abcd in the file and expect that to resolve abcd → localhost → 127.0.0.1.

    PS: DNS names are not case sensitive, but are conventionally written in lowercase.

  • pengguang001

    As far as I know, you need to change both /etc/hostname and /etc/hosts.


  • Related Question

    networking - Hostname redirection using Hosts file
  • bdhar

    We can use C:\WINDOWS\system32\drivers\etc\hosts file to map hostname with IP addresses like this

    127.0.0.1 localhost

    But is it possible to assign one hostname to another like this?

    localhost a_new_host_name


  • Related Answers
  • harrymc

    From wikipedia :

    The hosts file contains lines of text consisting of an IP address and one or more hostnames, each field separated by white space (blank or tabulation characters). Comment lines may be included; they are indicated by a hash character (#) in the first position of such lines. For example,

    #This is an example of the hosts file
    127.0.0.1  localhost loopback
    
  • njd

    You could put in the hosts file:

    a_new_host_ip_addr localhost 
    

    Beyond that, I think you would need to set up a local (or nearby) DNS server with CNAME entries to map localhost to some other host (and you'd have to remove 'localhost' from the hosts file for this to work).

  • pavium

    You may have got the impression that some kind of 'redirection' is possible from things like

    127.0.0.1    ads.stackoverflow.com
    

    in your hosts file. Purely as an example, you understand. This does not conflict with the usual definition of localhost elsewhere in the same hosts file.

    The Internet has a number of sample hosts file which ensure that unwanted images don't appear on your screen.