Leaderboard ad

4.15.2015

Setting Time and Date in Red Hat 7 / CentOS 7

Question sent in by Wayne from Oregon.

Q: I recently installed my first CentOS 7 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.

Now that Red Hat 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.

1) Setting the current time and date using the timedatectl command.

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.

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
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.

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.

2) Setting the current time and date using the date command.

We still have the option to use the old date command to set or show the current time and date.

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.%'

To set the current date use the --set option:

date --set HH:MM:SS

For example to set the clock to 1:14 PM:

date --set 13:14:00

To set the current date you also 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

You can set the current date and current time together like so:

date --set 2015-4-15 13:15:00


6 comments:

  1. Thanks for this - helpful tip regarding the newer tools available in Centos 7

    ReplyDelete
  2. I had to run the "service ntpd restart" command in order to get NTP to set my time/date correctly

    ReplyDelete
  3. Synchronise ntp: yum install ntp;ntpq -c lpeer
    Verify: timedatectl

    ReplyDelete
  4. Many Thanks for this compact tutorial!!

    ReplyDelete