Linux quick tips – setting time on Virtual Machine

There are several good reasons why I abandoned solutions such as XAMPP long time ago (more on that in some other post). I now use Debian and Ubuntu virtual machines as my development servers. I use VMware Workstation as my platform and no, I don’t use Vagrant. This setup works great, but it has one particularly annoying drawback.

Each time you put your (host) computer to sleep and turn it back again, the clock on your Linux server will stay on the time it was set when you powered down. It will simply “unfreezes” when you turn on your computer. VMware is supposed to have solution for this – VMware tools works great on Windows virtual machines, but for some reason that solution does not work for Debian and Ubuntu.

Having to run a command such as:

sudo date -s "2015-09-30 21:04"

each time you power on your computer is simply unthinkable and so is this:

sudo ntpdate pool.ntp.orgĀ 

although there is a bit less typing. Once you’ve installed ntpdate you can put the previous command in a script (yes, together with “sudo”) and run that each time, but even that is too tedious. Most of us developers are too lazy for that.

There is one solution that is not too hard. When Bash shell is started, that is when you log on to your Linux server, several scripts are executed, among which are ~/.bash_profile, ~/.bash_login and ~/.profile. The last one is not executed if one of the first two is present in your home folder. Any one of those three is a good place for the “sudo ntpdate” command.

There are of course some more advanced, fully automated solutions, such as installing NTP daemon on the Linux server, or querying the Windows host machine via SAMBA, but for me the described solution works. The only downside is having to type in the password twice when logging on (once for logon and once for sudo), but I can live with that (for now).

Read More