Two ways to modify the system time in Linux

One: How to update the system time

1. Manual modification

Use related tools to manually modify the system time.

2. Automatic synchronization

Use NTP to automatically synchronize system time.

Two: Manually modify the system time

1. date tool

Role: display and set system time
options:

-d  <字符串> 显示字符串所指的日期与时间,比如:"-1 day" 表示当前日期的前一天,必须要加双引号

-s  <字符串> 设置当前的时间和日期 #年月日使用(-)分隔,时分秒使用(:)分隔

Time format:

# 显示时间格式: date [+指定的格式]

# 相关格式说明:
    %T:time,显示时分秒,24小时制(hh:mm:ss)

    %F:full date,显示年月日,%Y-%m-%d

# 说明:
    Y: 年
    m:月
    d:日

    H:时
    M:分
    S:秒

For example: display the year, month, day, hour, minute, and second

# 格式:
	date "+%F %T" # 需要用双引号括起来 表示这是一个整体

root@ubuntu1804:~# date 
Tue 28 Mar 2023 11:07:08 AM CST

root@ubuntu1804:~# date +"%F"
2023-03-28

root@ubuntu1804:~# date +"%T"
11:07:18

root@ubuntu1804:~# date +"%F %T"
2023-03-28 11:07:29

Only modify the year, month, and day:

date -s "20220710" 或  date -s "YYYY-MM-DD"  # 时间设置成2022年7月10日0时0分

Only modify the hours, minutes and seconds:

date -s "hh:mm:ss"

Change all:

date -s "YYYY-YY-DD HH:mm:ss"

Note:
The time modified by using the date command takes effect temporarily and becomes invalid after restarting the system. It needs to synchronize the current time with the hardware time to take effect permanently.

2. hwclock tool

Function: Realize the proofreading of time, and proofread the hardware time and system time. After manually modifying the time using date, you need to use hwclock to check it.

options:

-s, --hctosys 	# 以硬件时钟为准,校正系统时钟

-w, --systohc 	# 以系统时钟为准,校正硬件时钟

Description: clock is a soft link of hwclock

# centos存在clock和hwclock
    [root@Centos8 ~]# ll `which hwclock`
    -rwxr-xr-x. 1 root root 66928 Jul 22  2020 /usr/sbin/hwclock
    [root@Centos8 ~]# ll `which clock`
    lrwxrwxrwx. 1 root root 7 Jul 22  2020 /usr/sbin/clock -> hwclock

# clock是hwclock的一个软链接,ubuntu上面只有hwclock
    root@ubuntu1804:~# which clock
    root@ubuntu1804:~# which hwclock 
    /sbin/hwclock

Example: Synchronize system time to hardware time

# 同步的时候没有任何提示
root@ubuntu1804:~# hwclock -w  
root@ubuntu1804:~# 

Example: Synchronize hardware time to system time

# # 同步的时候没有任何提示
root@ubuntu1804:~# hwclock -s

3. timedatectl tool

Function: The timedatectl tool can be used to change the time zone or to modify the system time, and is generally used to change the time zone.

View a list of time zones:

# 格式  timedatectl list-timezones 

# 例如:
    root@ubuntu1804:~# timedatectl list-timezones 
    Africa/Abidjan
    Africa/Accra
    .......

Modify time zone:

#   格式
	timedatectl set-timezone 时区
	
# 例如:修改时区      修改时区无任何提示
	root@ubuntu1804:~# timedatectl set-timezone Asia/Shanghai

Modified datetime:

timedatectl set-time "2017-01-23 10:30:00" # YYYY-mm-dd HH-MM-SS

Enable NTP:

timedatectl set-ntp true/false

Modify the time zone by soft link:

# 存放时区信息       /etc/timezone
	root@ubuntu1804:~# cat /etc/timezone
	Asia/Shanghai

# 修改时区影响的文件  /etc/localtime

# 例如
	root@ubuntu2004:~# rm -f /etc/localtime  #更改时区最终收到影响的文件:/etc/localtime
    root@ubuntu2004:~# ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

root@ubuntu1804:~# ll  /etc/localtime
lrwxrwxrwx 1 root root 35 Mar 28 11:20 /etc/localtime -> ../usr/share/zoneinfo/Asia/Shanghai

Three: Use NTP to automatically synchronize time

NTP (Network Time Protocol) protocol, Network Time Protocol. The computer time synchronization in the network can be realized by using the ntp protocol.

1. ntpdate tool

Function: Use the time of the time synchronization server to calibrate the time of the machine, and use the time synchronization server on the Internet to connect to the Internet

Format:

ntpdate server_host 

Example: Use ntpdate to synchronize time

root@ubuntu1804:~# apt install ntpdate

root@ubuntu1804:~# date 
Tue Dec 13 15:12:07 CST 2022

root@ubuntu1804:~# date -d '-1 day'
Mon Dec 12 15:12:22 CST 2022

root@ubuntu1804:~# ntpdate ntp.aliyun.com
13 Dec 15:12:36 ntpdate[15040]: adjust time server 203.107.6.88 offset 0.002588 sec

root@ubuntu1804:~# date 
Tue Dec 13 15:12:40 CST 2022  #同步后的时间

2. ntp tool

Function: Use the time of the time synchronization server to calibrate the time of the machine, and can synchronize the time permanently instead of one time.
Description: The ntp tool is a tool for both the client and the server, and it is used as the client by default.

ntp as a client: can be used to calibrate the local time to the time of other ntp servers

ntp as server: used to build ntp time server, and other hosts synchronize local time

NTP client tools:

  • ntpq tool: It is a command-line tool of ntp, which is used to query and control the NTP server, and obtain information synchronized with the server time.
# 查看 NTP 服务器状态:显示当前计算机与所有已配置 NTP 服务器之间的时间同步状态
ntpq -p

# 查看 NTP 服务器系统状态:显示每个 NTP 服务器的系统状态信息
ntpq -c sysstat

# 查看计算机与 NTP 服务器之间的同步状态:示计算机与 NTP 服务器之间的同步状态,包括时间差和延迟等信息
ntpq -c loopstats

3. chrony tool

Function: Use the time of the time synchronization server to calibrate the time of the machine, and can synchronize the time permanently instead of one time. The accuracy and speed of synchronization are faster than ntp.

port:

  • Port 123 of udp on the server side, if chrony enables the client function, it will monitor port 123\udp.
  • The client listens to port 323 of udp, and the function is the same as above.

chrony as a client: can be used to calibrate the local time to the time of other ntp servers

Chrony as a server: used to build an ntp time server, and other hosts to synchronize the local time

Chrony's client tools:

  • chronyc-tools: A command-line tool for managing and monitoring NTP (Network Time Protocol) clients
# 检查 NTP 服务器的状态:显示当前的时间误差,NTP 服务器的状态,NTP 版本号等信息。
chronyc tracking

# 查看可用的 NTP 服务器列表:查看本地计算机与 NTP 服务器之间的连接状态
chronyc sources

# 调整时钟频率:立即调整系统时钟,使其与 NTP 服务器同步
chronyc makestep

# 显示当前的活动:显示 NTP 守护进程的当前状态,例如它是否正在向其他 NTP 服务器发送请求或接收响应。
chronyc activity

Four: NTP configuration

By building a host as a time server, hosts in other LANs can synchronize with the local time.

1. ntp tool

As client:

Just specify the address of the ntp server in the configuration file

root@ubuntu1804:~# vim /etc/ntp.conf
pool ntp.aliyun.com iburst # iburst表示加速同步

As a server:
Use the local time as the standard to synchronize the time with other hosts.

# 以本机时间为准,允许所有用户进行同步
root@ubuntu1804:~# vim /etc/ntp.conf
server  127.127.1.0
fudge   127.127.1.0 stratum 10
restrict default

restrict:

  • restrict: Used to limit which hosts or networks can communicate with the time server. The default is restrict default to allow all hosts to synchronize
  • Configuring the restrict parameter can help protect the time server from unwanted traffic and security attacks.
# 格式  restrict [IP] [mask] [netmask_IP] [patameter]
# 参数 
	default  # 默认参数,表示没有限制。这意味着任何客户端都可以访问该服务器并进行时间同步
	ignore   # 表示忽略所有的 NTP 请求,这意味着客户端无法与服务器进行时间同步。
    nomodify # 表示客户端无法修改服务器的时间设置
    noquery  # 表示客户端无法查询服务器的状态信息,例如版本号、偏差等。
    limited  # 表示限制客户端的访问速率,防止客户端过于频繁地请求时间同步。
	ntpport  #  指定允许访问服务器的 NTP 端口范围

# 例如:
#  允许所有客户端访问服务器 (默认)
	restrict default 

# 限制只有同一个局域网内的主机可以同步时间
	restrict 192.168.0.0 mask 255.255.255.0 nomodify notrap

# 限制来自192.168.1.0/24子网范围内的主机只能通过1234端口访问NTP服务器,而不允许使用其他端口访问。
restrict 192.168.1.0 mask 255.255.255.0 ntpport 1234”

# 限制来自 192.168.1.0/24 网段的所有主机,禁止其查询服务器,并且不信任该网段的主机。
restrict 192.168.1.0 mask 255.255.255.0 noquery notrust


# 局域网里面搭建ntp就使用这个进行限制。
    # 允许192.168.0.0网段访问时间服务器,但是不能修改时间服务器的一些配置。
        # 	modify 表示禁止客户端修改服务器的时间设置,notrap 表示禁止客户端使用 trap 操作。
restrict 192.168.0.0 mask 255.255.255.0 nomodify notrap

2. chrony tool

As a client:
just use server or pool to specify the address of the ntp server.

root@ubuntu1804:~# vim /etc/chrony/chrony.conf
server ntp.aliyun.com

As a server:
the local time is used as the standard, and the time of other hosts is synchronized with the local time.

Because chrony is used as a client by default, that is, port 123 of the server is not opened by default, and it needs to be specified in the configuration file to switch to server mode.

# 修改改配置文件
root@ubuntu1804:~# vim /etc/chrony/chrony.conf
	server 127.127.1.0             
	fudge 127.127.1.0 stratum 10   
    local stratum 10  # 使用本地硬件时钟作为时间源,并将同步级别设置为 10,表示该服务器的时间较准确
    allow 0.0.0.0/0   # 指定允许同步的网段  表示的是任何主机都可以和本机进行时间的同步
 
# fudge这行是时间服务器的层次。设为0则为顶级,如果要向别的NTP服务器更新时间,请不要把它设为0

illustrate:

  • server: used to specify a single NTP server
  • pool: used to specify a pool of NTP servers instead of a single NTP server
  • iburst: Make time synchronization faster, open multiple parallel tasks for synchronization
  • prefer: Indicates that this is preferred if multiple time servers are specified
server 192.168.7.49 prefer iburst   # prefer表示如果指定了多个时间服务器的情况下优先用这个 iburst:表示加速时间同步

Five: concrete realization

1、 ntp

Use ntp to synchronize the time on the ntp server

# 安装ntp服务
root@ubuntu1804:~# apt install ntp

# 只需要在配置文件中加入这个就行
root@ubuntu1804:~# vim /etc/ntp.conf 
	pool ntp.aliyun.com

# 查看 NTP 服务器状态
root@ubuntu1804:~# ntpq  -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
 ntp.aliyun.com  .POOL.          16 p    -   64    0    0.000    0.000   0.000

# 查看同步后的时间
root@ubuntu1804:~# date 
Wed Mar 29 14:08:54 CST 2023

Use ntp to build a local time server

It takes 5-10 minutes to wait for the installation

# 修改客户端时间用于测试
root@ubuntu1804:~# date -s "20220329"
Tue Mar 29 00:00:00 CST 2022

# 1. 修改服务端配置文件
root@ubuntu1804:~# vim /etc/ntp.conf
pool ntp.aliyun.com iburst
server 127.0.0.1 iburst
fudge  127.127.1.0 stratum 10
restrict source 10.0.0.0 mask 255.255.255.0  nomodify

# 2. 查询和显示NTP服务器的状态
root@ubuntu1804:/var/log/ntpstats# ntpstat
synchronised to local net at stratum 11 
   time correct to within 199 ms
   polling server every 64 s
   
# 查看同步信息 没带星号说明还没开始同步
root@ubuntu1804:/var/log/ntpstats# ntpq  -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
 LOCAL(0)        .LOCL.          10 l   11   64   37    0.000    0.000   0.000

# 带星号说明已经开始同步
root@ubuntu1804:/var/log/ntpstats# ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
*LOCAL(0)        .LOCL.          10 l   54   64  377    0.000    0.000   0.000


# 3. 客户端时间同步
root@ubuntu1804:~# ntpdate  10.0.0.22
15 Oct 00:19:27 ntpdate[3631]: no server suitable for synchronization found

# 服务端配置好后,需要等5-10分钟左右才能同步成功,否则就会出现上面的提示
root@ubuntu1804:~# ntpdate  10.0.0.22
29 Mar 17:42:59 ntpdate[3632]: step time server 10.0.0.22 offset 14318478.924123 sec

# 查看更新后的时间
root@ubuntu1804:~# date 
Wed Mar 29 16:15:44 CST 2023

2. chrony tool

Use chrony to synchronize the time on the ntp server

# 将本机时间修改为一个错误时间测试
root@ubuntu1804:~# date 
Tue Mar 29 00:00:55 CST 2022

# 安装服务
root@ubuntu1804:~# apt install chrony

# 修改配置文件
root@ubuntu1804:~# vim /etc/chrony/chrony.conf 
pool ntp.aliyun.com iburst

# 重启服务
root@ubuntu1804:~# systemctl restart chronyd.service 

# 查看本地计算机与NTP服务器之间的连接状态
root@ubuntu1804:~# chronyc sources
210 Number of sources = 1
MS Name/IP address         Stratum Poll Reach LastRx Last sample               
===============================================================================
^? 203.107.6.88                  2   6     1    31   -8775h[ -8775h] +/-   31ms

# 查看有多少个ntp源在线
root@ubuntu1804:~# chronyc activity 
200 OK
1 sources online
0 sources offline
0 sources doing burst (return to online)
0 sources doing burst (return to offline)
0 sources with unknown address

root@ubuntu1804:~# date 
Wed Mar 29 14:38:41 CST 2023

Use chrony to build a local time server

# 客户端时间设置
root@ubuntu1804:~# date  -s "20220228"
Mon Feb 28 00:00:00 CST 2022

# 服务器配置
root@ubuntu1804:~# vim /etc/chrony/chrony.conf 
server 127.0.0.1 iburst
# fudge 127.127.1.0 stratum 10
local stratum 10
allow 0.0.0.0/0

# 重启服务
root@ubuntu1804:~# systemctl restart chronyd.service 

# 查看有多少个ntp源在线
root@ubuntu1804:~# chronyc activity 
200 OK
0 sources online
0 sources offline
1 sources doing burst (return to online)
0 sources doing burst (return to offline)
0 sources with unknown address

# 显示本机时间和远程服务器同步的情况
root@ubuntu1804:~# chronyc sources
210 Number of sources = 1
MS Name/IP address         Stratum Poll Reach LastRx Last sample               
===============================================================================
^* localhost                    10   6   177     9    -38ns[ +535ns] +/- 1362ns

# 客户端同步时间
root@ubuntu1804:~# ntpdate 10.0.0.22
29 Mar 14:58:27 ntpdate[3607]: adjust time server 10.0.0.22 offset -0.020780 sec
root@ubuntu1804:~# date 
Wed Mar 29 14:58:29 CST 2023

(51 messages) NTP server configuration_ntp configuration_Resines Blog-CSDN Blog

Six: Windows builds and uses NTP server

1. Windows builds NTP server

(1) Modify the registry to start the NTP server

快捷键win + R 打开“运⾏”菜单,输⼊regedit打开Windows注册表
修改注册表配置,设定授权时间服务器
“HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W32Time\Config”
[AnnounceFlags]修改设定值为5

Modify the registry configuration to start the ntp server:

“HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W32Time\TimeProviders\NtpServer”
[Enabled]数值修改为1

(2) Restart the WindowsTime service

# 管理员模式下cmd终端执⾏以下命令:
net stop w32time
net start w32time

2. Windows uses NTP server

(1) Set the WindowsTime service to start automatically

打开Windows系统服务(Win + R然后输⼊services.msc再回⻋),
找到Windows Time系统服务,右击更改属性,将其 "启动类型" 改为 "⾃动"。

(2) Specify the address of the ntp server

⿏标右击电脑左下⻆的时间,打开:调整⽇期/时间(A)
选择添加不同时区的时钟

Then select Internet time, click: Change Settings, check the upper box, select the IP address configured in the previous step for the server, and click Update Now.

Guess you like

Origin blog.csdn.net/dyuan134/article/details/130201683