This article provides a guide to modify server Hostname using SSH in CentOS server.
You can use the steps below to change the hostname of a running system and update the server configuration with the new hostname details.
What is a Hostname?
In computer networking, a "hostname" is a label that is assigned to a device connected to a computer network and that is used to identify the device.
How to Modify the Hostname
On any Linux system, the hostname can be checked and modified using the ‘hostname
’ command.
Here are some quick uses of the hostname
command:
hostname
Without any parameters, it will output the current hostname of the system.
hostname --fqd
With the --fqd
argument, hostname
will output the fully qualified domain name (or FQDN) of the system.
hostname NEW_NAME
When followed by normal text string (such as "NEW_NAME" above), hostname
command 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
You can use sysctl
to change the variable kernel.hostname
on CentOS server.
To utilize this method, use the following procedure.
First, to check the current hostname of the server, login via SSH and run the following command:
sysctl kernel.hostname
Now, to change the hostname of the server, login via SSH and run the following command, replacing "NEW_HOSTNAME" with the new server hostname:
sysctl kernel.hostname=NEW_HOSTNAME
The hostname will now be changed to the value entered for "NEW_HOSTNAME". To verify, you can run the hostname
command discussed in the beginning of the tutorial:
hostname NEW_HOSTNAME
You now know how to modify server hostname from SSH terminal in CentOS server.