[NTP 서버 구축]

ntp 데몬 설치

ntp 사용을 위해 서버에 ntp 데몬을 설치
$ yum install -y ntp

ntp 포트 열기

ntp는 udp 123 포트를 사용하기 때문에 방화벽 혹은 Secutiry group 등에 udp123 포트를 열어주자
  • inbound 룰에 udp 123포트가 열려야한다. (source 0.0.0.0/0)
  • 인스턴스의 보안 그룹 규칙은 포트 123(NTP)에서 아웃바운드 UDP 트래픽을 허용해야 하고, 네트워크 ACL 규칙은 포트 123에서 인바운드와 아웃바운드 UDP 트래픽을 모두 허용해야 한다.

ntp 설정 파일 수정

  • Client 접근을 허용하기 위해 restrict 설정을 추가
    • restrict [client IP] mask 255.255.255.0 nomodify notrap
  • 사용할 외부 ntp 서버주소를 추가해준다
    • server [ntp서버 주소]
  • 로컬 타임 서버로 동작하기 위해 127.127.1.0 서버 추가
  • stratum 값을 설정해준다
    • 값이 너무 낮으면 (Server dropped: strata too high) 이런 에러가 발생한다.
    • 적당히 낮은 수치로 해주자.
    • 1 혹은 2는 외부 ntp 서버값이므로 사용하지 말자
아래 conf 파일은 설정 완료된 상태
$ vi /etc/ntp.conf
# Permit time synchronization with our time source, but do not
# permit the source to query or modify the service on this system.
restrict default kod nomodify notrap nopeer noquery
restrict -6 default kod 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 -6 ::1
# Hosts on local network are less restricted.
#restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
restrict 172.31.0.0/16 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 0.centos.pool.ntp.org iburst
#server 1.centos.pool.ntp.org iburst
#server 2.centos.pool.ntp.org iburst
server kr.pool.ntp.org
server asia.pool.ntp.org
includefile /etc/ntp/crypto/pw
# Key file containing the keys and key identifiers used when operating
# with symmetric key cryptography.
keys /etc/ntp/keys
# Undisciplined Local Clock. This is a fake driver intended for backup
# and when no outside source of synchronized time is available.
server 127.127.1.0 # local clock
fudge 127.127.1.0 stratum 3

ntp daemon 부팅시 실행되게 서비스 등록, 시작

#centos 6
$ chkconfig ntpd on
$ service ntpd restart
#centos 7
$ systemctl enable ntpd
$ systemctl restart ntpd

ntp daemon 상태 확인

LOCAL이 표시되면 ntp 서버로 준비 된것임
$ ntpq -p
remote refid st t when poll reach delay offset jitter
==============================================================================
114.207.245.166 141.223.182.106 2 u 33 64 1 4.328 -5.253 0.000
202-65-114-202. 203.160.128.66 2 u 32 64 1 99.512 -1.440 0.000
*LOCAL(0) .LOCL. 3 l 31 64 1 0.000 0.000 0.000


[Client 설정] - ntp daemon

ntp 설정 파일 수정

다른 서버들은 모두 막아준다
$ vi /etc/ntp.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 [ ntp IP]
...

ntp daemon 부팅시 실행되게 서비스 등록, 시작

#centos 6
$ chkconfig ntpd on
$ service ntpd restart
#centos 7
$ systemctl enable ntpd
$ systemctl restart ntpd



[Client 설정] - cron.daily

!! Client 서버에서는 내부 ntp 서버와 cron으로 시간을 동기화 하기 때문에  ntpdaemon 서비스는 종료

cron.daily 사용

쉘 스크립트 생성
$ vi /etc/cron.daily/time-sync
#!/bin/bash
/usr/sbin/ntpdate [ ntp IP] && /sbin/clock -w # [ ntp IP] -> xxx.xxx.xxx.xxx,
실행 권한 부여
$ chmod 755 /etc/cron.daily/time-sync
매일 새벽 4시에 동기화 스크립트를 수행한다


반응형