Change hostname or Server name of a Linux Machine
Posted by Admin on September 17th, 2006
If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!
First, see if your host name is set correclty using the following commands:
#uname -n
#hostname -a
#hostname -s
#hostname -d
#hostname -f
#hostname
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‘.
#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/hostname.sh
#cat /etc/hostname
debianadmin
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/hostname.sh 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 hostname.sh).
If you want more details check hostname man page
Use sysctl to change the hostname
use sysctl to change the variable kernel.hostname:
Use:
#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
also.


August 13th, 2007 at 10:31 am
Shouldn’t that be:
hostname --fqdn ?
(or hostname -f).
January 21st, 2008 at 5:08 pm
Thanks… very useful information.
June 13th, 2008 at 2:25 pm
hostname -fqd didn’t work…
I think it should jsut be hostname -f ?
I may be wrong though..
But thanks
JAM
July 7th, 2008 at 7:26 pm
I think that should be hostname --fqdn
July 13th, 2008 at 7:54 pm
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!
July 27th, 2008 at 2:54 pm
My question is should you put the whole FQDN in the hostname file for a permanent change, or just the hostname itself?
August 7th, 2008 at 10:25 am
You have to edit the /etc/host file too. Not just /etc/hostname.
-Raymond Day
September 29th, 2008 at 5:09 pm
Just what I needed
Thanks for this great post!
November 6th, 2008 at 5:58 pm
Thanks, that what i was looking for.
December 20th, 2008 at 8:01 am
also need to change /etc/mailname to the new name.
December 21st, 2008 at 6:40 pm
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.
February 27th, 2009 at 1:26 am
Thanks, that is what I am looking for. good note for
/etc/hostname & /etc/hosts
March 21st, 2009 at 2:03 am
By the fully qualified domain name, does that mean like somehting.com or whatever you want the local hostname to be?
June 24th, 2009 at 12:12 am
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 ?
November 25th, 2009 at 10:19 am
to summarize:
#!/bin/sh
sed s:$(hostname):$1:g /etc/hosts.new
mv /etc/hosts.new /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.
November 25th, 2009 at 10:39 am
hmm… the last command is interactive for no reason. the following is a better script:
#!/bin/sh
if [ -z "$1" ] ; then
exit 1
fi
newname=$1
oldname=$(hostname)
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}
done
hostaname ${newname}
November 25th, 2009 at 10:42 am
oops, the filenames should be space separated or
/etc/{hostname,hosts,mailname,exim4/update-exim4.conf.conf}. And “bin” shoudn’t be Bin.December 29th, 2009 at 12:28 pm
hi,
thanks for nice and useful info
February 3rd, 2010 at 6:06 pm
Hi,
Can two machines in the same network have same hostname with different IPs. If so, how ping is resolved.