본문 바로가기

Linux/systemd

리눅스 시스템 시간 동기화하기

앞으로 티스토리 대신 blog.stackframe.dev에서 블로깅을 합니다. 이 블로그는 남겨 둘 예정입니다.

리눅스 시스템의 시간 동기화에 대해 사용되는 프로그램을 찾아보면 주로 ntpd, rdate를 소개하고 있습니다. 다만 둘 다 새로운 패키지를 설치해야 한다는 귀찮음과 rdate를 사용하여 정기적으로 동기화하려면 cron이나 systemd timer 등의 다른 프로그램의 도움이 필요합니다.


그래서 저는 요즘 대부분의 배포판에 도입된 systemd의 timesyncd 를 사용하여 시간 동기화를 하는 것을 추천합니다. systemd가 적용되어 있다면 systemd-timesyncd도 자동적으로 설치되기 때문에 따로 설치할 필요도 없고, systemd의 특유의 간단한 설정으로 어렵지 않게 적용할 수 있습니다.


먼저 timesyncd의 설정 파일은 /etc/systemd/timesyncd.conf 에 존재합니다.

#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.
#
# Entries in this file show the compile time defaults.
# You can change settings by editing this file.
# Defaults can be restored by simply deleting this file.
#
# See timesyncd.conf(5) for details.

[Time]
#NTP=
#FallbackNTP=0.arch.pool.ntp.org 1.arch.pool.ntp.org 2.arch.pool.ntp.org 3.arch.pool.ntp.org
#RootDistanceMaxSec=5
#PollIntervalMinSec=32
#PollIntervalMaxSec=2048

기본적으로 섹션을 알리는 [Time] 빼고 모두 주석처리되어 있습니다. 여기서 자신의 입맛에 맞게 설정하시면 됩니다.

먼저 NTP 옵션은 사용할 NTP 서버 리스트를 입력합니다. NTP Pool Project라고 세계 여러 NTP 서버들이 모여서 클러스터를 구축하는 프로젝트입니다. 여기서 원하는 NTP 서버의 주소를 찾아서 입력하시면 됩니다.

FallbackNTP는 이름 그대로 NTP 옵션에서 받아오지 못한다면 2차로 시도하는 NTP 서버 리스트들입니다. 보통 이 옵션에는 배포판이 기본 설정으로 입력한 주소들이 설정되어 있는게 대부분입니다.


밑의 3개는 NTP 서버에서 받아오는 정확도에 관한 옵션인데 딱히 설정하지 않고 기본 설정으로 둬도 무방할걸로 보입니다. RootDistanceMaxSec은 허용가능한 NTP 서버의 Root distance를 설정하는 것이고, PollIntervalMinSec과 PollIntervalMaxSec은 각각 NTP 서버에 동기화하는 최소, 최대 시간 간격입니다.


이 설정파일을 저장하고 systemd-timesyncd 서비스를 실행하고 시작 프로그램으로 등록하면 끝입니다.

# systemctl start systemd-timesyncd
# systemctl enable systemd-timesyncd

timedatectl 명령어로 현재 상태를 확인했을 때 NTP service: active 라고 나온다면 잘 작동하고 있는겁니다.

추가적으로 얻어오는 NTP 서버와 주기를 확인하고 싶으면 timedatectl timesync-status 명령으로 알 수 있습니다.


'Linux > systemd' 카테고리의 다른 글

systemd로 시스템 자원 제한하기  (6) 2019.01.09
systemd 사용자 유닛 만들기  (0) 2019.01.09
systemd timer로 cron 대체하기  (4) 2018.12.30
systemd로 서비스 관리하기  (0) 2018.12.27
systemd란 무엇인가  (0) 2018.12.27