Change Server hostname/Timezone/Date on RHEL|CentOS

Posted on 195 views

This guide will walk you through the steps to set or change a Server’s hostname/Timezone/Date on RHEL|CentOS 8/7 system. Having the right time and server hostname is an important aspect of Linux server operations. Most applications will reference the system local time when writing logs and reporting the event’s time.

The server hostname is often used for generating SSL certificates, local dns resolution, and for configuring applications. Note that setting up persistent server hostname and timezone on RHEL 8 / CentOS 8 is different from doing it on RHEL 6, but identical to RHEL 7. RHEL 8/7 uses systemd init system while RHEL 6 uses Upstart.

How To Set Server Hostname on RHEL/CentOS 8|7

RHEL/CentOS 8|7 ships with the hostnamectl tool for administering server host names. To view all the current host names, use the following command:

$ hostnamectl status
    Static hostname: rhel8.local
          Icon name: computer-vm
            Chassis: vm
         Machine ID: 992462394b1c4f2e80e7e2fd978f04f8
            Boot ID: e10a0c6a44ff4a15b7731df1bd45d6cf
     Virtualization: kvm
   Operating System: Red Hat Enterprise Linux 8.0 Beta (Ootpa)
        CPE OS Name: cpe:/o:redhat:enterprise_linux:8.0:beta
             Kernel: Linux 4.18.0-32.el8.x86_64
       Architecture: x86-64

Set hostname on RHEL/CentOS 8|7

To set all the host names on your system, the following command is used:

sudo hostnamectl set-hostname 

See example below:

sudo hostnamectl set-hostname rhel8.example.com

This will alter static, and transient host names alike. When setting hostname using hostnamectl command, spaces are be replaced with “-” and special characters removed.

Confirm changes

$ hostnamectl 
    Static hostname: rhel8.example.com
          Icon name: computer-vm
            Chassis: vm
         Machine ID: 992462394b1c4f2e80e7e2fd978f04f8
            Boot ID: e10a0c6a44ff4a15b7731df1bd45d6cf
     Virtualization: kvm
   Operating System: Red Hat Enterprise Linux 8.0 Beta (Ootpa)
        CPE OS Name: cpe:/o:redhat:enterprise_linux:8.0:beta
             Kernel: Linux 4.18.0-32.el8.x86_64
       Architecture: x86-64

Set Pretty host name

You can also set Pretty host name with quotation marks.

$ sudo hostnamectl set-hostname "Josphat's notebook" --pretty
$ hostnamectl 
    Static hostname: rhel8.example.com
    Pretty hostname: Josphat's notebook
          Icon name: computer-vm
            Chassis: vm
         Machine ID: 992462394b1c4f2e80e7e2fd978f04f8
            Boot ID: e10a0c6a44ff4a15b7731df1bd45d6cf
     Virtualization: kvm
   Operating System: Red Hat Enterprise Linux 8.0 Beta (Ootpa)
        CPE OS Name: cpe:/o:redhat:enterprise_linux:8.0:beta
             Kernel: Linux 4.18.0-32.el8.x86_64
       Architecture: x86-64

Clear system host name

Clear a particular system host name and revert to the default using:

sudo hostnamectl set-hostname ""

How To configure Timezone on RHEL/CentOS 8|7

Systemd comes with timedatectl command used to control the system time and date.

Display the Current Date and Time

To show current settings of the system clock and RTC, run:

$ timedatectl status
                Local time: Tue 2019-02-12 18:24:05 EAT
            Universal time: Tue 2019-02-12 15:24:05 UTC
                  RTC time: Tue 2019-02-12 15:22:22
                 Time zone: Africa/Nairobi (EAT, +0300)
 System clock synchronized: yes
               NTP service: active
           RTC in local TZ: no

Changing the Current Time – both system time and the hardware clock

To set the current time on your RHEL 8, use the command syntax:

sudo timedatectl set-time HH:MM:SS

Example below sets current time to 6:28 p.m.

sudo timedatectl set-time 18:28:00

Replace HH with an hour, MM with a minute, and SS with a second, all typed in two-digit form. If you have an active NTP service, this should fail with the error “Failed to set time: NTP unit is active“.

By default, the system is configured to use UTC. You can force it to use the local time by running:

sudo timedatectl set-local-rtc true

Changing the Current Date

The current date can be changes by running the command syntax.

sudo timedatectl set-time "yyyy-MM-dd hh:mm:ss"

Replace YYYY with a four-digit year, MM with a two-digit month, and DD with a two-digit day of the month. If you don’t specify the current time, it results in setting the time to 00:00:00. So set the date with time.

Example:

sudo timedatectl set-time "2019-04-29 11:08:00"

Changing the Time Zone

You may need to first list all available time zones.

timedatectl list-timezones

Then set your correct timezone using the command below.

sudo timedatectl set-timezone time_zone

While replacing time_zone with any of the values listed by the list command.

$ timedatectl list-timezones | grep Nairobi
Africa/Nairobi

$ sudo timedatectl set-timezone Africa/Nairobi

This will set the time zone to Africa/Nairobi

Synchronizing the System Clock with a Remote Server (Recommended)

As opposed to the manual adjustments, you can enable automatic synchronization of your system clock with a group of remote servers using the NTP protocol.

We have an article on How to Configure NTP Server Using Chrony on RHEL 8

To enable NTP service using a command as follows:

sudo timedatectl set-ntp yes

Cheers!. You have reached the end of setting up host names and timezone onRHEL/CentOS 8|7. Subscribe to our newsletter to receive fresh updates directly in your inbox.

coffee

Gravatar Image
A systems engineer with excellent skills in systems administration, cloud computing, systems deployment, virtualization, containers, and a certified ethical hacker.