In this lesson we’re going to discuss time maintenance on Linux.
Now when you initially setup a Linux system you’re prompted to configure your system time along with your time zone.
When you do this you’re probably not overly concerned about these settings because they don’t really seem all that critical to the functioning of the system.
However, understand that when you’re working with Linux it’s really important that you maintain the correct system time especially if the system is going to provide network services.
For example, let’s suppose you’re implementing a file server well in this situation you need to ensure that the timestamps on each of the files are accurate.
Or maybe the server system is going to be providing some type of directory service or maybe it’s a database server.
In these situations, maintaining the correct system time is absolutely critical.
For example, let’s suppose you’re running a database server then you need to make sure that each change that’s submitted to the database server from client systems has an accurate timestamp.
Imagine what kind of problems would arise if database modifications get applied out of sequence because the timestamps are inaccurate.
The integrity of your data would be toast.
So, in this lesson we’re going to look at setting the hardware clock with the
hwclock command and then we’re going to talk about synchronizing time with the
How to set Hardware clock in Linux
Let’s begin by discussing how to manage time with
Types of clocks in Linux
Now remember there are two-time sources that are used on a Linux system.
The first one is the Hardware Clock which is integrated into your systems motherboard also known as RTC, CMOS or BIOS clock and it runs all the time even when the system is powered off or even when it’s unplugged for that matter.
We also have the System Time, now the system time runs via software inside the Linux kernel itself.
Therefore, it only runs while the system is up and running.
Also remember the system time is measured not as the current Date and Time but instead as the number of seconds to develop stints midnight January 1st, 1970 UTC.
It’s important that you remember that the hardware clock and the system time may not be the same.
Therefore, when we are managing a Linux system, we are actually most concerned about the system time then the time that’s currently being reported by the Hardware clock.
Role of the Hardware clock
Really the role of the Hardware Clock is simple, its job is to keep time while the system’s Powered OFF.
When the system is powered on what will happen is that the Linux operating system will go out and grab the time from the hardware clock and use that to initially set the System Time.
Once that’s done Linux only cares about the system time it could care less about what time the Hardware Clock has.
The only time the Hardware Clock comes back into play is if you reboot the system at which point you know the kernel will grab the Hardware Clock time again to initially set the system time.
So, to manage the Hardware Clock on your system you use the
Features of hwclock Command
It can do a lot of different things as noted here:
- Display the current time.
- Set the hardware clock time.
- Synchronize the hardware clock to the system time.
- Synchronize the system time to the hardware clock.
Just a quick note, In addition to using the
hwclock command you can also view the current time of the hardware clock not the system time but the hardware clock time by simply displaying the
RTC stands for Real Time Clock.
# cat /proc/driver/rtc rtc_time : 10:51:46 rtc_date : 2021-05-25 alrm_time : 18:20:07 alrm_date : 2021-05-21 alarm_IRQ : no alrm_pending : no update IRQ enabled : no periodic IRQ enabled : no periodic IRQ frequency : 1024 max user IRQ frequency : 64 24hr : yes periodic_IRQ : no update_IRQ : no HPET_emulated : yes BCD : yes DST_enable : no periodic_freq : 1024 batt_status : okay
Now there are several different options you can use with the
hwclock command, these are available on most Linux distributions.
-r– Read the current time from the hardware clock just displays it on the screen.
--set --date="time"– Set the hardware clock time.
Now be aware that whatever time you specify right here will be set in local time and this is true even if you’ve got your hardware clock set to UTC the kernel will take care of calculating the appropriate offset.
--hctosys– Set the system time from the RTC
--systohc– Set the RTC from the system time
--utc– The RTC timescale is UTC
--localtime– The RTC timescale is Local
Synchronize time between Linux systems on Network
Now be aware that the scope of the
hwclock commands functionality is limited to just your local computer system.
Now that’s fine and dandy but if your computer system is operating on a network then it’s very likely that you’re going to need the system time on all of your Linux systems to be synchronized.
You don’t want one system having a radically different time than other systems because as we talked about earlier that screws up timestamps.
Now one tool that you can use to synchronize time between Linux systems is the
You can use the
netdate command to synchronize the time on the local system with the time on a time server somewhere on the network.
The syntax is shown here. Where we enter
netdate followed by the IP address of the time server and you can also use the DNS name as well.
netdate [options] [protocol] hostname...
Process of time synchronization in Linux.
The key thing is that the system that you specify has to be running the time service.
So, in this example I have a Time Provider over here.
It’s a Server system the time daemon has been enabled on this server and it is running and its synchronizing time over UDP Port 37.
That’s the default port, you can of course change it.
What the time server is going to do is listen on Port 37 for time synchronization requests.
So, let’s suppose we run the
netdate command on the Time Consumer (Workstation), It will send the time request from the
netdate command over to the Time Provider.
The time service on this Server (Time Provider) running on Port 37 receives the request and then sends the appropriate time back to the Time Consumer.
At which point the time on Time Consumer is set to whatever time it received from the server system (Time Provider).
So as a result, the time on these two systems is really close within a few milliseconds of each other which for all intents and purposes we consider to be synchronized.
Now it’s important that you remember that when you are synchronizing time with the
netdate command you are synchronizing System time not the Hardware clock time.
Therefore if you want to make sure that your system time comes up reasonably close to the synchronized time the next time you reboot the system not a bad idea to run the
hwclock command with the
-w parameter to synchronize the hardware clock on your system to the system time.
That way the next time you boot your system time should be fairly close to the time on the time provider because remember the Linux kernel will read the initial time for the system from the hardware clock which should be fairly close to the time provided by the time provider.
Now there’s one more thing that I want you to remember here with regards to system time.
Remember that we said the system time does not run in Hardware it runs as Software.
Now what this means is after you synchronize time with the
netdate command eventually the site time on Time Consumer will drift away from the time on Time Provider.
Therefore, it’s not a bad idea to run the
netdate command on a regular basis using a cronjob.
That way we keep the time synchronized regularly between these two systems.
I hope that now you have a good understanding of How to Maintain and Synchronize time on Linux using hwclock and netdate Commands.
If anyone does have any questions about what we covered in this guide then feel free to ask in the comment section below and I will do my best to answer those.
Here are a few other hand-picked guides for you to read next: