Setting the date and time in CentOS
Introduction
When working on the network, time and date synchronization is an important
factor that increases stability and
safety
the work of all systems.
In this article, you will learn how to set the date and time manually. And, what's more
the important thing is how to synchronize them with the ntp server.
These are articles about the date and time in CentOS 7.
About the date and time in Rocky Linux and CentOS 8
read
here
Find out the current system time
To find out the current system time, you can use the command date
date
2024-03-29 00:03:57.364911+02:00
Find out the current BIOS time
To find out the current BIOS system time, you can use the command hwclock
sudo hwclock
2024-03-29 00:03:57.389911+02:00
date и hwclock they can show completely different times
Fix 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, then 01 is written); |
hh | two digits indicating the hour |
mm | two digits denoting minutes |
CCYY | four digits of the year |
ss | two digits of seconds |
You can also enter «date MMDDhhmmCCYY» without seconds, then seconds are set to zero.
After executing date, you need to enter a new set time in the BIOS:
hwclock --systohc
Now it remains only to check whether the changes have been saved
sudo hwclock
Synchronization with ntp
Check if ntp is installed with the command
which ntpd
/usr/sbin/ntpd
If ntp is not installed execute
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) execute
systemctl start ntpd
systemctl enable ntpd
To check whether the ntp service has been added to the public zone in the firewall, you can use the command
sudo firewall-cmd --get-services | grep ntp
If not already added, then to allow UDP traffic from ntp execute
firewall-cmd --permanent --add-service=ntp
firewall-cmd --reload
success
success
Edit the file ntp.conf with server settings
vi /etc/ntp.conf
Find your time zone on the site
www.ntppool.org
I found the zone
Helsinki
and I 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 you need to restart ntpd
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 according to the time you just received with the command
hwclock --systohc
Force the system to synchronize time with ntp
You can use the following command
ntpdate -u 0.fi.pool.ntp.org
Instead of 0.fi.pool.ntp.org install the server you need
Timezone
Find out the current time zone
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
You can change the time zone with the set-timezone command
timedatectl set-timezone "Europe/Helsinki"
Check the result
timedatectl | grep "Time zone"
Time zone: Europe/Helsinki (EEST, +0300)