如何在 AmazonEC2中设置时区?

我想改变时区设置在我的亚马逊 EC2实例运行 Ubuntu Linux 到本地时间?

我的问题

如何更改 AmazonEC2中的时区?

106158 次浏览

it should be no different than your desktop Ubuntu process. See here

  1. SSH to your EC2 server
  2. execute the following (to set timezone to Australia/Adelaide)

    $ echo "Australia/Adelaide" | sudo tee /etc/timezone
    Australia/Adelaide
    $ sudo dpkg-reconfigure --frontend noninteractive tzdata
    
    
    Current default time zone: 'Australia/Adelaide'
    Local time is now:      Sat May  8 21:19:24 CST 2010.
    Universal Time is now:  Sat May  8 11:49:24 UTC 2010.
    

Update

You can use tzselect utility to browse through. See here: http://manpages.ubuntu.com/manpages/precise/man1/tzselect.1.html

It's an interactive software. My Ubuntu (11.10) has it.

You could also refer this Wikipedia article

Brazil

Brazil/Acre
Brazil/DeNoronha
Brazil/East
Brazil/West

Another way of changing the time (This was done on an Amazon EC2 Linux instance)

Remove your localtime file

sudo rm /etc/localtime

Change Directory to ZoneInfo

cd /usr/share/zoneinfo

This folder contains all of the timezone information. You then just need to softlink to the appropriate zone.

Create a softlink to /etc/localtime

sudo ln -s /usr/share/zoneinfo/GB /etc/localtime

That will change your server timezone to GB

None of the above steps worked for me, so thought of writing this new answer here

# Delete any existing localtime link
sudo rm /etc/localtime
# Update time clock file with ZONE property
sudo vi /etc/sysconfig/clock
#Update the ZONE property to what you want say
ZONE="America/Los_Angeles"
sudo ln -sf /usr/share/zoneinfo/America/Los_Angeles /etc/localtime
sudo reboot

Information is available in aws documentation for changing the time zone in ec2 machine .Please find below the details :

Changing the Time Zone

Amazon Linux instances are set to the UTC (Coordinated Universal Time) time zone by default, but you may wish to change the time on an instance to the local time or to another time zone in your network.

To change the time zone on an instance

1.Identify the time zone to use on the instance. The /usr/share/zoneinfo directory contains a hierarchy of time zone data files. Browse the directory structure at that location to find a file for your time zone.

[ec2-user ~]$ ls /usr/share/zoneinfo
Africa      Chile    GB         Indian       Mideast   posixrules  US
America     CST6CDT  GB-Eire    Iran         MST       PRC         UTC
Antarctica  Cuba     GMT        iso3166.tab  MST7MDT   PST8PDT     WET
Arctic      EET      GMT0       Israel       Navajo    right       W-   SU
...

Some of the entries at this location are directories (such as America), and these directories contain time zone files for specific cities. Find your city (or a city in your time zone) to use for the instance. In this example, you can use the time zone file for Los Angeles, /usr/share/zoneinfo/America/Los_Angeles.

2.Update the /etc/sysconfig/clock file with the new time zone.

a.Open the /etc/sysconfig/clock file with your favorite text editor (such as vim or nano). You need to use sudo with your editor command because /etc/sysconfig/clock is owned by root.

b.Locate the ZONE entry, and change it to the time zone file (omitting the /usr/share/zoneinfo section of the path). For example, to change to the Los Angeles time zone, change the ZONE entry to the following.

ZONE="America/Los_Angeles"

c.Save the file and exit the text editor.

3.Create a symbolic link between /etc/localtime and your time zone file so that the instance finds the time zone file when it references local time information.

[ec2-user ~]$ sudo ln -sf /usr/share/zoneinfo/America/Los_Angeles /etc/localtime

4.Reboot the system to pick up the new time zone information in all services and applications.

[ec2-user ~]$ sudo reboot

Check your current time zone by

$ date

To change it, run

$ sudo dpkg-reconfigure tzdata

This will show list of geographical areas. Select to narrow down available time zones. Next select city/timezone. And you have changed system to new timezone.

from RHEL 7 admin guide

Seems this might be the command on REDHAT7

timedatectl set-timezone "Europe/Stockholm"

To get all available timezones type:

timedatectl list-timezone

for more informations use

man timedatectrl

NOTE: This refers to a linux box (debian in my instance) should be used under your AWS launch configurations "User Data".

If you're planning to set the TIMEZONE on instance boot use below (works like a charm) use you're own Country/City instead of "Australia/Sydney".

#!/bin/bash


/bin/rm -f /etc/localtime; /bin/ln -s /usr/share/zoneinfo/Australia/Sydney /etc/localtime

Locate your timezone in the directory /usr/share/zoneinfo/.

E.g. To change timezone for Toronto /usr/share/zoneinfo/America/Toronto

Symlink your timezone to /etc/localtime e.g. for Toronto:

sudo ln -sf /usr/share/zoneinfo/America/Toronto /etc/localtime

Reboot to finish

reboot

UPDATED ANSWER FOR UBUNTU 16.04:

Do:

sudo timedatectl set-timezone America/New_York

to update your timezone.

To list down all the available timezones you can do:

timedatectl list-timezones

When you create a new EC2 instance and select "Amazon Linux 2 AMI (HVM)" as Amazon Machine Image (AMI), you can insert these lines in the "User Data" section. Based on which region you are in, change the second line with the timezone that works for you. I am in Central European Time (CET). Bear in mind that this bootstrap code is executed as root and only once when the instance is created.

#!/bin/bash
rm -rf /etc/localtime
ln -s /usr/share/zoneinfo/CET /etc/localtime

If you want to see all the other timezones available, ssh to one instance and run:

ls /usr/share/zoneinfo

For Red Hat or CentOS:

Check the time:

timedatectl status

List all available timezones:

timedatectl list-timezones

Set up your timezone:

sudo timedatectl set-timezone Europe/Paris