How to change the hostname of a Linux system
Normally we will set the hostname of a system during the installation process. Many peoples don’t care about this, and don’t change the hostname even if for example this was set to something really stupid by the datacenter that installed the system (most likely they will set this to “debian” on any debian installation, etc). For me, it is important to see on each one of the ssh screens I will have open at any time a different hostname that is relevant and will give me quickly the information on what system I am logged in.
Change the hostname on a running system
On any Linux system you can change its hostname with the command ‘hostname‘ (surprised?)… Here are some quick usages of the command line 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 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
/etc/hostname
server
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 start
to 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).
Permanent hostname change on RedHat based systems
RedHat based system use the file /etc/sysconfig/network to read the saved hostname at system boot. This is set using the init script /etc/rc.d/rc.sysinit
/etc/sysconfig/network
NETWORKING=yes
HOSTNAME="plain.domainname.com"
GATEWAY="192.168.0.1"
GATEWAYDEV="eth0"
FORWARD_IPV4="yes"
So in order to preserve your change on system reboot edit this file and enter the appropriate name using the HOSTNAME variable.
Use sysctl to change the hostname
Why would someone need a different method of doing the same thing as above? No idea, but here is anyway: 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.
Share This








9th August 2006, 21:41
Ubuntu’s Mark Shuttleworth on this TV channel (british, I think). First time I’ve heard him talk.
10th August 2006, 10:32
The problem is that hostname might be present in several files under /etc depending on the installed packages. So, you’ll have to change all the files, and restart/reload corresponding running daemons.
Therefore, try to choose original hostname. This way relevant entries will not be shaded by tons of false matches while grepping through /etc for files containing the hostname string.
12th August 2006, 19:07
Some additional Debian/Ubuntu related remarks that some people might find useful:
* You can’t really set the domain name part of the FQDN since it’s the DNS server that gives it to you. You can trick your system into thinking it’s a particular value by adding an entry for your own machine in /etc/hosts.
* If you’ve got a dynamic IP (DHCP — unlikely on a server), you can change the hostname that the DHCP client sends back to the server (e.g. for dynamic DNS) in /etc/dhcp3/dhclient.conf. Include a –send host-name “yourhostname”;– line.
13th February 2007, 10:45
Hi ,
Iam able to login the application URL through ipaddress but Iam not able to login through Server Name. I mean
//172.23.43.44:7777/
But i want to see through //servername:7777
Let me know how can I do this
13th February 2007, 10:46
On Red hat Linux
13th February 2007, 21:02
vamshi,
This is probably a DNS related issue. Check your nameserver to have the respective hostname defined.
HTH.
14th February 2007, 17:02
This post ranks quite highly on Google for the search terms “ubuntu change hostname”, so I’d like to add some advice to Ubuntu users: if you follow the steps above, you’ll find /etc/hosts still populated with your old hostname, and because of the same conflict, you won’t be able to use “sudo” to gain permission to modify it! The easiest way to gain root access is to restart your computer and select recovery mode from the GRUB menu. Then you’ll be able to do a simple “nano /etc/hosts” and modify it to reflect your new host name.
You can also use “hostname newhost”, which will modify the computer host name according to the value of newhost.
31st March 2007, 05:57
Check your /etc/hosts file also!
18th April 2007, 12:15
If you are using sendmail edit /etc/mail/local-host-names file to include the new host-name. This will prevent sendmail from hanging up when the system is restarted.
22nd August 2007, 16:09
Before you reboot your system, I would suggest the following:
sudo bash ; # starts a root shell
# next print a list of files with the current hostname in them
# (you may want to redirect this into a file for future reference)
grep -r -l $( hostname) .
# now edit that same set of filenames
vi $( grep -r -l $(hostname) . )
11th September 2007, 10:25
on my system (ubuntu) there’s also /etc/mailname and some settings in /etc/postfix/main.cf
but I’m still seeing one header in outgoing emails that show the old domain…
11th September 2007, 11:11
Peter: check /etc/hosts and change there any occurrence for the main system IP with the proper new hostname. Restart postfix and it should work now.
22nd April 2008, 05:50
[...] (http://www.ducea.com/2006/08/07/how-to-change-the-hostname-of-a-linux-system/) [...]
30th May 2008, 04:19
I think that we should ad an IN A to the hostname and modify the DNS zone as well.
Changing only the hostname will result in some truble
18th June 2008, 06:58
Hi,
I am using RHEL-4 on IBM Blade Server. But when I change IP add. hostname (localhost) automatically changed. Please give me the solution.
18th June 2008, 07:14
Ravi: how are you changing your IP? Please describe clearly what commands you run and what is the output, in order for other peoples to be able to help you…
29th June 2008, 20:27
[...] 1. http://www.ducea.com/2006/08/07/how-to-change-the-hostname-of-a-linux-system/ [...]