Q: I recently installed my first CentOS 7 Linux system and am having a hard time setting the clock correctly. I seem to be setting it correctly, but when I come back to the system a day or two later the time is not correct. It is usually a few minutes to an hour off.
A: If you clock is drifting that much in a short time it might be a symptom of another problem. But, maybe I can help with your initial question. Since the move to systemd setting the time and date is a little different on CentOS 7, Red Hat 7, or newer Fedora systems. Here we will show you the new timedatectl followed by the old trusty date command.
Using the timedatectl command.
Now that Red Hat Linux has moved to systemd, we have a new way to set the clock. the timedatectl command is very easy to use and an efficient way to set the time and date on your system.
NOTE: You must run the following command with root level permissions by either changing to root, or using sudo.
Displaying Current Time and Date using timedatectl
To display the current time and date settings just run the timedatectl command like so:
timedatectl
Example output:
Local time: Wed 2015-04-15 11:28:10 EDT
Universal time: Wed 2015-04-15 15:28:10 UTC
RTC time: Wed 2015-04-15 15:28:10
Time zone: America/New_York (EDT, -0400)
NTP enabled: yes
NTP synchronized: yes
RTC in local TZ: no
DST active: yes
Last DST change: DST began at
Sun 2015-03-08 01:59:59 EST
Sun 2015-03-08 03:00:00 EDT
Next DST change: DST ends (the clock jumps one hour backwards) at
Sun 2015-11-01 01:59:59 EDT
Sun 2015-11-01 01:00:00 EST
To set the current time, use the timedatectl command with the set-time option:
timedatectl set-time 22:31:14
To set the current date, use the timedatectl command with the set-time option using the date format (YEAR-MONTH-DAY) followed by the current time:
timedatectl set-time "2015-04-15 22:37:22"
NOTE: If you do not include the current time when changing the date you clock will be set to 00:00:00.
Changing the Timezone with timedatectl
To change the timezone, you can use the timedatectl command with set-timezone option followed by the desired timezone:
timedatectl set-timezone America/New_York
For a list of acceptable time zones, use the list-timezones option:
timedatectl list-timezones
If you know the name of your timezone you can use grep to easily find the exact syntax:
timedatectl list-timezones | grep -i york
America/New_York
Or to list all the timezones in the Americas:
timedatectl list-timezones | grep America
Enable NTP with timedatectl command
You can also enable or disable automatic clock synchronization using the remote NTP servers. I prefer this over setting the clock manually for many reasons. Enabling this should help keep your clock from drifting as described in the original question. To enable NTP clock syncronization, use set-ntp option with “yes” to enable or “no” to disable.
Read our tutorial on learning how to Configure and NTP server in Red Hat.
timedatectl set-ntp yes
NOTE: Setting the set-ntp boolean to yes will enable chronyd or ntpd service, depending on which is installed on your system.
Using the date Command
We still have the option to use the old linux date command to set or show the current time and date.
Displaying Current Time and Date with the date command
To display the current time and date simply type date at the command line like so:
[root@putor ~]# date
Wed Apr 15 13:02:19 EDT 2015
You can also use a custom format to display the time. This comes in very handy, especially when writing scripts. You can use control sequences to easily display the time or date in many formats or display only parts of the date (for example current month).
To display the current year in YYYY format:
date +"%Y"
To display the current month in MM format:
date +"%m"
To display the current date in MM-DD-YYYY format:
date +"%m-%d-%Y"
To display just the current hour:
date +"%H"
To display a list of control sequences you can use the following command or consult the man pages for date:
date --help | grep '^s.%'
Setting Current Time and Date with the date Command
To set the current date use the --set option:
date --set YYYY-MM-DD
For example to set the current date to April 15, 2015:
date --set 2015-4-15
To set the current time:
date --set HH:MM:SS
For example to set the clock to 1:14 PM:
date --set 13:14:00
To set both the time and date together:
date --set 2015-4-15 13:15:00
Conclusion
The timedatctl command and date command have very similar functions. Both are capable of displaying and setting the current time and date. The timedatectl command has the added bonus of being able to sync via NTP.
If I missed anything let me know in the comments.