Sep 172006

Sponsored Link

Checking your Linux host name

First, see if your host name is set correclty using the following commands:

#uname -n
#hostname -a
#hostname -s
#hostname -d
#hostname -f

If the above commands return correctly with no errors then all may be well; however, you may want to read on to verify that all settings are correct.

Normally we will set the hostname of a system during the installation process.

Change the hostname on a running Linux system

On any Linux system you can change its hostname with the command โ€˜hostnameโ€˜.


without any parameter it will output the current hostname of the system.

#hostname --fqd

it will output the fully qualified domain name (or FQDN) of the system.

#hostname NEW_NAME

will set the hostname of the system to NEW_NAME. This is active right away and will remain like that until the system will be rebooted (because at system boot it will set this from some particular file configurations -- see bellow how to set this permanently). You will most probably need to exit the current shell in order to see the change in your shell prompt.

Permanent hostname change on Debian based Linux systems

Debian based systems use the file /etc/hostname to read the hostname of the system at boot time and set it up using the init script /etc/init.d/

#cat /etc/hostname


So on a Debian based system we can edit the file /etc/hostname and change the name of the system and then run

/etc/init.d/ startto make the change active. The hostname saved in this file (/etc/hostname) will be preserved on system reboot (and will be set using the same script we used

If you want more details check hostname man page

Use sysctl to change the hostname

use sysctl to change the variable kernel.hostname:


#sysctl kernel.hostname

to read the current hostname, and

#sysctl kernel.hostname=NEW_HOSTNAME

to change it.

If you want more details check sysctl man page

You can use the same process for all debian based distributions and other linux machines


Sponsored Link

 Posted by at 11:50 am

  26 Responses to “Change hostname or Server name of a Linux Machine”

  1. Shouldn’t that be:
    hostname –fqdn ?

    (or hostname -f).

  2. Thanks… very useful information.

  3. hostname -fqd didn’t work…
    I think it should jsut be hostname -f ?

    I may be wrong though..
    But thanks ๐Ÿ˜€


  4. I think that should be hostname –fqdn

  5. Nope. It’s “hostname –fqdn” or “hostname -f”

    Sorry, couldn’t resist since everyone else was nitpicking. ๐Ÿ™‚

    Thanks for the excellent article. Worked like a charm!

  6. My question is should you put the whole FQDN in the hostname file for a permanent change, or just the hostname itself?

  7. You have to edit the /etc/host file too. Not just /etc/hostname.

    -Raymond Day

  8. Just what I needed ๐Ÿ™‚
    Thanks for this great post!

  9. Thanks, that what i was looking for.

  10. also need to change /etc/mailname to the new name.

  11. The mailname serves a different purpose from that of hostname but in many
    cases their values are the same. For /etc/mailname, dpkg-reconfigure exim4-config should do it.

  12. Thanks, that is what I am looking for. good note for
    /etc/hostname & /etc/hosts

  13. By the fully qualified domain name, does that mean like or whatever you want the local hostname to be?

  14. thanks for the info.. i edit my network file, hosts file and nw when i run the hostname or hostname -f everything points to new name.. but when i telnet my server it shows the old name… wht would be the problem ?

  15. to summarize:

    sed s:$(hostname):$1:g /etc/
    mv /etc/ /etc/hosts
    hostaname $1
    hostname > /etc/hostname
    dpkg-reconfigure exim4-config

    This is a useful script to have on a virtual machine template so you can rename new VMs easily.

  16. hmm… the last command is interactive for no reason. the following is a better script:


    if [ -z "$1" ] ; then
    exit 1


    for file in /etc/hostname,/etc/hosts,/etc/mailname,/etc/exim4/update-exim4.conf.conf ; do
    sed s:${oldname}:${newname}:g ${file}.new && mv ${file}.new ${file}

    hostaname ${newname}

  17. oops, the filenames should be space separated or /etc/{hostname,hosts,mailname,exim4/update-exim4.conf.conf}. And “bin” shoudn’t be Bin.

  18. hi,

    thanks for nice and useful info

  19. Hi,
    Can two machines in the same network have same hostname with different IPs. If so, how ping is resolved.

  20. need to change DOMAIN NAME (not hostname). How can I do this?

    Mine reads: hostname..domain.tld (i added ‘two dots’ by mistake during install)

  21. thanks so very much, had the same idea! btw great site (i love the designing!)

  22. Thanks for your help!
    will it work fine on next reboot if I just change /etc/host & /etc/hostname without restarting hostname service?

  23. i tried to install iredmail on my laptop. obviously, my laptop now work as green in using unix based OS…start using it a few month ago…could someone plis help me…really need a very detail explanation….TQ guys…

  24. This is very good information. Thanks!

    @atm: I like your script. I’m just not very confident about how to go about using scripts. I believe there’s something missing in your information…I’m very newbie about this — what file to save the script — does the sed command part of the script file? or do I execute the command in the prompt….


  25. Note !

    vi or nano /etc/hostname

    perfect !

  26. We have debian server, sometime our web ( not accecable (not resolve by other DNS).
    I think there is something error in own Name Server.

    We try Check : hostname –a
    No Result (blank)

    How to correct it?

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>