Linux 시스템 시간 설정

Linux 시스템 시간 설정
Linux 시스템의 시간 확인은 date 명령으로 할 수 있으며, date 명령은 수동으로 시간 설정도 가능하다. 
그리고, rdate, ntpdate 명령을 이용하여 time 서버와의 시간을 동기화 할 수 있으며, ntpd 데몬을 이용해서 자동으로 서버의 시간을 time서버와 맞출 수도 있다. 


1 시스템의 시간 확인

[root@localhost ~]# date
Wed May 16 12:01:03 KST 2018
[root@localhost ~]# 


2 시스템 시간 수동으로 변경하기

- date 명령어 뒤에 "월시간년도.초" 의 형식으로 입력하면 시간 세팅이 가능 하며, 별도의 형식으로 출력 및 세팅이 가능하다.
[root@localhost ~]# date 031211572018.00
Mon Mar 12 11:57:00 KST 2018
[root@localhost ~]# date
Mon Mar 12 11:57:02 KST 2018
[root@localhost ~]# 

- YYYY-MM-DD 형식으로 날짜 확인 및 날짜 변경
[root@localhost ~]# date '+%Y-%m-%d'
2018-05-16
[root@localhost ~]# date '+%Y-%m-%d' -s "2018-05-13"
2018-05-13
[root@localhost ~]# date '+%Y-%m-%d'
2018-05-13

- hh:mm:ss 형식으로 시간 확인 및 시간 변경
[root@localhost ~]# date +%T
11:56:42
[root@localhost ~]# date +%T -s "10:00:00"
10:00:00
[root@localhost ~]# date +%T
10:00:01
[root@localhost ~]# 


3. 시스템 시간을 time 서버와 동기화 하기

rdate 명령이나, ntpdate 명령을 이용해서 시스템의 시간을 time서버와 동기화 할 수 있다.
참고로 ntpdate 명령은 아래에서 설명할 ntpd 데몬이 실행되고 있는 경우 사용이 불가능하기 때문에, ntpd 데몬을 종료한 후 사용해야 한다.

- rdate 명령을 이용한 time서버와이 시간 동기화
[root@localhost ~]# date
Mon Mar 12 12:02:31 KST 2018
[root@localhost ~]# rdate -s time.bora.net
[root@localhost ~]# date
Wed May 16 12:10:09 KST 2018
[root@localhost ~]# 

- ntpdate 명령을 이용한 time서버와의 시간 동기화
[root@localhost ~]# ntpdate time.bora.net
16 May 12:10:56 ntpdate[26374]: adjust time server 203.248.240.140 offset 0.036118 sec
[root@localhost ~]# date
Wed May 16 12:11:00 KST 2018
[root@localhost ~]# 


4. 시스템의 하드웨어 시간 확인 및 변경

시스템의 하드웨어 시간은 mainboard 에 저장되어 있는 시간이며, 시스템의 로드 증가나, 메인보드 battery에 따라 시간이 느려질 수 있다.
hwclock 명령으로 하드웨어 시간을 확인 할 수 있으며, -w 옵션을 추가하면 하드웨어 시간을 시스템시간과 동일하게 맞출 수 있다. 

[root@localhost etc]# hwclock
Wed 16 May 2018 02:41:56 AM KST  -0.487239 seconds
[root@localhost etc]# hwclock -w
[root@localhost etc]# hwclock
Wed 16 May 2018 11:44:41 AM KST  -0.200472 seconds
[root@localhost etc]# 


5. cron을 이용한 time 서버와의 시간 동기화

- 아래와 같이 crontab에 등록하여, 주시적으로 시스템의 시간을 time서버와 동기화 하고, 하드웨어 시간을 시스템 시간과 동기화 할 수 있다.
[root@localhost etc]# crontab -l
#DATE sync
0 0 * * * /usr/bin/rdate -s time.bora.net && /sbin/clock -w
[root@localhost ~]# 


6. ntpd 데몬을 이용한 time서버와의 자동 동기화

- ntp 패키지가 설치되어 있지 않을 경우, yum 으로 설치
[root@localhost ~]# rpm -qa | grep ntp
[root@localhost ~]# yum install ntp
Loaded plugins: fastestmirror, security
Setting up Install Process
Loading mirror speeds from cached hostfile
 * base: mirror.kakao.com
 * epel: mirror.premi.st
 * extras: mirror.kakao.com
 * updates: mirror.navercorp.com
Resolving Dependencies
--> Running transaction check
---> Package ntp.x86_64 0:4.2.6p5-12.el6.centos.2 will be installed
--> Processing Dependency: ntpdate = 4.2.6p5-12.el6.centos.2 for package: ntp-4.2.6p5-12.el6.centos.2.x86_64
--> Running transaction check
---> Package ntpdate.x86_64 0:4.2.6p5-12.el6.centos.2 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

====================================================================================================================================
 Package                    Arch                      Version                                      Repository                  Size
====================================================================================================================================
Installing:
 ntp                        x86_64                    4.2.6p5-12.el6.centos.2                      updates                    600 k
Installing for dependencies:
 ntpdate                    x86_64                    4.2.6p5-12.el6.centos.2                      updates                     79 k

Transaction Summary
====================================================================================================================================
Install       2 Package(s)

Total download size: 679 k
Installed size: 1.8 M
Is this ok [y/N]: y
Downloading Packages:
(1/2): ntp-4.2.6p5-12.el6.centos.2.x86_64.rpm                                                                | 600 kB     00:00     
(2/2): ntpdate-4.2.6p5-12.el6.centos.2.x86_64.rpm                                                            |  79 kB     00:00     
------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                               133 kB/s | 679 kB     00:05     
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing : ntpdate-4.2.6p5-12.el6.centos.2.x86_64                                                                           1/2 
  Installing : ntp-4.2.6p5-12.el6.centos.2.x86_64                                                                               2/2 
  Verifying  : ntpdate-4.2.6p5-12.el6.centos.2.x86_64                                                                           1/2 
  Verifying  : ntp-4.2.6p5-12.el6.centos.2.x86_64                                                                               2/2 

Installed:
  ntp.x86_64 0:4.2.6p5-12.el6.centos.2                                                                                              

Dependency Installed:
  ntpdate.x86_64 0:4.2.6p5-12.el6.centos.2                                                                                          

Complete!
[root@localhost ~]# 

- ntpd 데몬 실행
[root@localhost ~]# /etc/init.d/ntpd start
Starting ntpd:                                             [  OK  ]

- ntp 상태 확인
ntpq -p 명령으로 확인 할 수 있으며, ntpd 데몬을 실행 하고 일정 시간이 지나면 * 표시된 서버가 현재 시스템과 시간이 동기화되고 있는 time 서버이다.
[root@localhost ~]# ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
*dadns.cdnetwork 204.123.2.5      2 u    2   64    1    1.538  -12.351   0.245
-static.betaidc. 106.247.248.106  3 u    1   64    1    2.140   -9.795   0.271
+211.52.209.148  216.239.35.12    2 u    2   64    1    8.017   -9.683   0.141
+send.mx.cdnetwo 204.123.2.5      2 u    1   64    1    1.343  -11.319   0.194
[root@localhost ~]# 

- ntp 데몬 활성화
런레벨 2,3,4,5 에서 ntpd 데몬이 자동으로 실행 되도록 활성화 한다.
[root@localhost etc]# chkconfig --list | grep ntp
ntpd            0:off   1:off   2:off   3:off   4:off   5:off   6:off
ntpdate         0:off   1:off   2:off   3:off   4:off   5:off   6:off
[root@localhost etc]# chkconfig ntpd on
[root@localhost etc]# chkconfig --list | grep ntp
ntpd            0:off   1:off   2:on    3:on    4:on    5:on    6:off
ntpdate         0:off   1:off   2:off   3:off   4:off   5:off   6:off

- time서버 변경
시간을 동기화 할 time서버를 변경할 경우, /etc/ntp.conf 파일에서 아래와 같이 기존 time 서버에 주석처리하고 변경할 time서버를 입력하여 저장한 후에 ntpd 데몬을 리스타트 하면 된다.

#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 kr.pool.ntp.org
server asia.pool.ntp.org
server pool.ntp.org

[root@localhost ~]# /etc/init.d/ntpd restart
Shutting down ntpd:                                        [  OK  ]
Starting ntpd:                                             [  OK  ]
[root@localhost ~]# ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
+send.mx.cdnetwo 204.123.2.5      2 u   29   64  377    1.395   -4.453   4.039
+ntp.nap.net.id  103.31.225.225   3 u   22   64  257  433.249   -6.238   5.598
*211.52.209.148  216.239.35.12    2 u   21   64  377    8.066    4.448   5.816
[root@localhost ~]# 
태그가 없습니다.

3개의 댓글이 있습니다.

5년 이하 전

예전 엔지니어일때 많이 사용했던 ntpdate
좀더 자세한 내용 감사합니다.

Reply

댓글 남기기

댓글을 남기기 위해서는 로그인이 필요합니다.

로그인 회원가입

6년 이하 전

감사합니다.

Reply

댓글 남기기

댓글을 남기기 위해서는 로그인이 필요합니다.

로그인 회원가입

1st 5stars

6년 이하 전

rdate와 clock 명령으로 하루에 한번씩 동기화 시켜 두고 사용하고 있는데...
ntpdate 명령도 있나보네요.
잘 읽었습니다.
참... 요즘 데비안 계열의 OS는 ntp 서버가 기본적으로 설치되어 있는 것 같더라구요.
time server 구축하려고 자료도 찾아 보고 했었는데...
이미 작동되고 있어서 xubuntu, lubuntu 몇대 더 확인해 보았는데 모두 이미 작동되고 있더군요~

Reply

댓글 남기기

댓글을 남기기 위해서는 로그인이 필요합니다.

로그인 회원가입

댓글 남기기

댓글을 남기기 위해서는 로그인이 필요합니다.

로그인 회원가입