Linux Timezone 변경 & 시간 설정

우분투 기준 타임존 변경하는 방법과 시간 설정 방법이다.

Timezone 변경하기

date를 해보면 UTC1로 시간이 표시된다.

$ date
Fri Dec  1 05:25:04 UTC 2017

tzselect

tzselect를 사용해 TZ format을 알아볼 수 있고 이 값으로 Local Timezone을 변경할 수 있다. 이 방법은 로컬에만 적용된다.

$ tzselect
Please identify a location so that time zone rules can be set correctly.
Please select a continent, ocean, "coord", or "TZ".
 1) Africa
 2) Americas
 3) Antarctica
 4) Asia
 5) Atlantic Ocean
 6) Australia
 7) Europe
 8) Indian Ocean
 9) Pacific Ocean
10) coord - I want to use geographical coordinates.
11) TZ - I want to specify the time zone using the Posix TZ format.
#? 4
Please select a country whose clocks agree with yours.
 1) Afghanistan           18) Israel                35) Palestine
 2) Armenia               19) Japan                 36) Philippines
 3) Azerbaijan            20) Jordan                37) Qatar
 4) Bahrain               21) Kazakhstan            38) Russia
 5) Bangladesh            22) Korea (North)         39) Saudi Arabia
 6) Bhutan                23) Korea (South)         40) Singapore
 7) Brunei                24) Kuwait                41) Sri Lanka
 8) Cambodia              25) Kyrgyzstan            42) Syria
 9) China                 26) Laos                  43) Taiwan
10) Cyprus                27) Lebanon               44) Tajikistan
11) East Timor            28) Macau                 45) Thailand
12) Georgia               29) Malaysia              46) Turkmenistan
13) Hong Kong             30) Mongolia              47) United Arab Emirates
14) India                 31) Myanmar (Burma)       48) Uzbekistan
15) Indonesia             32) Nepal                 49) Vietnam
16) Iran                  33) Oman                  50) Yemen
17) Iraq                  34) Pakistan
#? 23

The following information has been given:

        Korea (South)

Therefore TZ='Asia/Seoul' will be used.
Local time is now:      Fri Dec  1 14:44:16 KST 2017.
Universal Time is now:  Fri Dec  1 05:44:16 UTC 2017.
Is the above information OK?
1) Yes
2) No
#? 1

You can make this change permanent for yourself by appending the line
        TZ='Asia/Seoul'; export TZ
to the file '.profile' in your home directory; then log out and log in again.

Here is that TZ value again, this time on standard output so that you
can use the /usr/bin/tzselect command in shell scripts:
Asia/Seoul

TZ format을 얻었다. 이 부분을 사용하는 쉘의 rc2에 추가하면 된다. 재로그인 또는 rc를 reload하면 timezone이 변경된걸 확인할 수 있다.

$ date
Fri Dec  1 05:48:03 UTC 2017
$ echo "TZ='Asia/Seoul'; export TZ" >> .profile
$ . .profile
$ date
Fri Dec  1 14:48:36 KST 2017

localtime

이 방법은 시스템 전체에 적용된다.

$ ll /etc/localtime
lrwxrwxrwx 1 root root 27 Nov 21 19:31 /etc/localtime -> /usr/share/zoneinfo/Etc/UTC

localtime이 UTC를 가르키고 있다. 이 부분을 다음과 같이 변경한다.

$ date
Fri Dec  1 06:10:04 UTC 2017
$ sudo ln -sf /usr/share/zoneinfo/Asia/Seoul /etc/localtime
$ date
Fri Dec  1 15:10:08 KST 2017

다른 시간대로 변경해야 한다면 /usr/share/zoneinfo에서 link할 파일을 찾아 적용하면 된다.

$ ls -ld /usr/share/zoneinfo/*/
drwxr-xr-x  2 root root 4096 Nov 21 19:31 /usr/share/zoneinfo/Africa/
drwxr-xr-x  6 root root 4096 Nov 21 19:31 /usr/share/zoneinfo/America/
drwxr-xr-x  2 root root 4096 Nov 21 19:31 /usr/share/zoneinfo/Antarctica/
drwxr-xr-x  2 root root 4096 Nov 21 19:31 /usr/share/zoneinfo/Arctic/
drwxr-xr-x  2 root root 4096 Nov 21 19:31 /usr/share/zoneinfo/Asia/
drwxr-xr-x  2 root root 4096 Nov 21 19:31 /usr/share/zoneinfo/Atlantic/
drwxr-xr-x  2 root root 4096 Nov 21 19:31 /usr/share/zoneinfo/Australia/
drwxr-xr-x  2 root root 4096 Nov 21 19:31 /usr/share/zoneinfo/Brazil/
drwxr-xr-x  2 root root 4096 Nov 21 19:31 /usr/share/zoneinfo/Canada/
drwxr-xr-x  2 root root 4096 Nov 21 19:31 /usr/share/zoneinfo/Chile/
drwxr-xr-x  2 root root 4096 Nov 21 19:31 /usr/share/zoneinfo/Etc/
drwxr-xr-x  2 root root 4096 Nov 21 19:31 /usr/share/zoneinfo/Europe/
drwxr-xr-x  2 root root 4096 Nov 21 19:31 /usr/share/zoneinfo/Indian/
drwxr-xr-x  2 root root 4096 Nov 21 19:31 /usr/share/zoneinfo/Mexico/
drwxr-xr-x  2 root root 4096 Nov 21 19:31 /usr/share/zoneinfo/Pacific/
drwxr-xr-x 19 root root 4096 Nov 21 19:31 /usr/share/zoneinfo/posix/
drwxr-xr-x 19 root root 4096 Nov 21 19:31 /usr/share/zoneinfo/right/
drwxr-xr-x  2 root root 4096 Nov 21 19:31 /usr/share/zoneinfo/SystemV/
drwxr-xr-x  2 root root 4096 Nov 21 19:31 /usr/share/zoneinfo/US/

시간대 조회

zdump(timezone dumper)를 사용하면 시간대를 조회할 수 있다.

로컬

$ zdump /etc/localtime
/etc/localtime  Fri Dec  1 15:43:39 2017 KST

태평양 표준시 (미국과 캐나다)

$ zdump  Canada/Pacific
Canada/Pacific  Thu Nov 30 22:39:36 2017 PST

하와이

$ zdump Pacific/Honolulu
Pacific/Honolulu  Thu Nov 30 20:39:48 2017 HST

시간 설정

손목시계나 탁상용시계의 시간을 맞추드시 시스템의 시간을 변경해보자.

수동 시간 설정

date로 간단하게 날짜와 시간을 변경할 수 있다. 루트권한이 필요하다.

$ sudo date -s "2015-12-24 17:58:23"
Thu Dec 24 17:58:23 KST 2015

rdate

rdate는 타임서버를 참조하여 표준시를 동기화 하는 툴이다.

요즘 시간 동기화는 NTP가 주로 이용되므로 임시적으로 사용할만한 방법이다.

rdate가 설치되어있지 않다면 먼저 설치한다.

$ rdate
-bash: /usr/bin/rdate: No such file or directory

$ sudo apt-get install rdate
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following package was automatically installed and is no longer required:
  zsh-common
Use 'sudo apt autoremove' to remove it.
The following NEW packages will be installed:
  rdate
0 upgraded, 1 newly installed, 0 to remove and 7 not upgraded.
Need to get 14.2 kB of archives.
After this operation, 69.6 kB of additional disk space will be used.
Get:1 http://us-west-2.ec2.archive.ubuntu.com/ubuntu xenial/main amd64 rdate amd64 1:1.2-6 [14.2 kB]
Fetched 14.2 kB in 0s (0 B/s)
Selecting previously unselected package rdate.
(Reading database ... 57441 files and directories currently installed.)
Preparing to unpack .../rdate_1%3a1.2-6_amd64.deb ...
Unpacking rdate (1:1.2-6) ...
Processing triggers for man-db (2.7.5-1) ...

time.bora.net 타임서버를 참조하여 표준시를 동기화 한다.

$ sudo date -s "09:32"
Fri Dec  1 09:32:00 KST 2017

$ date
Fri Dec  1 09:32:06 KST 2017

$ sudo rdate -s time.bora.net

$ date
Fri Dec  1 16:49:39 KST 2017

crontab

crontab에 등록하여 주기적으로 시간을 동기화 한다.

vi /etc/crontab

# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23) 
# | | .---------- day of month (1 - 31) 
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ... 
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat 
# | | | | | 
# * * * * * user-name command to be executed
0 5 * * * root /usr/bin/rdate -s time.bora.net && /sbin/hwclock -w

수동 시간 설정(date, rdate)이 안되는 문제

Ubuntu 16.04.3 LTS에서 위 방법으로 시간을 변경하면 변경이 안된다.

$ date
Fri Dec  1 16:34:53 KST 2017

$ sudo date -s "2015-12-24 17:58:23"
Thu Dec 24 17:58:23 KST 2015

$ date
Fri Dec  1 16:34:59 KST 2017

검색해보니 NTP가 시간을 자동으로 동기화 시키고 있었다.

$ ps -ef | grep timesyncd
systemd+ 19494     1  0 16:34 ?        00:00:00 /lib/systemd/systemd-timesyncd

timedatectl을 사용하여 데몬을 종료하면 시간을 수동으로 바꿀 수 있다.

$ sudo timedatectl set-ntp 0
$ ps -ef | grep timesyncd
$ sudo date -s "2015-12-24 17:58:23"
Thu Dec 24 17:58:23 KST 2015
$ date
Thu Dec 24 17:58:25 KST 2015

데몬을 다시 올리면 자동으로 표준시로 동기화 한다.

$ sudo timedatectl set-ntp 1
$ ps -ef | grep timesyncd
systemd+ 20337     1  0 16:40 ?        00:00:00 /lib/systemd/systemd-timesyncd

$ date
Fri Dec  1 16:41:00 KST 2017

시간 동기화및 타임존, 시간설정을 timedatectl 하나로 모두 설정할 수 있다.

시간 변경

$ sudo timedatectl set-time "2017-05-30 18:17:16"
Failed to set time: Automatic time synchronization is enabled

$ sudo timedatectl set-ntp 0

$ sudo timedatectl set-time "2017-05-30 18:17:16"

$ date
Tue May 30 18:17:17 KST 2017

타임존 변경

$ sudo timedatectl set-timezone Europe/Paris

$ date
Fri Dec  1 08:43:40 CET 2017

$ ll /etc/localtime
lrwxrwxrwx 1 root root 34 Dec  1 08:43 /etc/localtime -> ../usr/share/zoneinfo/Europe/Paris

References


  1. 협정 세계시(協定世界時, 프랑스어: Temps Universel Coordonné, 영어: Coordinated Universal Time) 또는 UTC는 1972년 1월 1일부터 시행된 국제 표준시이다. UTC는 국제원자시와 윤초 보정을 기반으로 표준화되었다. UTC는 그리니치 평균시(GMT)로 불리기도 하는데, UTC와 GMT는 초의 소숫점 단위에서만 차이가 나기 때문에 일상에서는 혼용되어 사용된다. 기술적인 표기에서는 UTC가 사용된다. – 위키백과 Ref, GMT와 UTC 차이 [return]
  2. Run Commands [return]
comments powered by Disqus