Changing system time using /bin/date
I'm trying to change system time via a script.
Basically the command date -s "<date>"
doesn't work since I run the command as user www-data. Although I edited /etc/sudoers
file to give root privileges to the user www-data, I still can't change or set time.
What could be the reason?开发者_StackOverflow社区
After editing a /etc/sudoers, do a
sudo date -s
command. This will do the actual date command from "Root" user.
Just edit of /etc/sudoers is not enough, because it will not give a root to user, but it will give a capability to became root from user using sudo
utility.
type
sudo visudo
add the next command
<username> ALL=(ALL) NOPASSWD: /bin/date
or
<username> ALL=(ALL) NOPASSWD: /usr/bin/timedatectl
now you can edit your time from the specific account without password like
sudo timedatectl set-time <datetime>
Also should note that line
Default requiretty
should becommented in sudoers file. Otherwise you will get a message to STDERR, saying
sudo: sorry, you must have a tty to run sudo
when trying to invoke the script from outer world.
This was a problem in my case.
精彩评论