bash 脚本 systemctl 在 CentOS7 上以非 root 用户身份启用服务
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32852250/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
bash script systemctl enable service as non root user on CentOS7
提问by Egon.Schiller
I have created a script that deploys java programms that have to run on startup. I wrote a litte template that is customised by the script and adds it into /etc/systemd/system/ServerName.service
我创建了一个脚本,用于部署必须在启动时运行的 Java 程序。我写了一个由脚本自定义的小模板并将其添加到 /etc/systemd/system/ServerName.service
[Unit]
Description= ServerName Java Daemon
[Service]
ExecStart=/usr/lib/jvm/java/bin/java -jar PathToFile.jar
User=psadmin
[Install]
WantedBy=multi-user.target # Similar to runlevel 5
I have added the user psadmin in visudo to run systemctl without beeing promted for the password. This works pretty fine for starting the service with sudo systemctl start ServerName.service. Then the script reloads the systemctl daemon, "sudo systemctl daemon-reload".
我在 visudo 中添加了用户 psadmin 来运行 systemctl 而不提示输入密码。这对于使用 sudo systemctl start ServerName.service 启动服务非常有效。然后脚本重新加载 systemctl 守护进程“sudo systemctl daemon-reload”。
When the script executes the part with systemctl enable ServerName.service, I get in return "Failed to issue method call:Access denied".
当脚本使用 systemctl enable ServerName.service 执行部分时,我得到的回报是“无法发出方法调用:拒绝访问”。
When I do this by hand under the same user context I get in return "Failed to issue method call: Invalid argument", but it works and the service is enabled. Thanks for any hints!
当我在同一用户上下文下手动执行此操作时,我得到的回报是“无法发出方法调用:无效参数”,但它可以工作并且服务已启用。感谢您的任何提示!
回答by Marc Young
Systemctl is protected from non-privileged users. You will have to modify sudoers to allow this user or a group (that the user is a part of) to execute the command.
Systemctl 受到非特权用户的保护。您必须修改 sudoers 以允许此用户或组(用户所属的组)执行命令。
Also this is a better question for ServerOverflow most likely. See the manpage for sudo
这对于 ServerOverflow 来说也是一个更好的问题。请参阅 sudo 的联机帮助页
%wheel = /usr/bin/systemctl restart ServerName.service
%wheel = /usr/bin/systemctl stop ServerName.service
%wheel = /usr/bin/systemctl start ServerName.service
%wheel = /usr/bin/systemctl status ServerName.service