Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


How to set time in OpenVZ container
New on LowEndTalk? Please Register and read our Community Rules.

How to set time in OpenVZ container

I want to share my experience of solving time problem inside OpenVZ container.

This article is not related to KVM containers, they should have no problems with time.

OpenVZ container is running on same Linux kernel as host machine, and OpenVZ container is very well isolated from host system and cannot change many important system-wide parameters.



Problem #1: Bad container timezone

Time is wrong, difference can be measured in hours.

'date' command - result: Wed Mar 8 10:15:05 GMT 2017

'date' command - expected result: Wed Mar 8 15:15:05 GMT+5 2017

Solution: 'dpkg-reconfigure tzdata'.

System clock keep running in UTC time, your container timezone is changed, you see correct time in your console but always can check original UTC clock (with command 'date -u' or any other way).



Problem #2: Bad system time

Time is wrong, difference can be measured in minutes.

'date' command - result: Wed Mar 8 10:15:05 GMT 2017

'date' command - expected result: Wed Mar 8 10:37:05 GMT 2017

'ntpdate time.nist.gov' - result: Operation not permitted

'date -s 10:37' - result: Operation not permitted



Solution 1: Ask your provider to allow you to change system time.

Google quickly finds a command like "vzctl set 101 --capability sys_time:on --save". But this is bad solution because your container will be able to set whole system time (for other containers, too). Your provider should not give you this option.



Solution 2: Ask your provider to fix time on host system. The best variant but depends on provider support quality. Solution can take some time.



Solution 3: Do it yourself. Use libfaketime

Most google links say that it's impossible to change time inside OpenVZ container. But you can change time for your important applications, even for shell. You can use special library for any application and it will tune time to any value. Link is here: https://github.com/wolfcw/libfaketime, there is complete instruction about installation and usage.

Proof:

Thanked by 1vpsGOD

Comments

  • WSSWSS Member

    Just what we need, library injections to deal with a common problem of hosts being too lazy to setup NTP before going to production.

    Thanked by 2ucxo sin
  • MaouniqueMaounique Member
    edited March 2017

    Or you can link to your timezone:

    rm -rf /etc/localtime
    ln -s /usr/share/zoneinfo/Europe/Bucharest /etc/localtime

    WSS said: Just what we need, library injections to deal with a common problem of hosts being too lazy to setup NTP before going to production.

    I am afraid you did not read the post. Time is accurate, just the timezone does not fit, which is expected, you cannot get international customers from only one timezone, in most cases.

    Thanked by 1dfmcvn
  • WSSWSS Member

    @Maounique said:
    I am afraid you did not read the post. Time is accurate, just the timezone does not fit, which is expected, you cannot get international customers from only one timezone, in most cases.

    No, you're right. I didn't read the post, nor check the code, and see that it basically runs strtotime() on an environmental variable. Please enlighten me, since time_t can be computed by localtime or GMT, and generally the systems are setup appropriately to do so, even if they're not syncronized.

  • @WSS said:
    No, you're right. I didn't read the post, nor check the code, and see that it basically runs strtotime() on an environmental variable. Please enlighten me, since time_t can be computed by localtime or GMT, and generally the systems are setup appropriately to do so, even if they're not syncronized.

    You havent read my post either.

  • WSSWSS Member

    I guess I was being too subtle about making fun of you. Have a nice preload.

    Thanked by 1brueggus
  • adwsislifeadwsislife Member
    edited November 2022

    @user6752 said:
    Solution: dpkg-reconfigure tzdata

    Initially I was thinking to use sudo timedatectl set-timezone but I was unsure whether it will work or not.
    This thread helped me to do it properly with command sudo dpkg-reconfigure tzdata
    Thank you for this thread :)

    Thanked by 1emg
  • @adwsislife said:

    @user6752 said:
    Solution: dpkg-reconfigure tzdata

    Initially I was thinking to use sudo timedatectl set-timezone but I was unsure whether it will work or not.
    This thread helped me to do it properly with command sudo dpkg-reconfigure tzdata
    Thank you for this thread :)

    Holy necropost

    Not to mention that the OP is long gone

    Please read the rules

    Thread closed

This discussion has been closed.