본문 바로가기

배포/Aws

리눅스와 윈도우는 시간이 다르게 흘러가나??

리눅스의 시간과 윈도우의 시간은 다르게 흘러가나??

스프링 프로잭트를 ec2에 배포하고 상태를 살펴보았습니다. 로컬에서와 ec2에서의 시간이 다르게 나왔습니다. 이를 해결해보려 합니다.

Windows 와 Linux 는 각각 시간 동기화 방식이 다릅니다

  • Linux 는 동기화된 시간을 bios 에 반영할 때 UTC 시간을 반영하고,
  • Windows 는 Local(한국은 KST) 시간을 반영합니다.

위와 같은 차이로 인해서 OS 간 부팅을 하고 난 이후 간혹 시간 동기화 전에는 틀린 시간을 표시하는 경우가 발생합니다.

1. @PostConstruct를 사용하는 방법

@SpringBootApplication
public class SpringBootServerTimezoneApplication {

    @PostConstruct
    public void started() {
      // timezone UTC 셋팅
      TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
    }
    public static void main(String[] args) {       
      SpringApplication.run(SpringBootServerTimezoneApplication.class, args);
    }
}

위와같은 timezone 을 @Postconstruct로 셋팅하는 방법이 있답니다.

@PostConstruct란?

@PostConstruct는 의존성 주입이 이루어진 후 초기화를 수행하는 메서드입니다. @PostConstruct가 붙은 메서드는 클래스가 service(로직을 탈 때)를 수행하기 전에 발생합니다. 이 메서드는 다른 리소스에서 호출되지 않는다해도 수행됩니다. SpringApplication에 붙여놨으니 spring이 동작할 때 timezone이 셋팅될 것 입니다.

 

 

2. jvm 실행할 때 KST 시간으로 변경하는 방법

jar파일을 실행할 때

java -Duser.timezone="Asia/Seoul" demo-0.0.1-SNAPSHOT.jar

이렇게 linux를 실행할 때 jvm의 argument를 KST로 넘겨서 jar파일을 실행하면 됩니다.

 

 

3. aws의 linux를 Timezone을 KST 시간으로 변경하는 방법

방법 1: dpkg-reconfigure 사용

$ sudo dpkg-reconfigure tzdata

img

방향키를 사용하여 원하는 지역을 찾은 뒤 Enter키를 누릅니다.


img

변경할 시간대를 찾아 Enter키를 누르시면 설정이 완료됩니다.

buffashe@buffashe-vm:~$ sudo dpkg-reconfigure tzdata

Current default time zone: 'Asia/Seoul'

Local time is now: Thu Feb 6 13:56:11 KST 2020.

Universal Time is now: Thu Feb 6 04:56:11 UTC 2020.

 

방법 2: timedatectl 사용

원하는 시간대를 검색합니다.

$ timedatectl list-timezones

$ timedatectl list-timezones | grep Seoul

Asia/Seoul

시간대를 변경합니다.

$ sudo timedatectl set-timezone Asia/Seoul

변경된 것을 확인할 수 있습니다.

buffashe@buffashe-vm:~$ timedatectl
                      Local time: Thu 2020-02-06 14:00:59 KST
                  Universal time: Thu 2020-02-06 05:00:59 UTC
                        RTC time: Thu 2020-02-06 05:01:01
                       Time zone: Asia/Seoul (KST, +0900)
       System clock synchronized: yes
systemd-timesyncd.service active: yes
                 RTC in local TZ: no
728x90