Setting the date and time in Linux
Introduction
When working on a network, time and date synchronisation is an important
factor that increases the stability and
security
of all systems.
In this article you will learn how to set the date and time manually. And, more
importantly, how to synchronise them with an ntp server.
Find the current system time
The current system time can be found with the command date
date
2020-12-27 11:25:17.364911+02:00
Find the current BIOS time
The current BIOS time can be found with the command hwclock
sudo hwclock
2020-12-27 11:28:17.389911+02:00
date and hwclock can show completely different times
Correct the current time
First you need to set the time with the command date
date MMDDhhmmCCYY.ss
| MM | two digits of the current month (example: January=01); |
| DD | two digits of the current date (if the current calendar day is 1, write 01); |
| hh | two digits for the hour |
| mm | two digits for the minutes |
| CCYY | four digits of the year |
| ss | two digits of the seconds |
You can also enter «date MMDDhhmmCCYY» without seconds, then the seconds are set to zero.
After running date you need to write the new time into the BIOS:
hwclock --systohc
Now all that remains is to check that the changes have been saved
sudo hwclock
Synchronisation with ntp
Check whether ntp is installed with the command
which ntpd
/usr/sbin/ntpd
If ntp is not installed, run
sudo yum install ntp
Check whether ntpd starts automatically with the command
systemctl list-unit-files | grep ntpd.s
ntpd.service enabled
If ntpd is inactive (disabled), run
systemctl start ntpd
systemctl enable ntpd
You can check whether the ntp service has been added to the public zone in the firewall with the command
sudo firewall-cmd --get-services | grep ntp
If it has not been added yet, then to allow UDP traffic from ntp run
firewall-cmd --permanent --add-service=ntp
firewall-cmd --reload
success
success
Edit the file ntp.conf with the server settings
vi /etc/ntp.conf
Find your time zone on the site
www.ntppool.org
I found the zone
Helsinki
and add the lines
server 0.fi.pool.ntp.org
server 1.fi.pool.ntp.org
server 2.fi.pool.ntp.org
server 3.fi.pool.ntp.org
Now ntpd has to be restarted
systemctl restart ntpd
You can check where the time comes from with the command
ntpq -p
remote refid st t when poll reach delay offset jitter ============================================================================== time.cloudflare 10.79.8.177 3 u 14 64 7 3.987 -0.366 0.016 37.228.129.2 193.66.253.94 2 u 16 64 7 5.301 -1.639 1.908 ivanova.ganneff 237.17.204.95 2 u 13 64 7 5.296 3.850 0.266 ntp23.kashra-se 192.168.100.15 2 u 12 64 5 32.935 -2.213 0.192
You can set the BIOS time from the time just obtained with the command
hwclock --systohc
Make the system synchronise time with ntp
You can do it with the following command
ntpdate -u 0.fi.pool.ntp.org
Replace 0.fi.pool.ntp.org with the server you need
Article author: Andrei Olegovich