简体   繁体   English

使用/ bin / date更改系统时间

[英]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. 基本上命令date -s "<date>"不起作用,因为我以用户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. 虽然我编辑/etc/sudoers文件给用户www-data赋予root权限,但我仍然无法更改或设置时间。

What could be the reason? 可能是什么原因?

After editing a /etc/sudoers, do a 编辑/ etc / sudoers后,执行一个

 sudo date -s

command. 命令。 This will do the actual date command from "Root" user. 这将从“Root”用户执行实际日期命令。

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. 仅仅编辑/ etc / sudoers是不够的,因为它不会给用户提供root权限,但是它将提供使用sudo实用程序从用户成为root用户的能力。

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. 应该在sudoers文件中注明。 Otherwise you will get a message to STDERR, saying 否则你会收到一条消息给STDERR,说

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. 这是我的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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