David Ghedini

Linux, Java, Oracle, and PostgreSQL


David Ghedini

Wednesday Mar 02, 2011

How to Set Hostname on CentOS

This post will cover how to set the server hostname on CentOS.

Well, I could have sworn when I ordered this VPS that I entered the hostname I wanted in the order form. I guess I must have been hallucinating....

Begin by checking the existing hostname using the hostname command.

login as: root
root@vps12's password:
Last login: Mon Jun 28 16:21:53 2010
[root@vps12 ~]# hostname
vps12


We can also check via sysctl kernel.hostname

[root@vps12 ~]# sysctl kernel.hostname
kernel.hostname = vps12


The actual hostname I need to set for this server is blanche.mydomain.net

I can set the hostname initially using the hostname command followed by the desired hostname.

[root@vps12 ~]# hostname blanche.mydomain.net


This will set the hostname:

[root@vps12 ~]# hostname
blanche.mydomain.net


However, once the server is rebooted, the hostname will revert to it's previous setting (vps12).

To permanently change the hostname, I need to edit the network file at /etc/sysconfig/network.

So,

[root@vps12 ~]# cd /etc/sysconfig
[root@vps12 sysconfig]# vi network


The Network file will look as so.

NETWORKING=yes
HOSTNAME=vps12
GATEWAYDEV=eth0
GATEWAY=192.168.0.1


Hit 'i' to insert and change the HOSTNAME value to your FQDN, in my case, blanche.mydomain.net

Hit Escape, then :wq to save and exit.

Check that the hostname is properly set:

[root@vps12 sysconfig]# hostname
blanche.mydomain.net
[root@vps12 sysconfig]# sysctl kernel.hostname
kernel.hostname = blanche.mydomain.net


Finally, if we log out, and log back in using a new session, we see I am now root@blanche:

login as: root
root@blanche's password:
Last login: Mon Jun 28 16:21:53 2010
[root@blanche ~]#  


To properly check, you may wish to reboot the server.

Bookmark and Share



Comments:

nice , perfectly all r8..

Posted by siva on March 04, 2013 at 06:51 AM CST #

Thanks for that hop this fixes my cpanel issue re hostnames.

Posted by Steven on February 04, 2014 at 09:56 AM CST #

I tried this on CentOS7, hostname is lost after reboot. Needs to be set in /etc/hostname

Posted by Gordon Hickey on November 25, 2014 at 06:05 AM CST #

Post a Comment:
  • HTML Syntax: Allowed

Main Menu

Built With

Search

Pages

LinkedIn

Tag Cloud

Hosted by:

Navigation

Visitors

Sponsors

petiole Cloud

Feeds