16

I have three Ubuntu EC2 instances on Amazon AWS (EU/Ireland), all set up as web server. I noticed the system date/time is incorrect in two of them (timezone is always set to UTC for my choice). Here is the output of date -R for my PC (EU/Italy, 2 hours ahead of UTC) and for the servers:

lorenzo@LOCALPC ~ $ date -R
Wed, 05 Jun 2013 09:48:19 +0200

ubuntu@AWS-EC2-1 ~ $ date -R # SHOULD BE 07:48 (-10 minutes)
Wed, 05 Jun 2013 07:38:58 +0000

ubuntu@AWS-EC2-2 ~ $ date -R # SHOULD BE 07:48 (-15 minutes)
Wed, 05 Jun 2013 07:33:55 +0000

ubuntu@AWS-EC2-3 ~ $ date -R # SHOULD BE 07:48 (-50 secs)
Wed, 05 Jun 2013 07:47:29 +0000

As you can see, difference is significant for the first and second EC2 server. I tried using ntpdate, but:

ubuntu@AWS-EC2-1 ~ $ ntpdate
 5 Jun 07:42:10 ntpdate[3583]: no servers can be used, exiting

ubuntu@AWS-EC2-1 ~ $ cat /etc/default/ntpdate
# The settings in this file are used by the program ntpdate-debian, but not
# by the upstream program ntpdate.

# Set to "yes" to take the server list from /etc/ntp.conf, from package ntp,
# so you only have to keep it in one place.
NTPDATE_USE_NTP_CONF=yes

# List of NTP servers to use  (Separate multiple servers with spaces.)
# Not used if NTPDATE_USE_NTP_CONF is yes.
NTPSERVERS="ntp.ubuntu.com"

# Additional options to pass to ntpdate
NTPOPTIONS=""

Reading first comment from the configuration file I just pasted, I tried ntpdate-debian:

ubuntu@AWS-EC2-1 ~ $ sudo ntpdate-debian 
 5 Jun 07:51:58 ntpdate[3619]: step time server 91.189.94.4 offset 561.511643 sec

The ~560sec offset correspond to the ~10min I found before. After that command the system time is ok. It looks strange to me that the system does not do that automatically. Should I use a cron to do it?!? Am I missing something?

lorenzo-s
  • 347
  • 4
  • 10
  • 19
  • I found this forum thread informative if you read all the way to the end: https://forums.aws.amazon.com/thread.jspa?messageID=364749 – odigity Jul 16 '14 at 17:51
  • If one got "the NTP socket is in use, exiting" when running `ntpdate`, this answer (https://superuser.com/a/639516/562405) is useful. – drodata May 19 '17 at 04:41

2 Answers2

7

EC2 instances should take their time from the host but there are occasional problems with that. If you configure ntp correctly it should resolve this.

Some more instructions here: http://support.rightscale.com/06-FAQs/FAQ_0025_-_Can_I_use_Network_Time_Protocol_(NTP)_on_my_RightScale_servers%3F

Colin Pickard
  • 1,260
  • 2
  • 16
  • 28
1

Amazon provides Amazon Time Sync Service which eliminates the time drift you are experiencing. The overall steps to take on each EC2 instance are:

  1. Remove NTP using sudo apt-get remove ntp
  2. Install Chrony using sudo apt install chrony
  3. use sudo vim /etc/chrony/chrony.conf to edit the file to include server 169.254.169.123 prefer iburst
  4. Restart Chrony using sudo /etc/init.d/chrony restart
  5. Verify Chrony is using the correct IP using chronyc sources -v and verifying that the output includes '^* 169.254.169.123'
  6. Identify the best timezone to use with sudo timedatectl list-timezones
  7. Update the timezone from UTC using the path from the previous step, such as sudo timedatectl set-timezone America/Chicago
enharmonic
  • 176
  • 1
  • 9