大网数据广告活动
服务器运维问答

Grant a user sudo powers to start/stop/etc a systemd service w/o sudo password

· 52CE 我爱测

I have one non-root user, and I would like to give them sudo permission to start & stop (etc.) a systemd service file without needing a password. I tried to just make the following file in /etc/sudoers.d/servicename (where [usern…

问题原文

原帖链接 · Amandasaurus · CC BY-SA 4.0

I have one non-root user, and I would like to give them sudo permission to start & stop (etc.) a systemd service file without needing a password.

I tried to just make the following file in /etc/sudoers.d/servicename (where [username] and [servicename] are the actual names):

[username] ALL=(root) NOPASSWORD: systemctl start [servicename]
[username] ALL=(root) NOPASSWORD: systemctl stop [servicename]
[username] ALL=(root) NOPASSWORD: systemctl restart [servicename]

(repeated for lots of system commands, like reload/try-restart/enable/…)

But visudo told me that was a syntax error. How can I fix that?

I want to put in scripts (which are being run as this non-root user) sudo systemctl stop [servicename].

Ubuntu Linux 18.04, systemd from ubuntu (which for this is v245). I also want this to work on Ubuntu 20.04.

已采纳解答

原帖链接 · João Alves · CC BY-SA 4.0

You can implement what you want by using the following:

Cmnd_Alias USER_SERVICES = /usr/bin/systemctl start [servicename], /usr/bin/systemctl stop [servicename], /usr/bin/systemctl restart [servicename]

[username] ALL=(ALL) NOPASSWD:USER_SERVICES

You can add more commands to USER_SERVICES by placing a comma (,) between them. Always use the full path for the command.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2021-03-18)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。