1

Is it possible to change date only for a specific user (non-root) in Linux ?

If possible, How to do it ?

And it would be great if someone can point out the impact of doing so.

EDIT

Putting my question in other words .

I want to change the day and time from what it is now to another day and time in Linux without having root privileges.How can I achieve this ?

g4ur4v
  • 1,764
  • 8
  • 29
  • 34

3 Answers3

3

The date is a system-wide parameter that is crucial for the system (not the user). It appears in filesystem timestamps, logging, daemon operation, scheduling and so on. Many programs break or may even corrupt your system if the date is not accurate. So in this sense, the time has to be continuously increasing and correct not only locally, but if any non-www networking is done, also globally. Even certificates stop working if there is too much time difference between client and server.

So in this sense, the system date/time is only one - the actual date/time (preferably in UTC, otherwise your laptop's filesystem may break when you switch timezones).

On the other hand, the timezone setting is just a translation layer from UTC to the date-time that is presented to the user. That can be changed (see the comment by X Tian). But I don't know if that is what you want - it can only set an integer (sometimes fractions of integers) number of hours, never more than a day. It's technically possible to construct a custom timezone to induce arbitrary delay, but it's an ugly hack.

All basic manipulation of time still deals with the unix time (an integer that ticks uniformly from 1/1/1970), so the locale (timezone & stuff) is usually only used for visual display of time, or some very specific uses in scripts that rely on this functionality. For instance, the date calls clock_gettime function that retrieves the system time, and then calls localtime to translate into the current locale. The internal representation is always system time.

There may be special kernel modules to do that - or replacement libraries. You could create a corrupt library with adapted clock_gettime function and make it visible to one user. I have no idea how would this affect your system. The filesystem manipulation and stuff like that is still handled by the kernel, so the essentials would work. However, scheduling, backup facilities and makefiles may not work correctly.

EDIT: maybe what you want is a virtual machine. That is then more isolated and if you wanted to change the time, you could. It's sometimes useful to circumvent time-triggered software failures.

orion
  • 12,502
  • +1 for VMs, I agree that it can be very dangerous to corrupt the system's time, but a VM would be a great environment to set up something like this with its own time frame. – Eric Mar 21 '14 at 11:40
  • 1
    libfaketime is a workable solution in most cases, i.e. exporting LD_PRELOAD in the login shell. – mr.spuratic Mar 21 '14 at 12:21
  • @mr.spuratic exactly what I had in mind. – orion Mar 21 '14 at 12:56
  • Small beartrap with this approach, as you noted: filesystem timestamps will be "real", unless a process changes them directly via utime(). Sadly time is not handled by namespaces, though there was a UML related patch to unshare time, it was never merged. See http://lwn.net/Articles/180375/ – mr.spuratic Mar 21 '14 at 17:05
2

You can set their TZ environment variable, either in their profile or in the common profile specifically for those users.

$ date
Fri Mar 21 10:11:15 GMT 2014
$ export TZ="PST+8"
$ date
Fri Mar 21 02:11:53 PST 2014

The maximum value for TZ is 24 hours

X Tian
  • 10,463
  • Exporting TZ variable changes timezone. Can it change time also ? For example , If I want to change date from Mar 21 to Nov 21 in your example. – g4ur4v Mar 21 '14 at 10:19
  • Sorry this does nothing to change the time, it just changes how it is displayed. – jgm Mar 21 '14 at 14:53
  • OP has been edited since I answered this question, initially it sounded like one user was in a different timezone. Now it sounds like a CS question. Thanks, I know *nix works in UTC. – X Tian Mar 21 '14 at 15:13
1

There are only a few system calls which obtain the date/time, so you can put a preload library in place to alter the values returned from these calls depending on what it is you want to do (which is still somewhat unclear from the question). This was a common trick back when Y2K testing was at it's height.

Looks like https://github.com/wolfcw/libfaketime might do what you want, but if not it's simple enough to roll your own.

jgm
  • 111
  • This doesn't work with statically linked executables or suid programs. – X Tian Mar 21 '14 at 15:18
  • No it won't, and it is also relatively easy to abuse it. There are options here, but without more information about what you're trying to achieve it's very hard to provide further direction. – jgm Mar 21 '14 at 16:06