본문 바로가기
프로그래밍/OS

Windows NTP Server - Linux NTP Client 동기화

by 남생 namsaeng 2022. 4. 4.
반응형

[구축 환경]

 

  • Windows NTP Sever : Windows 10
  • Linux NTP Client : Red Hat Enterprise Linux 8.2

 

 

1. Windows NTP Server

 

  • Windows NTP Server를 만드는 방법은 아래의 과정을 거친다.

 

1) NTP Server 기능 ON

2) 방화벽 인바운드 설정(UDP 123 포트 연결 허용)

3) w32time 서비스 자동화

 

아래의 배치 스크립트는 위의 절차를 한 번에 처리하기 위한 일련의 명령들로, (. bat) 파일로 만들었다.

 

  • WindowsNtpServer.bat
@echo off
net start w32time
reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W32Time\TimeProviders\NtpServer" /v "Enabled" /t REG_DWORD /d 1 /f
reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W32Time\Config" /v "AnnounceFlags" /t REG_DWORD /d 5 /f
netsh advfirewall firewall add rule name="MyNTPServer" protocol=UDP dir=in localport=123 action=allow enable=yes
net stop w32time
net start w32time

 

 

 

2. Linux NTP Client

 

1) 리눅스 시간 형식 변경

  • 리눅스는 기본으로 UTC(Universal Time, Coordinated), 윈도우는 RTC(Real Time Clock)을 이용하여 시간을 계산한다.
  • 리눅스 시간 설정 상에서 [RTC in local TZ]를 “no”에서 “yes”로 바꿔준다.
[root@localhost ~]# timedatectl
               Local time: 금 2022-04-01 15:47:23 KST
           Universal time: 금 2022-04-01 06:47:23 UTC
                 RTC time: 금 2022-04-01 06:47:23
                Time zone: Asia/Seoul (KST, +0900)
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: no

[root@localhost ~]# timedatectl set-local-rtc 1

[root@localhost ~]# timedatectl
               Local time: 금 2022-04-01 15:49:13 KST
           Universal time: 금 2022-04-01 06:49:13 UTC
                 RTC time: 금 2022-04-01 15:49:13
                Time zone: Asia/Seoul (KST, +0900)
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: yes
Warning: The system is configured to read the RTC time in the local time zone.
         This mode cannot be fully supported. It will create various problems
         with time zone changes and daylight saving time adjustments. The RTC
         time is never updated, it relies on external facilities to maintain it.
         If at all possible, use RTC in UTC by calling
         'timedatectl set-local-rtc 0'.

 

 

2) chrony 설치 및 NTP 기능 활성화

 

(1) chrony 설치

[root@localhost ~]# dnf install chrony

 

 

(2) chrony 활성화

[root@localhost ~]# systemctl enable chronyd

 

(3) 방화벽 등록(Linux NTP Server를 대비하기 위하여 설정)

[root@localhost ~]# firewall-cmd --add-service=ntp --permanent
[root@localhost ~]# firewall-cmd --reload

 

(4) 환경파일 설정

  • 기존의 코드부분을 주석처리한 후에 Windows NTP Server IP주소 추가
  • Windows NTP Server의 ‘root dispersion’ 값이 높으면 신뢰할 수 없다고 판단하여 통신은 되는데 동기화가 안 되는 현상이 발생한다. 이를 해결하기 위해 maxdistance 값을 설정해준다.
[root@localhost ~]# vi /etc/chrony.conf

 

 

- /etc/chrony.conf 파일

#server 2.redhat.pool.ntp.org
#server 0.kr.pool.ntp.org
#server 0.asia.pool.ntp.org
#server 2.asia.pool.ntp.org
server 192.168.1.1
maxdistance 16.0

 

 

(5) port 활성화(Linux NTP Server를 대비하기 위하여 설정)

[root@localhost ~]# firewall-cmd --zone=public --add-port=123/udp --permanent
[root@localhost ~]# firewall-cmd --reload

 

 

(6) chrony 시작 또는 재시작

[root@localhost ~]# systemctl start chronyd
[root@localhost ~]# systemctl restart chronyd

 

(7) chrony 상태 확인

[root@localhost ~]# systemctl status chronyd
chronyd.service - NTP client/server
Loaded: loaded (/usr/lib/systemd/system/chronyd.service; enabled; vendor pre>
Active: active (running) since Mon 2022-04-04 10:04:04 KST; 1h 50min ago
Docs: man:chronyd(8)
man:chrony.conf(5)
Process: 6426 ExecStopPost=/usr/libexec/chrony-helper remove-daemon-state (co>
Process: 6435 ExecStartPost=/usr/libexec/chrony-helper update-daemon (code=ex>
Main PID: 6434 (chronyd)
Tasks: 1 (limit: 100405)
Memory: 796.0K
CGroup: /system.slice/chronyd.service
└─6434 /usr/sbin/chronyd
4월 04 10:04:04 localhost.localdomain systemd[1]: Starting NTP client/server...
4월 04 10:04:04 localhost.localdomain chronyd[6434]: chronyd version 4.1 start>
4월 04 10:04:04 localhost.localdomain chronyd[6434]: Frequency 37.510 +/- 8.01>
4월 04 10:04:04 localhost.localdomain chronyd[6434]: Using right/UTC timezone >
4월 04 10:04:04 localhost.localdomain systemd[1]: Started NTP client/server.
4월 04 10:06:13 localhost.localdomain chronyd[6434]: Selected source 192.168.1>
4월 04 10:06:13 localhost.localdomain chronyd[6434]: System clock wrong by 299>
4월 04 10:56:12 localhost.localdomain chronyd[6434]: System clock was stepped >
4월 04 10:56:12 localhost.localdomain chronyd[6434]: System clock TAI offset s>
lines 1-23/24 95%

 

 

(8) chrony 소스 정보 확인

  • 아직 Windows NTP Server가 켜져 있지 않아서 Reach, LastRx, sample 값이 0ns이다.
[root@localhost ~]# chronyc sources
MS Name/IP address Stratum Poll Reach LastRx Last sample
===============================================================================
^? 192.168.1.1 0 6 0 - +0ns[ +0ns] +/- 0ns

 

 

(9) chrony 트래킹 정보 확인 

  • 아직 Windows NTP Server가 켜져 있지 않아서 여러 파라미터 값이 디폴트로 되어 있다.
[root@localhost ~]# chronyc tracking
Reference ID : 00000000 ()
Stratum : 0
Ref time (UTC) : Thu Jan 01 00:00:00 1970
System time : 0.000000001 seconds fast of NTP time
Last offset : +0.000000000 seconds
RMS offset : 0.000000000 seconds
Frequency : 37.693 ppm fast
Residual freq : +0.000 ppm
Skew : 0.000 ppm
Root delay : 1.000000000 seconds
Root dispersion : 1.000000000 seconds
Update interval : 0.0 seconds
Leap status : Not synchronised

 

 

 

3. Windows NTP Server – Linux NTP Client 동기화

  

1) Windows NTP Server

  • 위에서 만든 WindowsNtpServer.bat 배치파일을 관리자 권한으로 실행한다.

 

2) Linux NTP Client

  • “systemctl status chronyd” 명령을 실행해서 상태가 Inactive일 경우에 “systemctl start chronyd” 명령을 실행한다.
  • chrony는 64초마다 시간의 동기화를 시도하며 차이는 1ms를 넘기지 않는다.

 

[root@localhost ~]# chronyc sources
MS Name/IP address Stratum Poll Reach LastRx Last sample
==========================================================================
^? 192.168.1.1 1 6 1 1 +1179us[+1179us] +/- 10.0s
 
// 64 seconds later
 
[root@localhost ~]# chronyc sources
MS Name/IP address Stratum Poll Reach LastRx Last sample
==========================================================================
^* 192.168.1.1 1 6 7 3 +18us[+1118us] +/- 10.0s
 
 
[root@localhost ~]# chronyc tracking
Reference ID : C0A80101 (192.168.1.1)
Stratum : 2
Ref time (UTC) : Mon Apr 04 03:04:59 2022
System time : 0.000000000 seconds fast of NTP time
Last offset : +0.001099276 seconds
RMS offset : 0.001099276 seconds
Frequency : 37.693 ppm fast
Residual freq : -0.563 ppm
Skew : 0.745 ppm
Root delay : 0.000640071 seconds
Root dispersion : 10.001871109 seconds
Update interval : 0.0 seconds

 

 

4. 동기화 상태에서 시간설정 및 동기화 테스트

 

1) Windows NTP Server의 시간을 바꾸고 동기화가 되기를 기다렸다.

  • 윈도우 시간이 더 빠를 때 5분 30초가 걸렸고, 리눅스 시간이 더 빠를 때는 10분이 지나도 안 바뀌었다.

 

2) Windows NTP Server의 시간을 바꾸고 WindowsNtpServer.bat 배치파일을 관리자 권한으로 실행한 후에 동기화가 되기를 기다렸다.

  • 윈도우 시간 및 리눅스 시간에 상관없이 5분 30초 정도가 걸렸다.

 

3) Windows NTP Server의 시간을 바꾸고 WindowsNtpServer.bat 배치파일을 관리자 권한으로 실행 및 “systemctl restart chronyd” 명령을 실행한 후에 동기화가 되기를 기다렸다.

  • 두 시간 차이가 4분, 6분, 50분, 1시간, 2시간이 나도 2분 10초 ~ 3분 20초 정도가 걸렸다
  • Server와 Client 시간이 50분 차이에서 점점 좁혀지는 과정 예시이며, Client가 64초마다 지속적으로 Server와의 시간 차이를 좁히려 하고 있다.
MS Name/IP address Stratum Poll Reach LastRx Last sample
==========================================================================
^? 192.168.1.1 1 6 1 8 -2998s[ -2998s] +/- 10.0s
 
MS Name/IP address Stratum Poll Reach LastRx Last sample
==========================================================================
^? 192.168.1.1 1 6 1 64 -2998s[ -2998s] +/- 10.0s
 
MS Name/IP address Stratum Poll Reach LastRx Last sample
==========================================================================
^? 192.168.1.1 1 6 3 1 -2998s[ -2998s] +/- 10.0s
 
MS Name/IP address Stratum Poll Reach LastRx Last sample
==========================================================================
^? 192.168.1.1 1 6 3 64 -2998s[ -2998s] +/- 10.0s
 
MS Name/IP address Stratum Poll Reach LastRx Last sample
==========================================================================
^* 192.168.1.1 1 6 7 1 -149us[ -2998s] +/- 10.0s
 
MS Name/IP address Stratum Poll Reach LastRx Last sample
==========================================================================
^* 192.168.1.1 1 6 17 20 -76us[ -183us] +/- 10.0s
 
MS Name/IP address Stratum Poll Reach LastRx Last sample
==========================================================================
^* 192.168.1.1 1 6 77 17 -528us[ -592us] +/- 10.0s
 
MS Name/IP address Stratum Poll Reach LastRx Last sample
==========================================================================
^* 192.168.1.1 1 6 177 0 -465us[ -558us] +/- 10.0s
 
MS Name/IP address Stratum Poll Reach LastRx Last sample
==========================================================================
^* 192.168.1.1 1 6 377 1 -305us[ -394us] +/- 10.0s
 
MS Name/IP address Stratum Poll Reach LastRx Last sample
==========================================================================
^* 192.168.1.1 1 6 377 0 -226us[ -293us] +/- 10.0s
 
MS Name/IP address Stratum Poll Reach LastRx Last sample
==========================================================================
^* 192.168.1.1 1 6 377 3 -144us[ -189us] +/- 10.0s
 
MS Name/IP address Stratum Poll Reach LastRx Last sample
==========================================================================
^* 192.168.1.1 1 6 377 64 -11us[ -16us] +/- 10.0s
 
MS Name/IP address Stratum Poll Reach LastRx Last sample
==========================================================================
^* 192.168.1.1 1 6 377 27 -409us[ -449us] +/- 10.0s

 

 

[참고문헌]

 

  • 윈도우10 NTP 서버 만드는 방법은 아래의 포스팅을 참고하였으며, 윈도우10 NTP 클라이언트 만드는 방법도 자세히 나와있다.

 

jeong-pro.tistory.com/201

 

 

 

 

 

시간동기화

  

 

반응형

댓글