본문 바로가기
IT/리눅스 이론

[Linux] 리눅스 ntp 서버 및 ntp client 시간 동기화 구성

by 꾸꾸웍스 2022. 11. 19.
반응형

linux ntp 서버 및 클라이언트 설정 썸네일
linux ntp 서버 및 클라이언트 설정

 

유닉스 및 리눅스 서버의 NTP를 설정하여 1대 혹은 다수의 서버의 시간 동기화를 하는 방법에 대해 소개해드리겠습니다. 1대의 리눅스를 사용한다면 아래 내용의 NTP 서버 설정을 완료하면 되고 다수의 리눅스 서버를 사용한다면 NTP 서버와  다수의 NTP 클라이언트 설정을 통해 시간 동기화를 완료할 수 있습니다.

 

※ 목차 구성
1) linux ntp란?
2) 리눅스 ntp 서버 설정
3) 리눅스 ntp client 서버 설정
4) 리눅스 ntp 설정 확인

 

linux ntp란?

NTP란 Network Time Protocol의 약자로 가장 오래된 인터넷 프로토콜 중 하나로 패킷 교환, 가변 레이턴시 데이터를 이용해 네트워크를 통해 컴퓨터 및 서버 간 시간 동기화 즉, 현재시간을 맞춰주는 네트워크 프로토콜로 OSI 7 계층의 응용계층에 속하며 NTP 서버는 서버와 클라이언트 구조로 이루어지는 프로토콜입니다.

 

인터넷 온라인 상태의 UDP 기반 방식을 이용하기 때문에 NTP 서버를 구성한 서버는 보안에 취약해 따로 NTP 서버를 구성할 Time Server를 구성하기도 합니다. 모든 서버가 NTP로 구성한 서버를 바라보도록 설정하는 것이 아닌 대부분 트리구조로 이루어지며 NTP 클라이언트 서버들은 최상위로부터 받아온 다음 단계, 또는 다음다음 단계의 서버에서 시간을 가져옵니다.

 

 

 

반응형

 

 

리눅스 ntp 서버 설정

NTP 패키지 설치

NTP 서버를 구성할 Time Server에 ntp를 설치합니다. rpm 명령어를 활용해 이미 설치가 되어있는지 확인하여 설치가 되지 않았다면 yum 명령어를 이용하여 ntp 패키지를 설치합니다.

 

yum install ntp

 

root@server # yum install ntp
...

root@server # rpm -qa | grep ntp
ntp-4.2.6p5-29.el7.centos.2.x86_64
ntpdate-4.2.6p5-29.el7.centos.2.x86_64

 

 

NTP 설정 파일 수정

ntp 서버에 ntp 패키지가 설치되었다면 /etc 디렉토리 하위에 ntp.conf 파일이 생성됩니다. ntp서비스를 시작하기 전에 ntp 서버 /etc/ntp.conf 파일에 Time Server가 시간을 동기화할 IP를 추가하고 사용하지 않을 설정들을 주석처리 후 ntp log를 설정 등을 설정합니다.

cp -p /etc/ntp.conf /etc/ntp.conf_bk

/etc/ntp.conf 파일을 수정하기 전 원본 파일을 백업한 후 수정합니다.

 

vim /etc/ntp.conf

 

root@server # vim /etc/ntp.conf
...
 8  #restrict default nomdify notrap nopeer noquery
...
13  #restrict 127.0.0.1
14  #restrict ::1
...
21  #server 0.centos.pool.ntp.org. iburst
22  #server 1.centos.pool.ntp.org. iburst
23  #server 2.centos.pool.ntp.org. iburst
24  #server 3.centos.pool.ntp.org. iburst
...
26  server time2.kriss.re.kr prefer
27  server 127.127.1.0
28  broadcast 224.0.1.1 ttl 4
...
69  #disable monitor
70  logfile /var/log/security/ntp.log

 

ntp 서버 /etc/ntp.conf 파일에 8, 13, 14, 21, 22, 23, 24, 69번 줄은 주석처리를 하고 26, 27, 28, 70번 줄을 추가해 줍니다. 68번 줄은 ntp 서버의 ntp 관련 로그를 설정해 놓는 설정으로 경로는 원하는 경로를 설정하면 됩니다. 또한 26번 줄의 ntp 서버의 시간 동기화의 설정은 아래 표 중 하나를 선택하여 공용 ntp 서버를 설정합니다.

 

주소 IP
time.bora.net 203.248.240.140
time.nuri.net 211.115.194.21
time2.kriss.re.kr 210.98.16.101

 

NTP  서비스 시작
systemctl enable ntpd
systemctl start ntpd

 

root@server # systemctl enable ntpd
Created symlink from /etc/systemd/system/multi-user.target.wants/ntpd.service to /usr/lib/systemd/system/ntpd.service.

root@server # systemctl start ntpd

 

 

 

 

리눅스 ntp client 서버 설정

NTP 패키지 설치

NTP 서버를 구성할 ntp client 서버에 ntp를 설치합니다. rpm 명령어를 활용해 이미 설치가 되어있는지 확인하여 설치가 되지 않았다면 yum 명령어를 이용하여 ntp 패키지를 설치합니다.

 

yum install ntp

 

root@client # yum install ntp
...

root@client # rpm -qa | grep ntp
ntp-4.2.6p5-29.el7.centos.2.x86_64
ntpdate-4.2.6p5-29.el7.centos.2.x86_64

 

 

NTP 설정 파일 수정

ntp 클라이언트 서버에 ntp 패키지가 설치되었다면 ntp 서버와 동일하게 /etc 디렉토리 하위에 ntp.conf 파일이 생성됩니다. ntp서비스를 시작하기 전에 ntp 클라이언트 서버 /etc/ntp.conf 파일에 ntp 서버 IP를 추가하고 사용하지 않을 설정들을 주석처리 후 ntp log를 설정 등을 설정합니다.

cp -p /etc/ntp.conf /etc/ntp.conf_bk

/etc/ntp.conf 파일을 수정하기 전 원본 파일을 백업한 후 수정합니다.

 

vim /etc/ntp.conf

 

root@client # vim /etc/ntp.conf
...
 8  #restrict default nomdify notrap nopeer noquery
...
13  #restrict 127.0.0.1
14  #restrict ::1
...
21  #server 0.centos.pool.ntp.org. iburst
22  #server 1.centos.pool.ntp.org. iburst
23  #server 2.centos.pool.ntp.org. iburst
24  #server 3.centos.pool.ntp.org. iburst
...
26  server 192.168.2.80
...
67  #disable monitor
68  logfile /var/log/security/ntp.log

 

ntp 클라이언트 서버 /etc/ntp.conf 파일 8, 13, 14, 21, 22, 23, 24, 67을 주석 처리하고 26번 라인은 ntp 서버를 바라보게 ntp 서버가 설정된 IP를 입력 후 68번 줄의 ntp log를 설정합니다. 26번 라인은 IP를 직접 입력하거나 /etc/hosts 파일에 ntp 서버가 등록된 호스트 이름이 있다면 ntp서버의 hostname을 작성해도 됩니다.

 

 

NTP  서비스 시작
systemctl enable ntpd
systemctl start ntpd

 

root@client # systemctl enable ntpd
Created symlink from /etc/systemd/system/multi-user.target.wants/ntpd.service to /usr/lib/systemd/system/ntpd.service.

root@client # systemctl start ntpd

 

 

 

 

리눅스 ntp 설정 확인

NTP 시간 동기화 확인

ntpq -p
ntpq -c rl

 

NTP 서버

root@server # ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
*210.98.16.101   10.172.11.238    2 u  290 1024  377   11.702   -0.445   1.444
 LOCAL(0)        .LOCL.           5 l  24d   64    0    0.000    0.000   0.000
 ntp.mcast.net   .MCST.          16 u    -   64    0    0.000    0.000   0.000

root@server # ntpq -c rl
associd=0 status=0618 leap_none, sync_ntp, 1 event, no_sys_peer,
version="ntpd 4.2.6p5@1.2349-o Tue Jun 23 15:38:18 UTC 2020 (1)",
processor="x86_64", system="Linux/3.10.0-1160.45.1.el7.x86_64", leap=00,
stratum=3, precision=-23, rootdelay=12.907, rootdisp=36.534,
refid=210.98.16.101,
reftime=e721a40e.8391b409  Fri, Nov 18 2022 15:26:22.513,
clock=e721a536.dd58240c  Fri, Nov 18 2022 15:31:18.864, peer=51233,
tc=10, mintc=3, offset=-0.445, frequency=80.305, sys_jitter=1.444,
clk_jitter=1.894, clk_wander=0.259

 

NTP 클라이언트 서버
root@client # ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
*server            210.98.16.101    3 u  729 1024  377    0.224   -2.204   1.272

root@client # ntpq -c rl
associd=0 status=0618 leap_none, sync_ntp, 1 event, no_sys_peer,
version="ntpd 4.2.6p5@1.2349-o Tue Jun 23 15:38:18 UTC 2020 (1)",
processor="x86_64", system="Linux/3.10.0-1160.53.1.el7.x86_64", leap=00,
stratum=4, precision=-25, rootdelay=13.636, rootdisp=89.628,
refid=192.168.2.80,
reftime=e7219df8.aee8390e  Fri, Nov 18 2022 15:00:24.683,
clock=e721a508.45a5cb56  Fri, Nov 18 2022 15:30:32.272, peer=16047,
tc=10, mintc=3, offset=-2.204, frequency=-8.699, sys_jitter=0.000,
clk_jitter=1.974, clk_wander=0.358

 

 

 

 

반응형
그리드형

댓글