54

I can't seem to change the hostname on my CentOS 6.5 host. I am following instructions I found on this (now defunct) page.

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

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

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

    [root@mig-dev-006 ~]# cat /etc/sysconfig/network
    NETWORKING=yes
    HOSTNAME=ost-dev-00.domain.example
    NTPSERVERARGS=iburst

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

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

... 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 ~]#
AdminBee
  • 21,637
  • 21
  • 47
  • 71
Red Cricket
  • 2,183
  • 6
  • 25
  • 37
  • 1
    Have you edited your `/etc/hostname` file? – Anthon Jul 17 '14 at 05: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 05: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 05:59
  • 4
    I think `/etc/hostname` is in CentOS 7 but not in CentOS 6. – Pavel Šimerda Jan 04 '15 at 09:21
  • I realize this is old, but I smell NetworkManager at work, either that or you may have HOSTNAME= set in another file than network (ie, in network-scripts). The /etc/hostname file is not used in CentOS 6 (it's a systemd thing; that means CentOS 7 and up). Check the contents of the /etc/sysconfig/network-scripts/ifcfg-eth0 file; what is in it? Note: If you look at Redhat's if_post script in network-scripts/, you will see that set_hostname simply runs "hostname $HOSTNAME", where HOSTNAME is a shell variable set either in the network file or the ifcfg-eth0 file. – Mike S Apr 30 '15 at 18:12
  • ...which means: the HOSTNAME variable is used for persistence, it must be in /etc/sysconfig/network or a file in /etc/sysconfig/network-scripts. The hostname command is used to notify the kernel what its host name is. /etc/hosts is used for name lookups. If you strace -f hostname blah, you can see that it performs a sethostname() system call, and from that point any utilities that perform gethostname() or uname() [the system call] will query the kernel who will return the requested information. – Mike S Apr 30 '15 at 18:17
  • [ekerner@au000844 ~]$ cat /etc/redhat-release /etc/hostname CentOS release 6.5 (Final) centos-6-amd64 – ekerner Jul 11 '16 at 11:46
  • The more general question is https://unix.stackexchange.com/q/12829/5132 . – JdeBP Oct 07 '20 at 02:10

7 Answers7

74

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

vi /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=newHostName

and: a good idea if you have any applications that need to resolve the IP of the hostname)

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

and then

 rebooting the system
Nidal
  • 8,856
  • 11
  • 55
  • 74
  • 7
    At the end of this two changes add `hostname newHostName`, is not necessary to reboot I think – evalarezo Sep 19 '14 at 21:01
  • @Edakos, no it is not necessary – Nidal Sep 19 '14 at 21:07
  • 4
    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 :) – Nidal Dec 13 '14 at 13:04
  • 2
    The answer has been accepted, but first, `/etc/hosts` change is there only to make the hostname resolvable, not to change it, and `/etc/sysconfig/network` looks the same as in OP. Can anyone explain what exactly OP needed to change to make it work? – Pavel Šimerda Jan 04 '15 at 09:24
  • 3
    I think a correct answer should work as an answer to the question. Repeating data from the question is good as long as the answer says the OP configured it correctly but didn't perform actions needed to load the new configuration (e.g. reboot). – Pavel Šimerda Jan 04 '15 at 12:46
  • 2
    Reboot not necessary. Check strace -f hostname blah . The kernel gets the message right away. /etc/hosts does not affect the host's hostname, in CentOS 6.5 (and 7) at least. And this answer is incorrect in that it's missing the hostname command which is what informs the kernel of its host's name. – Mike S Apr 30 '15 at 18:56
  • As noted above, editing `/etc/hosts` is not necessary, but it's a good idea if you have any applications that need to resolve the IP of the hostname. – Aaron Blenkush Dec 04 '15 at 19:00
  • https://support.rackspace.com/how-to/centos-hostname-change/ is probably the best answer to this question. You don't need to reboot. – Andrew Feb 29 '16 at 21:53
20

You can change your hostname with a single command.

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

Example

$ hostname mynewhost

For persisting the new hostname across reboot

edit /etc/sysconfig/network file to look like below

NETWORKING=yes
HOSTNAME=myhostanme

For video help

Vishwanath gowda k
  • 594
  • 1
  • 5
  • 11
6

Apart from the answers already provided for CentOS 6.5,

CentOS 7 also has a text based user interface tool to change hostname (and other network settings), part of Network Manager:

nmtui

starts this user interface:

enter image description here

which has an option to Set system hostname.

The text user interface of NetworkManager is installed by default in CentOS-7-x86_64-DVD-1511.iso image, base install. If yours doesn't have it, you can install it with the following command:

yum install NetworkManager-tui
quasoft
  • 169
  • 1
  • 3
4

No reboot is required for changing hostname on CentOS 6.5.

The following command informs the kernel of the new hostname :

# hostname new_hostname

The following command so that the hostname be persistent across reboot :

# sed -i "s/^HOSTNAME=.*/HOSTNAME=new_hostname/g" /etc/sysconfig/network
Pozinux
  • 1,305
  • 5
  • 16
  • 27
2

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")
target.truncate()
target.write("NETWORKING=yes\n")
target.write("HOSTNAME=")
target.write(newHostName)
target.write("\n")
target.close()

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

print "Set new hostname to %r" % newHostName
os.system('/bin/hostname ' + newHostName)
Alex Begun
  • 37
  • 1
  • 2
    Hmm that's an ugly script. I wonder if Puppet would be a better tool for dealing with hostname. – Red Cricket Jan 04 '15 at 07:32
  • 2
    http://docs.ansible.com/hostname_module.html – jgritty Jan 15 '15 at 06:31
  • 4
    This script is about 3 times as long as it needs to be. Python is the wrong tool for the job. A bash script would be much shorter, more readable, and more maintainable. – Mike S Apr 30 '15 at 18:59
1

If you are on the Centos 7.x. Insert the following command.

nmtui

It will have an option of set system hostname. Press Enter on it.

Write your hostname.

Click Ok.

Now, check with command hostname.

It will have your saved hostname.

Hiren Parghi
  • 117
  • 4
0

Unfortunately I was not able to change the hostname by making entries in:

/etc/sysconfig/network

and

/etc/hosts file

However I was able to change the hostname by editing the file below:

vi /etc/hostname
server01

Restart and test hostname by:

hostname
Noam M
  • 441
  • 1
  • 7
  • 17
  • 3
    Note the comment on the OP stating that `/etc/hostname` exists in CentOS 7, but not CentOS 6 – Fox Mar 17 '17 at 22:46