리눅스 NTP 정리

2024. 1. 5. 14:48IT/네트워크

CentOS 7으로 ntpd를 설치하여 테스트한다.

Redhat Enterprise Linux 7은 ntp 기본 설정이 chronyd로 되어있다. 

chronyd가 보안과 성능, 자원 소모량에서 더 우월하다

 

ntp는 UTC 시간을 사용하고,

포트는 UDP 123번를 사용한다.

 

설치

yum -y install ntp

[root@Centos7 ~]# rpm -qa |grep ntp
ntp-4.2.6p5-29.el7.centos.2.x86_64
ntpdate-4.2.6p5-29.el7.centos.2.x86_64

 

설정파일 

/etc/ntp.conf

[root@Centos7 ~]# cat /etc/ntp.conf
# For more information about this file, see the man pages
# ntp.conf(5), ntp_acc(5), ntp_auth(5), ntp_clock(5), ntp_misc(5), ntp_mon(5).

driftfile /var/lib/ntp/drift

# Permit time synchronization with our time source, but do not
# permit the source to query or modify the service on this system.
restrict default nomodify notrap nopeer noquery

# Permit all access over the loopback interface.  This could
# be tightened as well, but to do so would effect some of
# the administrative functions.
restrict 127.0.0.1
restrict ::1

# Hosts on local network are less restricted.
#restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap

# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
server time.bora.net  

#broadcast 192.168.1.255 autokey        # broadcast server
#broadcastclient                        # broadcast client
#broadcast 224.0.1.1 autokey            # multicast server
#multicastclient 224.0.1.1              # multicast client
#manycastserver 239.255.254.254         # manycast server
#manycastclient 239.255.254.254 autokey # manycast client

# Enable public key cryptography.
#crypto

includefile /etc/ntp/crypto/pw

# Key file containing the keys and key identifiers used when operating
# with symmetric key cryptography.
keys /etc/ntp/keys

# Specify the key identifiers which are trusted.
#trustedkey 4 8 42

# Specify the key identifier to use with the ntpdc utility.
#requestkey 8

# Specify the key identifier to use with the ntpq utility.
#controlkey 8

# Enable writing of statistics records.
#statistics clockstats cryptostats loopstats peerstats

# Disable the monitoring facility to prevent amplification attacks using ntpdc
# monlist command when default restrict does not include the noquery flag. See
# CVE-2013-5211 for more details.
# Note: Monitoring will not be disabled with the limited restriction flag.
disable monitor

 

driftfile

 

 

restrict

Access Control로 해당하는 주소에 대한 권한을 제어한다.

nomodify  ntpq 쿼리 중  서버 상태를 수정하는 쿼리는 deny
notrap trap 사용 막음
nopeer 본 서버와 peer 관계를 맺으려는 시도 deny
noquery ntpq 쿼리를 deny한다.

*trap : 하드웨어 시그널에 반응하는 쉘 기능

 

기본 권한  : 

기본 4개 권한 deny

ignore를 사용하면 모든 권한을 deny

restrict default nomodify notrap nopeer noquery

 

 127.0.0.1과 ::1에서 모든 권한을 허가

restrict 127.0.0.1
restrict ::1

 

같은 IP대역대에서 권한 설정

restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap

 

동기화할 시간 서버 및 옵션 설정

server time.bora.net iburst

server 
동기화할 시간 서버 IP 혹은 host 이름
peer 
peer 관계 구축 시 사용 
iburst
1개 패킷 대신 8개를 전송하여 초기 동기화에 걸리는 시간을 단축 
burst
모든 polling 시에 서버가 8개 패킷을 전송
공용서버에 burst 옵션을 넣는 건 악용으로 간주된다고 하니
내부망에서만 사용

 

server 127.127.t.u 

Local reference clock으로 시간을 맞출 때는 "127.127.t.u" 형식을 사용한다.
t는 clock type 

u는 unit number 
https://www.eecis.udel.edu/~mills/ntp/html/refclock.html 
type 분류는 위 링크에서 확인 가능

가장 많이 사용하는 type 1은 폐쇄망 상황에서 로컬 시간을 기준으로 삼는다.
해당 링크에서는 1번 type 사용보다는 Orphan mode를 권장하고 있다.
https://www.eecis.udel.edu/~mills/ntp/html/orphan.html

 

fudge [ip] stratum 10

해당 서버와의 동기화를 stratum 10으로 간주하게 만든다.
보통 127.127.t.u와 같이 사용하여 내부 폐쇄망 상황에서 로컬 시간으로 NTP서버를 구축할 때 사용한다.

 

 

includefile /etc/ntp/crypto/pw

keys /etc/ntp/keys

disable monitor

 

 

동기화 상태 확인 방법은 여러가지가 있다.

[root@Centos7 ~]# ntpq -p

     remote           refid                st  t  when poll  reach   delay    offset    jitter
==============================================================================
*time.bora.net   1.222.230.51    3 u  121    64    12     14.180   34.453 114.111

 

remote 동기화된 서버 도메인 이름을 나타낸다.
공백 : 연결이 안되는 서버
* : 정상적으로 동기화 중
+ : 동기화가 가능한 후보 서버 
 - :  clustering 알고리즘에 의해 폐기
refid remote 서버가 NTP 정보는 받는 서버 IP 정보 
st stratum은 NTP의 계층 구조를 나타내며, 0에서 15까지 있다.
stratum 0은 기준이 되는 원자시계
stratum 1은 0에서 직접 시간을 받는 Primary Time Server다.
16은 unsync 상태를 나타낼 때 사용된다.

time.bora.net은 stratum 2에 해당하고, 
time.bora.net에서 시간 동기화를 받는 서버는
stratum 3에 해당한다.
t 동기화 방식 
l: local
u: unicast
m: multicast
b: broadcast
when 수신한 후 경과 시간, 수신 후 0으로 초기화됨
poll sync 요청 주기 
낮은 값으로 시작하여, 변화가 없으면 점차 증가해
max poll 값이 된다.(1024 default)
reach  최근 8번 poll 요청에 대한 응답
8번 모두 성공하면 11111111로 377(8진법)로 나타낸다.
8번 시도 중 가장 최근에 동기화에 실패하면 
11111110 = 376
11111101 = 375 
...
01111111 = 177
위와 같이 계속하여 갱신되므로 
숫자값으로 최근 8번 중 언제 실패했는지 유추가 가능하다

동기화 초기부터 8번 연속 성공이라면 
1,3,7,17,37,77,177,377로 점차 바뀌어간다.
delay (round trip time) 응답 지연 시간/ 밀리세컨드 단위
offset (time difference) NTP서버와 로컬과 시간 차이 / 밀리세컨드 단위
jitter  시간을 측정했을 때 발생하는 오차 중 작은 값 / 밀리세컨드 단위

 

 

[root@Centos7 ~]# ntpstat

synchronised to NTP server (203.248.240.140) at stratum 3
   time correct to within 253 ms
   polling server every 64 s

 

[root@Centos7 ~]# ntptime

ntp_gettime() returns code 0 (OK)
  time e94146ab.2fd08d3c  Fri, Jan  5 2024  0:00:59.186, (.186776912),
  maximum error 1697323 us, estimated error 12180 us, TAI offset 0
ntp_adjtime() returns code 0 (OK)
  modes 0x0 (),
  offset 421.662 us, frequency 4.501 ppm, interval 1 s,
  maximum error 1697323 us, estimated error 12180 us,
  status 0x2001 (PLL,NANO),
  time constant 6, precision 0.001 us, tolerance 500 ppm,

 

 

rdate 명령어 

[root@Centos7 ~]# rdate -p time.bora.net

rdate: [time.bora.net]  Fri Jan  5 14:46:40 2024
-s 해당 서버와 시간 바로 동기화 
-p 해당 서버 시간 체크

 

NTP서버와 1000초 이상 차이가 나면 자동으로 동기화를 더 이상 하지 않으므로

수동으로 시간을 맞춰줘야하는 경우가 있다.

 

 

 

peer 설정해보기

개요

서버 1: 192.168.17.100
서버 2: 192.168.17.50
-time.bora.net에서 ntp 정보를 받아온다.
-서버1, 2 사이 peer 설정을 맺어본다.
서버 1: 192.168.17.100
restrict default nomodify notrap noquery
restrict 192.168.17.0 mask 255.255.255.0 nomodify notrap
server time.bora.net iburst
peer 192.168.17.50 iburst

서버 2: 192.168.17.50
restrict default nomodify notrap noquery
restrict 192.168.17.0 mask 255.255.255.0 nomodify notrap
server time.bora.net iburst
peer 192.168.17.100 iburst

 

결과 

 

더 찾아보기 

NTP.CONF 설정 방법

https://www3.physnet.uni-hamburg.de/physnet/Tru64-Unix/HTML/MAN/MAN4/0134____.HTM

 

NTP 개념 및 명령어 설정 방법 

https://joungkyun.gitbook.io/annyung-3-user-guide/chapter6/chapter6-ntp

 

NTP - 안녕리눅스 3 User Guide

그리고, 2대의 Time server 간에는 peer 구성을 하여 서로 동기화를 하게 할 수 있지만, 제 개인적인 견해로는 Time service 특성상 peer 구성 보다는 그냥 master 2대로 구성하는 것이 관리상 더 편했던 것

joungkyun.gitbook.io

ntp 설정파일정보 

https://docs.ntpsec.org/latest/ntp_conf.html

 

ntpd Configuration File

The ntp.conf configuration file is read at initial startup by the ntpd(8) daemon in order to specify the synchronization sources, modes, and other related information. Usually, it is installed in the /etc directory, but could be installed elsewhere (see th

docs.ntpsec.org

 

ntpdate 명령어 메뉴얼 정보 

https://linux.die.net/man/8/ntpdate

ntppool 시간서버 클러스터 프로젝트

https://www.ntppool.org/ko/

 

pool.ntp.org: the internet cluster of ntp servers

Packet is awesome. When we started planning our recent unplanned server move, we investigated options for having not one, but two sites, for the “hub” systems for the NTP Pool. With 4000 NTP servers and hundreds of millions of clients using the system,

www.ntppool.org

ntp와 chronyd 비교 

https://s-core.co.kr/post_os/%EC%84%9C%EB%B2%84-%EC%8B%9C%EA%B0%84-%EB%8F%99%EA%B8%B0%ED%99%94-%EB%B0%A9%EB%B2%95/

 

에스코어

에스코어는 디지털 혁신을 위한 고급 프로페셔널 서비스를 제공합니다. 매니지먼트 컨설팅과 소프트웨어 테크놀로지 서비스 오퍼링을 살펴보세요.

s-core.co.kr

https://chrony-project.org/comparison.html

 

chrony – Comparison of NTP implementations

This is a comparison of accuracies that can be achieved when the NTP implementations are used as NTP clients in various clock and network conditions. The accuracy of the synchronized clock was measured in a simulated Linux environment. The results are mean

chrony-project.org

 

'IT > 네트워크' 카테고리의 다른 글

L3 FWLB  (0) 2020.08.29