简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM