Setting Time and Date (Red Hat/CentOS)

To totally unlock this section you need to Log-in


Login

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.

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.

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:

[[email protected] ~]# 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 command:

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

Category