Modify Server Hostname Using SSH in CentOS

Change the hostname on a running system

On any Linux system, the hostname can be modified using the command ‘hostname’ 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.

CentOS/RHEL

RedHat based systems 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="change.domainname.com"
GATEWAY="74.63.222.209"
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.

Was this answer helpful?

 Print this Article

Also Read

Install WGET on CentOS VPS or Dedicated Server

This article describes the process of installing wget on a new installation of CentOS Installing...

Removing cPanel Accounts with /scripts/removeacct Script

The /usr/local/cpanel/scripts/removeacct script terminates cPanel accounts from the command line....

How to Find the IP of cPanel Server

This tutorial is designed to troubleshoot and diagnose issues with cPanel license errors....

chmod Entire Directories from the Command Line

This article demonstrates the process of chmod file and subfolder permissions per directory,...

Recursively Delete Directory Using SSH

This article will demonstrate the process of Recursively deleting an entire directory using SSH...