Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Hi can't seem to change the hostname on my centos 6.5 host. I am following the instructions I found here: http://www.rackspace.com/knowledge_center/article/centos-hostname-change

I set my /etc/hosts like so ...

    [root@mig-dev-006 ~]# cat /etc/hosts   localhost localhost.localdomain  ost-dev-00.domain.com ost-dev-00  ost-dev-01.domain.com ost-dev-01

... then I make my /etc/sysconfig/network file like so ...

    [root@mig-dev-006 ~]# cat /etc/sysconfig/network

... then I run hostname like so ...

    [root@mig-dev-006 ~]# hostname ost-dev-00.domain.com

... and then I run bash and all seems well ...

    [root@mig-dev-006 ~]# bash

... but when I restart my network the old hostname comes back:

    [root@ost-dev-00 ~]# /etc/init.d/network restart
    Shutting down interface eth0:  Device state: 3 (disconnected)
                                                               [  OK  ]
    Shutting down loopback interface:                          [  OK  ]
    Bringing up loopback interface:                            [  OK  ]
    Bringing up interface eth0:  Active connection state: activating
    Active connection path: /org/freedesktop/NetworkManager/ActiveConnection/6
    state: activated
    Connection activated
                                                               [  OK  ]
    [root@ost-dev-00 ~]# bash
    [root@mig-dev-006 ~]# 

... I can't figure it out. What am I doing wrong here.

share|improve this question
Have you edited your /etc/hostname file? –  Anthon Jul 17 '14 at 5:54
There isn't a /etc/hostname file. [root@smp-mig-dev-006 ~]# ls -l /etc/hostname outputs ls: cannot access /etc/hostname: No such file or directory –  Red Cricket Jul 17 '14 at 5:56
Might be a Centos thing. What does your manpage tell you about the permanency of hostname somename, that it has a lasting effect? (if not why do you make that call?) –  Anthon Jul 17 '14 at 5:59
I think /etc/hostname is in CentOS 7 but not in CentOS 6. –  Pavel Šimerda Jan 4 at 9:21

5 Answers 5

up vote 18 down vote accepted

to change the hostname permanently, you need to change it in two places:

vi /etc/sysconfig/network


vi /etc/hosts newHostName   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

and then rebooting the system

share|improve this answer
At the end of this two changes add hostname newHostName, is not necessary to reboot I think –  Edakos Sep 19 '14 at 21:01
@Edakos, no it is not necessary –  Networker Sep 19 '14 at 21:07
Editing the /etc/hosts file does not seem to be required –  jgritty Dec 12 '14 at 21:09
@jgritty, I think it is necessary , but to know for sure you should try it your self :) –  Networker Dec 13 '14 at 13:04
That's exactly how I arrived at my conclusion :) –  jgritty Dec 13 '14 at 17:43

A solution for centOS7 can be found here:

change hostname centOS7

hostnamectl set-hostname <new hostname>
share|improve this answer
I'm liking centos7! –  Red Cricket Feb 3 at 7:02

I had to do this for a bunch of machines. Here is a simple python script to help you out. Simply provide the name of the new host as the first argument to the script.

for example: if you name the script changeHost.py then run it as

changeHost.py [NewHostName]

where NewHostName is your desired host name.

Also make sure to run this script as root.

#!/usr/bin/env python
import os

from sys import argv
script, newHostName = argv

print "Modifying network file..."
target = open("/etc/sysconfig/network","w")

print "Modifying hosts file..."
target = open("/etc/hosts","w")
target.write(" ")
target.write(" localhost.localdomain localhosts\n")

print "Set new hostname to %r" % newHostName
os.system('/bin/hostname ' + newHostName)
share|improve this answer
Hmm that's an ugly script. I wonder if Puppet would be a better tool for dealing with hostname. –  Red Cricket Jan 4 at 7:32
Whoa! What's ansible? Sounds awesome. –  Red Cricket Feb 3 at 7:04

If changing /etc/sysconfig/network and /etc/hosts does not change your screen name, do the following.

When you login, you see your screen name:

[john@oldhostname ~]

See what the current hostname is: oldhostname.olddomain.com. Then change the hostname to newhostname.newdomain.com. Then logout and login.


This returns: oldhostname.olddomain.com

hostname newhostname.newdomain.com

You won't see this reflected in your bash terminal. You have to logout and login again.

[john@newhostname ~]
share|improve this answer
This won't make a permanent change at all. –  Pavel Šimerda Jan 4 at 9:25
Thank you for your reply :) –  Red Cricket Feb 3 at 7:05

You can change your hostname with a single command.

  • Command: hostname - this displays old hostname
  • Command: hostname <new name> -this sets new hostname


$ hostname mynewhost

For video help

share|improve this answer
Thank you for your reply :) –  Red Cricket Feb 3 at 7:04

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.