본문 바로가기

리눅스

Ubuntu에서 NTP 서버를 구성하는 방법

728x90

Ubuntu에서 NTP(Network Time Protocol) 서버를 구성하는 방법

테스트 환경

docker run -it --rm --privileged --platform linux/amd64 anti1346/ubuntu2204:systemd bash

NTP 서버 설치 및 구성하는 방법

1. 패키지 설치

sudo apt update
sudo apt install ntp

2. 설정 파일 편집

NTP 구성 파일인 /etc/ntp.conf를 편집합니다.

vim /etc/ntp.conf

ntp.conf

더보기
더보기
# /etc/ntp.conf, configuration for ntpd; see ntp.conf(5) for help

driftfile /var/lib/ntp/ntp.drift

# Leap seconds definition provided by tzdata
leapfile /usr/share/zoneinfo/leap-seconds.list

# Enable this if you want statistics to be logged.
#statsdir /var/log/ntpstats/

statistics loopstats peerstats clockstats
filegen loopstats file loopstats type day enable
filegen peerstats file peerstats type day enable
filegen clockstats file clockstats type day enable

# Specify one or more NTP servers.

# Use servers from the NTP Pool Project. Approved by Ubuntu Technical Board
# on 2011-02-08 (LP: #104525). See http://www.pool.ntp.org/join.html for
# more information.
pool 0.ubuntu.pool.ntp.org iburst
pool 1.ubuntu.pool.ntp.org iburst
pool 2.ubuntu.pool.ntp.org iburst
pool 3.ubuntu.pool.ntp.org iburst

# Use Ubuntu's ntp server as a fallback.
pool ntp.ubuntu.com

# Access control configuration; see /usr/share/doc/ntp-doc/html/accopt.html for
# details.  The web page <http://support.ntp.org/bin/view/Support/AccessRestrictions>
# might also be helpful.
#
# Note that "restrict" applies to both servers and clients, so a configuration
# that might be intended to block requests from certain clients could also end
# up blocking replies from your own upstream servers.

# By default, exchange time with everybody, but don't allow configuration.
restrict -4 default kod notrap nomodify nopeer noquery limited
restrict -6 default kod notrap nomodify nopeer noquery limited

# Local users may interrogate the ntp server more closely.
restrict 127.0.0.1
restrict ::1

# Needed for adding pool entries
restrict source notrap nomodify noquery

# Clients from this (example!) subnet have unlimited access, but only if
# cryptographically authenticated.
#restrict 192.168.123.0 mask 255.255.255.0 notrust


# If you want to provide time to your local subnet, change the next line.
# (Again, the address is an example only.)
#broadcast 192.168.123.255

# If you want to listen to time broadcasts on your local subnet, de-comment the
# next lines.  Please do this only if you trust everybody on the network!
#disable auth
#broadcastclient

3. NTP 서버 설정

파일에서 server 섹션을 찾아 NTP 서버를 추가하거나 기본 서버를 사용할 수 있습니다. 다음은 예시입니다.

vim /etc/ntp.conf
server 0.ubuntu.pool.ntp.org
server 1.ubuntu.pool.ntp.org
server 2.ubuntu.pool.ntp.org
server 3.ubuntu.pool.ntp.org

4. 필요에 따라 로컬 클라이언트 허용

restrict 섹션에서 로컬 클라이언트의 액세스를 허용하거나 제한할 수 있습니다. 예를 들어, 다음과 같이 설정하면 로컬 클라이언트에 대한 액세스가 허용됩니다.

restrict 192.168.0.0 mask 255.255.255.0 nomodify notrap

5. 서비스 재시작

NTP 서비스를 재시작하여 설정 변경을 적용합니다.

sudo service ntp restart

6. NTP 피어 목록

ntpq -p

 

ntpq 명령어의 주요 옵션

  • -p 또는 --peers: 현재 연결된 NTP 피어 목록을 표시합니다. 각 피어에 대한 정보 및 동기화 상태를 제공합니다.
  • -c 또는 --command: 특정 NTP 커맨드를 실행합니다. 커맨드는 명령어 뒤에 지정하여 사용됩니다. 예를 들어, -c rv는 rv 커맨드를 실행합니다.
  • -n 또는 --numeric: IP 주소를 숫자 형태로 표시합니다.
  • -i 또는 --numeric-hosts: 호스트 이름을 IP 주소 형태로 표시합니다.
  • -4 또는 --ipv4: IPv4 주소만 사용합니다.
  • -6 또는 --ipv6: IPv6 주소만 사용합니다.
  • -w 또는 --wide: 피어 표시를 넓은 형식으로 표시합니다.
  • -l 또는 --dual-clocl: 듀얼 클럭 모드로 실행합니다.
  • -v 또는 --verbose: 상세한 출력을 표시합니다.
  • -? 또는 --help: 도움말을 표시합니다.

이제 Ubuntu 시스템은 NTP 서버로 구성되었습니다. 다른 클라이언트 시스템에서 해당 NTP 서버에 액세스하여 시간 동기화를 받을 수 있습니다.

 

참고URL

- ubuntu manuals(ntpd) : https://manpages.ubuntu.com/manpages/jammy/en/man1/ntpq.1.html

 

728x90