Set date and time Rocky
Introduction
Syncing date and time is an important factor increasing stability and
security
of all systems.
In this article you can learn how to configure date and time manually. And what is more important - how to sync them with ntp server.
Get current system time
You can get current system time by running date
date
2024-04-20 00:00:01.364911+02:00
Get current BIOS date and time
You can get current BIOS date and time by running hwclock
sudo hwclock
2024-04-20 01:33:46.389911+02:00
date и hwclock can show absolutely different time
Change current date
You can set date by running date
date MMDDhhmmCCYY.ss
MM | month number (example: Jan=01); |
DD | date number (e.g. if it is the first day of month DD is 01); |
hh | hour |
mm | minutes |
CCYY | year |
ss | seconds |
If you use «date MMDDhhmmCCYY» without seconds, ss is set to 00.
After setting date this new time should be enetered to BIOS:
hwclock --systohc
To check if changes are saved run
sudo hwclock
Sync with ntp
chrony is used as ntp server and client in Rocky
which chronyd
/usr/sbin/chronyd
If chrony is not installed run
sudo yum install -y chrony
To check if chronyd is starting after boot execute
systemctl list-unit-files | grep chronyd.s
chronyd.service disabled
If chronyd is disabled execute
systemctl start chronyd
systemctl enable chronyd
To check if ntp service is added to Firewall public zone execute
sudo firewall-cmd --get-services | grep ntp
If it is not allowed yet you can allow UDP traffic for ntp by executing
firewall-cmd --permanent --add-service=ntp
firewall-cmd --reload
success
success
Edit chrony.conf file with server configurations
vi /etc/chrony.conf
Search for your timezone at
www.ntppool.org
I have found
Helsinki
and added
server 0.fi.pool.ntp.org
server 1.fi.pool.ntp.org
server 2.fi.pool.ntp.org
server 3.fi.pool.ntp.org
Restart ntpd
systemctl restart ntpd
Set BIOS time to system time
hwclock --systohc
Force Rocky to sycn time with ntp server
You can use the following command
chronyc makestep
To allow everything that can be allowed use
sudo sed -i 's/#allow/allow/' /etc/chrony.conf
Check chronyd status
systemctl status chronyd
● chronyd.service - NTP client/server Loaded: loaded (/usr/lib/systemd/system/chronyd.service; disabled; vendor preset: enabled) Active: inactive (dead) Docs: man:chronyd(8) man:chrony.conf(5)
Start chronyd
systemctl start chronyd
● chronyd.service - NTP client/server Loaded: loaded (/usr/lib/systemd/system/chronyd.service; disabled; vendor preset: enabled) Active: active (running) since Wed 2021-09-01 19:39:43 EEST; 19s ago Docs: man:chronyd(8) man:chrony.conf(5) Process: 2878 ExecStartPost=/usr/libexec/chrony-helper update-daemon (code=exited, status=0/SUCCESS) Process: 2874 ExecStart=/usr/sbin/chronyd $OPTIONS (code=exited, status=0/SUCCESS) Main PID: 2876 (chronyd) Tasks: 1 (limit: 23548) Memory: 788.0K CGroup: /system.slice/chronyd.service └─2876 /usr/sbin/chronyd Apr 20 01:33:46 localhost.localdomain systemd[1]: Starting NTP client/server... Apr 20 01:33:46 localhost.localdomain chronyd[2876]: chronyd version 3.5 starting (+CMDMON +NTP +REFCLOCK +RTC +PRIVDROP +SCFILTER +SIGND +ASYNCDNS +SECHASH +IPV6 +DEBUG) Apr 20 01:33:46 localhost.localdomain chronyd[2876]: Using right/UTC timezone to obtain leap second data Apr 20 01:33:46 localhost.localdomain systemd[1]: Started NTP client/server. Apr 20 01:33:46 localhost.localdomain chronyd[2876]: Selected source 162.159.200.123 Apr 20 01:33:46 localhost.localdomain chronyd[2876]: System clock TAI offset set to 37 seconds
Stop chronyd
systemctl stop chronyd
Restart chronyd
systemctl restart chronyd
tracking: check sync
To check if sync is working run
chronyc tracking
Reference ID : 4E2412B8 (78-36-18-184.dynamic.murmansk.dslavangard.ru) Stratum : 2 Ref time (UTC) : Sat Apr 20 01:33:46 2024 System time : 0.000049305 seconds fast of NTP time Last offset : -0.000010323 seconds RMS offset : 0.000790649 seconds Frequency : 4.357 ppm fast Residual freq : +0.154 ppm Skew : 1.831 ppm Root delay : 0.034635838 seconds Root dispersion : 0.000497150 seconds Update interval : 64.6 seconds Leap status : Normal
Comments: Reference ID – the reference ID and name to which the computer is currently synced. Stratum – number of hops to a computer with an attached reference clock. Ref time – this is the UTC time at which the last measurement from the reference source was made. System time – delay of system clock from synchronized server. Last offset – estimated offset of the last clock update. RMS offset – long term average of the offset value. Frequency – this is the rate by which the system’s clock would be wrong if chronyd is not correcting it. It is provided in ppm (parts per million). Residual freq – residual frequency indicated the difference between the measurements from reference source and the frequency currently being used. Skew – estimated error bound of the frequency. Root delay – total of the network path delays to the stratum computer, from which the computer is being synced. Leap status – this is the leap status which can have one of the following values – normal, insert second, delete second or not synchronized. To check information about chrony’s sources, you can issue the following command.
Configure chronyd
sudo vi /etc/chrony.conf
# Use public servers from the pool.ntp.org project. # Please consider joining the pool (http://www.pool.ntp.org/join.html). pool 2.pool.ntp.org iburst # Record the rate at which the system clock gains/losses time. driftfile /var/lib/chrony/drift # Allow the system clock to be stepped in the first three updates # if its offset is larger than 1 second. makestep 1.0 3 # Enable kernel synchronization of the real-time clock (RTC). rtcsync # Enable hardware timestamping on all interfaces that support it. #hwtimestamp * # Increase the minimum number of selectable sources required to adjust # the system clock. #minsources 2 # Allow NTP client access from local network. #allow 192.168.0.0/16 # Serve time even if not synchronized to a time source. #local stratum 10 # Specify file containing keys for NTP authentication. keyfile /etc/chrony.keys # Get TAI-UTC offset and leap seconds from the system tz database. leapsectz right/UTC # Specify directory for log files. logdir /var/log/chrony # Select which information is logged. #log measurements statistics tracking
Timezone
Get current timezone
timedatectl
Local time: Fri 2021-05-21 01:42:44 MDT Universal time: Fri 2021-05-21 07:42:44 UTC RTC time: Fri 2021-05-21 07:42:43 Time zone: America/Denver (MDT, -0600) NTP enabled: yes NTP synchronized: yes RTC in local TZ: no DST active: yes Last DST change: DST began at Sun 2021-03-14 01:59:59 MST Sun 2021-03-14 03:00:00 MDT Next DST change: DST ends (the clock jumps one hour backwards) at Sun 2021-11-07 01:59:59 MDT Sun 2021-11-07 01:00:00 MST
Change timezone with set-timezone command
timedatectl set-timezone "Europe/Helsinki"
Check the result
timedatectl | grep "Time zone"
Time zone: Europe/Helsinki (EEST, +0300)