Linux - Changing the system time and hostname
Linux Systems offer an X Windows interface which can make changing system settings and configuration pretty straight forward, but what if you remotely log in via a Secure Shell (SSH) session using a tool such as putty?
Here is how to change the system date and time and the hostname from the command line.
Changing The Date & Time in Linux
The date command can be used by root to change the system date and time.
Displaying the Current Date and Time
The simple use of date is to display the current date and time:
Changing the Current Date and Time
The date command can take an argument to enable the date and time to be changed, but only if logged in as root:
Changing The Hostname in Linux
Changing the hostname is achieved in slightly different ways depending upon whether you are using CentOS/Redhat 7 (or equivalent) or CentOS/Redhat 6 (or equivalent) or earlier.
CentOS & RedHat 6
The hostname command can be used to display and change the system hostname.
Displaying the Hostname
Changing the Hostname
To change the hostname pass the new hostname as an argument to the hostname command:
Although this has changed the hostname it has only been changed in memory and if the server were restarted the hostname would revert back to the value configured in the file /etc/sysconfig/network.
To make the change permanent you will need to edit the /etc/sysconfig/network file and set the value for the HOSTNAME variable to the new hostname:
Now the hostname change will be permanent.
CentOS & RedHat 7
The hostnamectl command was introduced in CentOS and Redhat version 7.
The hostnamectl command can be used for changing the system hostname or displaying the hostname.
Displaying The Hostname Status
The hostnamectl command has a status argument that displays quite a bit of information about the Linux server, not just the hostname:
We can see from the above screen shot that the kernel version, server architecture and virtualisation hosting environment (where relevant) is also displayed by this command.
Changing The System Hostname
The hostnamectl command has a set-hostname argument that enables you to change the hostname:
This change is permanent and the new hostname will be set on subsequent reboots.
The changed value is written to the file /etc/hostname by hostnamectl.