Jan 04, 2023
How to Set the Timezone in CentOS 7
While you are setting up a new server, very often you might have to set the timezone. In certain cases, you might want to change your timezone.
Setting the appropriate timezone on servers is something many of us forget. Setting an inappropriate timezone will impact your server reporting and logs. That’s why in this tutorial, we’ll show you how to set a timezone in CentOS 7.
Setting the correct time zone for your CentOS based VPS is important for accurately logging timestamps, else you may end up noting a wrong date and timestamp for the incident or error log.
Apart from logging and reporting, another very important factor is the time set for crontabs and automated jobs. Setting an inappropriate time zone may end up running the processes at a different than actually intended time. Luckily if you want to set the timezone in CentOS 7, you’ll only need to use a few simple commands!
Overall, not setting the proper time zone won’t break your operation, but will definitely improve your workflow and will easily prevent a lack of clarity in your logs.
Let’s learn how to set the timezone in CentOS 7.
Important! CentOS Linux 8 has reached it’s End of Lifetime (EOL) on 2021-12-31. While CentOS Linux 7 is still supported, it will reach EOL on 2024-06-30. We recommend keeping that in mind when choosing this OS. You can read more about it in their official website.
How to Check the Set Timezone in CentOS 7
First of all, we need to access our virtual private server using SSH. If you’re having trouble, reference the PuTTY tutorial!
You can check your current time zone by using the date command as shown below:
This will output the time in the following format:
Fri Mar 15 14:52:20 UTC 2019
Alternatively, you can use the timedatectl command. This command can:
- Review the dates and timestamps
- Change the date and time
- Setup a timezone for the system
- Enable automatic synchronization of the clock based on a remote server
The basic command looks like this:
The output will be as shown below:
Local time: Fri 2019-03-15 14:54:51 UTC Universal time: Fri 2019-03-15 14:54:51 UTC RTC time: n/a Time zone: Host (UTC, +0000) NTP enabled: n/a NTP synchronized: yes RTC in local TZ: no DST active: n/a
This contains information such as local time, universal time, and timezone.
How to List Timezones in CentOS 7
Once you have viewed your current timezone, you might want to list all the available timezone options. The below command can be used to list all the timezone options:
You’ll get a long list showing all the timezones in alphabetical order. You can refine your search by adding filters using grep patterns. One such example is as shown below:
timedatectl list-timezones | grep Pacific
How to Set Timezone in CentOS 7
Once you have the list of timezones, you can set the one you prefer. To set the timezone in CentOS 7, you can use:
timedatectl set-timezone America/Chicago
You can replace the time zone accordingly in the command above. Over here we can also set a specific time and date. This can be done using the below format:
timedatectl set-time 'YYYY-MM-DD HH:MM:SS'
For instance, if you want this to be set the time as Mar-16-2019 and the current time as 09:20:00, then you can use:
timedatectl set-time '2019-03-16 09:20:00'
After making these changes, you can again check the time zone details using timedatectl.
How to Set the Hardware Clock in CentOS 7
A hardware clock is different from the system clock which is managed by the Linux kernel. This is also referred to as the BIOS clock. The hardware clock is enabled once the system is shut down. Using Linux commands, you can set the hardware clock.
To check if the hardware clock is set to local time zone use the below command:
timedatectl | grep local
This will give output as shown below, which means the hardware clock does not have a timezone specified:
RTC in local TZ: no
Next, you can set your hardware clock to local time zone using the below command:
timedatectl set-local-rtc 1
If you’re seeing the error “Failed to set local RTC: Failed to set RTC to local/UTC: Input/output error” when trying to run this command, try editing the file /etc/adjtime/ by adjusting the third line. You can read more about this error over at Stack Overflow.
To revert the changes, you can use:
timedatectl set-local-rtc 0
How to Perform Clock Synchronization on CentOS 7
Network Time Protocol (NTP) is an internet protocol which is commonly used to synchronize system clocks on different computers. The timedatectl command can configure a system’s time zone to automatically sync with a remote server using NTP.
To enable this, first, we should have NTP installed on the system. If you don’t have this installed on your CentOS device yet, you can do so by installing Chrony:
yum install chrony
In order to start synchronization with a remote NTP server use the below command:
timedatectl set-ntp true
In case you want to revert these changes, use the below command. This will disable time synchronization:
timedatectl set-ntp false
Here we have seen how to set timezone on CentOS 7 as well as how to set system time, date, hardware clock time and synchronize systems using NTP. Setting the right timezone is important for correct logging, automated processes and more!
It’s easy to set the timezone on CentOS 7. And yes, to learn about more options, you can always check the manual pages for the timedatectl command.