This tutorial focuses on the techniques to set the date and time on Linux using timedatectl, NTP or date.
As a system administrator, you may be asked to set the system date on multiple servers.
They may be configured badly or their timezones might not be configured properly.
As a consequence, some of your programs may log invalid dates, leading to being unable to inspect some of your server logs for example.
In this tutorial, we are going to see how you can properly set the date on Linux in order to match your current geographic timezone.
First, we are going to take a look at the timedatectl command with the systemd-timesync option. Next, we are going to install either chrony or NTP services in order to synchronize our system date efficiently.
Finally, we are going to use the “date” command to statically set our system date.
We are also going to discuss the differences between those methods and which one you may want to use preferably.
Before starting, you will need to have sudo privileges on your host in order to perform some of the commands.
To make sure of it, you can run the following command
$ sudo -l User user may run the following commands on localhost: (ALL) ALL
Set date on Linux using timedatectl
Whether you are working on Debian/Ubuntu or on RHEL/CentOS, the way to set the date is the same.
To set the date, you need to set your timezone information and to (optionally) activate the NTP protocol in order for your system to synchronize with NTP servers. If you don’t use the NTP protocol (with ntpd or chrony), your system will use the timesync service by default.
If your timezone is badly configured, you will need to modify your timezone information.
In order to consult your current timezone on Linux, you can run the timedatectl command.
$ timedatectl Local time: Wed 2019-10-16 22:26:23 UTC Universal time: Wed 2019-10-16 22:26:23 UTC RTC time: Wed 2019-10-16 22:26:24 Time zone: Etc/UTC (UTC, +0000) System clock synchronized: no systemd-timesyncd.service active: inactive RTC in local TZ: no
Let’s say that you want to modify it.
To list timezones on Linux, use the “list-timezones” option with timedatectl.
$ timedatectl list-timezones
As you can see, timezones are presented using the following syntax : continent/region.
To set your timezone to New York, you can search for it easily using grep.
$ timedatectl list-timezones | grep York America/New_York
To set the timezone, use the “set-timezone” command.
$ timedatectl set-timezone America/New_York
In order to check your current system date, you have to run the timedatectl command again.
$ timedatectl Local time: Wed 2019-10-16 18:35:36 EDT Universal time: Wed 2019-10-16 22:35:36 UTC RTC time: Wed 2019-10-16 22:35:36 Time zone: America/New_York (EDT, -0400) System clock synchronized: no systemd-timesyncd.service active: inactive RTC in local TZ: no
However, your current date is not synchronized yet, you will have to activate the NTP service in order for it to synchronize.
By default, if you are working with a distribution with systemd, you should have the systemd-timesync daemon by default.
The main difference between systemd-timesync and NTP is that the daemon only implements a client-side and it is not using complex mechanisms like NTP.
To enable time synchronization, simply run this command.
$ timedatectl set-ntp true systemd-timesyncd.service active: true
If you prefer using chrony or NTP, the next sections describe how you can set NTP and chrony in order to synchronize your system date.
Set date on Linux using chrony (NTP)
The most accurate way to set the date on Linux is to use the ntp protocol.
As a quick reminder, the NTP protocol is used in order to synchronize time between different servers over a network.
You will use reference time servers that will be synchronized to your own server by computing latencies to contact reference servers.
Using the NTP protocol, you will be able to have a very precise date on Linux.
To set the date, you are going to use the chrony service.
First of all, make sure that the timesyncd service is not running on your host.
$ sudo systemctl stop systemd-timesyncd.service
Next, make sure that your packages are up to date on your server.
$ sudo yum update
Then, install the chrony package using YUM.
$ sudo yum install chrony [RHEL, CentOS] $ sudo apt-get install chrony [Debian, Ubuntu]
In most of the cases, the chrony service might be installed by default on your distribution.
To enable and start the chrony service, you have to run the following commands.
$ sudo systemctl enable chronyd $ sudo systemctl start chronyd
Verify the status of your service with the status command.
$ sudo systemctl status chronyd
Now that chrony is installed on your system, you can change your system date by adjusting the timezone of your system.
Note that you don’t have to set the activate the NTP protocol for the timedatectl command as starting chrony will automatically desactivate the systemd-timesync daemon in order to use NTP.
If you want to make sure of it, you can still run the following command.
$ timedatectl set-ntp true
Run the timedatectl command again to make sure that everything is synchronized.
$ timedatectl Local time: Wed 2019-10-17 18:37:37 EDT Universal time: Wed 2019-10-16 22:37:37 UTC RTC time: Wed 2019-10-16 22:37:33 Time zone: America/New_York (EDT, -0400) System clock synchronized: yes NTP service: active RTC in local TZ: no
Note : instead of having “NTP service”, you can still have the “systemd-timesyncd.service active” label set to yes. It does not mean that the service is active, but it means that the host is using chrony as a way to synchronize.
Also, you can run “date” to check your current system date.
$ date Thu Oct 17 13:16:10 EDT 2019
You have successfully set your date on Linux (CentOS/RHEL) using chrony.
Set date on Linux using ntpd (NTP)
As described before, using the NTP protocol is probably the most accurate way to set your date on Ubuntu and Debian.
First of all, you are going to install the ntp service on your host.
To install a NTP client, simply run the following command.
$ sudo apt-get install ntp [Debian, Ubuntu] $ sudo yum install ntp [CentOS, RHEL]
By default, running this command will install and activate a NTP (Network Time Service) service on your host.
To ensure it, get the status of the NTP service.
$ sudo systemctl status ntp
By default, running or restarting the NTP service won’t activate the NTP service for the timedatectl command.
To activate NTP synchronization, specify the “set-ntp” option.
$ timedatectl set-ntp true
Again, run a simple timedatectl command in order to make sure that everything is running properly.
$ timedatectl Local time: Wed 2019-10-17 18:37:37 EDT Universal time: Wed 2019-10-16 22:37:37 UTC RTC time: Wed 2019-10-16 22:37:33 Time zone: America/New_York (EDT, -0400) System clock synchronized: yes systemd-timesyncd.service active: yes RTC in local TZ: no
You have correctly set the date on Linux using ntp.
Set date on Linux using the date command
One of the ways to set the date on Linux is to use the date command.
One of the major flaws of this approach is the fact that you will never be as precise as setting the timezone and synchronizing it with timesyncd or chronyd.
However, it is still possible to do it this way.
First of all, desactive time synchronization on your host, otherwise your manual date change will be erased everytime by the synchronization service.
$ sudo timedatectl set-ntp false
To set the date, use “date” with the “-s” option.
$ date -s <date>
For example, using the yyyy-MM-dd format, you can set the date this way.
$ date -s '2019-10-17 12:00:00' Thu Oct 17 12:00:03 EDT 2019 $ date -s '23 October 2019 12:00:00' Wed Oct 23 12:00:00 EDT 2019
You can also use shortcuts in order to set the date easily.
If you want to modify only the year, you can use the “next year” or “last year” shortcut.
$ sudo date -s "next year" Sat Oct 17 12:02:21 EDT 2020 $ sudo date -s "last year" Thu Oct 17 12:02:43 EDT 2018
You can also choose to modify only the month, or the day with the following shortcuts.
$ sudo date -s "next day" Tue Oct 18 00:00:12 EDT 2019 $ sudo date -s "monday" Mon Oct 21 00:00:00 EDT 2019 $ sudo date -s "next hour" Wed Oct 21 13:00:19 EDT 2019
Synchronize your hardware clock with your system date
Before moving on with your daily system administration tasks, it can be a good idea to synchronize your hardware clock with your system date.
This way, you are making sure that your two clocks are not displaying different dates and hours.
To synchronize your hardware clock with your system date, use the “hwclock” command with the “–systohc” option.
$ sudo hwclock --systohc $ sudo hwclock 2019-10-23 13:03:56.466724-0400
You have successfully synchronized your hardware clock with your current system date.
In today’s tutorial, you learnt how you can easily set the date on any Linux system.
You learnt that you can set the timezone and let the systemd-timesyncd daemon perform the time synchronization.
If you are looking for more precise and efficient time synchronization techniques, you learnt that you can use either chrony or NTP in order to achieve this result.
You also saw that it is possible to set the date statically using the date command.
If you are interested in Linux system administration, we have plenty of tutorials on the subject.