How to Change Time Zone in CentOS 7
Setting the correct time zone on your CentOS server is essential for accurate logging, reporting, and automated processes like cron jobs. This guide explains how to review, set, and manage the time zone in CentOS 7 step-by-step.
Access Your Server and CentOS via SSH
To begin, connect to your server via SSH. Use the following command:
ssh user@your_server_ip
Replace user
with your username and your_server_ip
with your server’s IP address.
Step 1: Check Current Time Zone
You can check your server’s current time and time zone using the date
or timedatectl
commands.
date
Example output:
Fri Mar 15 14:52:20 UTC 2019
Alternatively, use timedatectl
:
timedatectl
Example output:
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: UTC (UTC, +0000) NTP enabled: yes NTP synchronized: yes DST active: n/a
Step 2: List Available Time Zones
To list all available time zones:
timedatectl list-timezones
This will display a comprehensive list of supported time zones in alphabetical order. Use grep
to refine your search. For instance, to search for Pacific time zones:
timedatectl list-timezones | grep Pacific
Step 3: Set the Time Zone
Once you’ve identified the desired time zone, use the following command to set it:
timedatectl set-timezone <Time_Zone>
Replace <Time_Zone>
with the desired time zone, such as America/Chicago
or Asia/Dubai
.
Example:
timedatectl set-timezone America/Chicago
After setting, verify the new time zone with:
timedatectl
Step 4: Set the System Date and Time (Optional)
To manually set the date and time, use the following format:
timedatectl set-time 'YYYY-MM-DD HH:MM:SS'
Example:
timedatectl set-time '2019-03-16 09:20:00'
Verify the updated date and time with:
date
Step 5: Configure the Hardware Clock
The hardware clock (RTC) operates even when the system is powered off. To check if it’s using the local time zone:
timedatectl | grep local
Output:
RTC in local TZ: no
To set the hardware clock to the local time zone:
timedatectl set-local-rtc 1
To revert this setting:
timedatectl set-local-rtc 0
Step 6: Enable NTP Synchronization
Network Time Protocol (NTP) synchronizes system time with remote servers. If not already installed, first install NTP:
yum install ntp -y
To enable NTP synchronization:
timedatectl set-ntp true
To disable it:
timedatectl set-ntp false
Verify NTP synchronization status:
timedatectl
Best Practices
- Double-check the impact of time zone changes on server applications, especially cron jobs and logs.
- Use NTP synchronization to maintain accurate system time.
- Document the changes for future reference or audits.
Following these steps ensures your server’s time zone is configured correctly for smooth operations.
conclusion
correctly setting the time zone on your CentOS 7 server is essential for ensuring accurate logging, smooth operation of automated tasks like cron jobs, and effective server management. By following the outlined steps—from checking the current time zone to enabling NTP synchronization—you can ensure that your server maintains the correct time, which will ultimately enhance system reliability and prevent potential issues. Always verify your changes and consider any impact they may have on applications running on your server. Utilizing NTP synchronization is a recommended best practice to keep your server’s time accurate.