OS/Linux

[Linux] Chrony를 이용한 NTP 설정

모클 2023. 10. 17. 13:56

 

1. NTP 설정을 하는 이유

 

NTP 란 네트워크 시간 프로토콜(Network Time Protocol)의 약자로 시간을 동기화 하는 데 사용되는 프로토콜입니다.

각 서버가 있는 위치가 다르다보니 중력에 의해 미세하게 시간의 오차가 발생하여 그 간격을 줄여주는 작업이 NTP 설정작업입니다.

 

이 시간동기화 작업을 진행하기 위해선 기준점이 되는 시간을 가지고 있는 서버가 필요하며 그 서버와 연동작업을 진행하야하는데, 모든 서버가 그서버를 바라보게되면, 과부하가 올것입니다. 따라서, 이 서버의 아래에 계층별로 서버를 두어 부하분산을 하게끔 설정이 되어있는데 이 계층 구조를 Stratum 이라 합니다.

 

예를 들면 아래와 같이 생각해볼 수 있습니다. 

(예시)

Stratum 0 : Atomic Clock(절대시계 개념)

Stratum 1 : 국내 시간동기화에 사용하는 NTP 서버 

Stratum 2 : DMZ Cloud내 구축할 NTP  Master서버

Stratum 3 : Client NTP 서버 

 

2. NTP 서버 구축

 

NTP 서버 패키지를 설치하기 위해선 두가지 패키지가 존재합니다.. ntpd 와 chrony 패키지가있는데 

chrony는 RedHat Enterprise Linux 8 부터 기본 NTP제품으로 채택되었기때문에 Chrony 패키지를 사용하여 구성해보겠습니다.

 

기존 ntp패키지가있다면 삭제하고 아래와같이 chrony패키지를 설치해줍니다.

$ sudo yum -y install chrony

chrony.conf 파일 수정 

 

 

#server 부분에는 현재 NTP 서버가 바라볼 두 서버(time.bora.net, kr.kornet.net )를 기입해줍니다.

#위의 두서버의 stratum이 2이기 때문에 이곳엔 local stratum에 3을 기입해주었습니다.

#Allow NTP clients 절에는 동기화시킬 client서버의 ip 혹은 대역대를 기입해줍니다. 

# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
#server 0.centos.pool.ntp.org iburst //주석처리
#server 1.centos.pool.ntp.org iburst //주석처리
#server 2.centos.pool.ntp.org iburst //주석처리
#server 3.centos.pool.ntp.org iburst //주석처리

server time.bora.net prefer iburst 
server kr.kornet.net iburst 

# Serve time even if not synchronized to a time source.
# local stratum 10

local stratum 3 

# Specify file locations
driftfile /var/lib/chrony/chrony.drift
logdir /var/log/chrony

# Allow NTP clients

allow 172.25.10.1/24
allow 172.25.20.1/24

# Listen on all network interfaces
bindcmdaddress 0.0.0.0

/etc/chrony.conf 파일 적용을위해서 chrony 데몬 리스타트가 필요합니다.

서버 재기동시 안올라오면 안되니 enable도 같이시켜줍니다.

systemctl restart chronyd
systenctl enable chronyd

timedatectl 명령어로 동기화 확인을 해줍니다. yes 라고 뜨면 정상입니다.

$timedatectl


 Local time: Tue 2023-10-17 17:19:18 KST
  Universal time: Sat 2023-10-17 08:19:18 UTC
        RTC time: Sat 2020-10-17 08:19:18
       Time zone: Asia/Seoul (KST, +0900)
     NTP enabled: yes
NTP synchronized: yes
 RTC in local TZ: no
      DST active: n/a

연동된 IP를 확인할 수있는 chronyc sources -v 명령어도 확인해줍니다.

chronyc sources -v

210 Number of sources = 2

  .-- Source mode  '^' = server, '=' = peer, '#' = local clock.
 / .- Source state '*' = current synced, '+' = combined , '-' = not combined,
| /   '?' = unreachable, 'x' = time may be in error, '~' = time too variable.
||                                                 .- xxxx [ yyyy ] +/- zzzz
||      Reachability register (octal) -.           |  xxxx = adjusted offset,
||      Log2(Polling interval) --.      |          |  yyyy = measured offset,
||                                \     |          |  zzzz = estimated error.
||                                 |    |           \
MS Name/IP address         Stratum Poll Reach LastRx Last sample               
===============================================================================
^* time.bora.net                 0   6     0     -     +0ns[   +0ns] +/-    0ns
^? kr.kornet.net        	 2   6    17     9  +4376ns[ +135us] +/-   47ms

Name/IP 앞에 * 표시가 있으면 성공적으로 동기화 된걸로 확인할수있습니다.

 

추가적으로 방화벽을 outbound 로 UDP 123포트로 열어주어야합니다.

 

3. NTP Clinet 구축

 

위의 내용과 비슷하지만 차이점은 이제 연결시킬대상이 외부서버가 아닌 구축한 NTP서버로 바꿔주시는겁니다.

제가 구축한서버는 172.25.10.99, 172.25.11.100이기때문에 chrony.conf를 아래와같이 작성해줍니다.

# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
#server 0.centos.pool.ntp.org iburst //주석처리
#server 1.centos.pool.ntp.org iburst //주석처리
#server 2.centos.pool.ntp.org iburst //주석처리
#server 3.centos.pool.ntp.org iburst //주석처리

server 172.25.10.99 prefer iburst 
server 172.25.11.100 iburst 

# Serve time even if not synchronized to a time source.
# local stratum 10


# Specify file locations
driftfile /var/lib/chrony/chrony.drift
logdir /var/log/chrony

# Allow NTP clients

# Listen on all network interfaces
bindcmdaddress 0.0.0.0

감사합니다.