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

Allowing a non-root user to restart a service

· 52CE 我爱测

Background : I created an app called myapp with Spring-boot . It consists of a self-executable jar and is compatible with systemd services. Now, I am trying to integrate it with jenkins. What I want: I want jenkins to be able to …

问题原文

原帖链接 · Arnaud Denoyelle · CC BY-SA 3.0

Background :

I created an app called myapp with Spring-boot. It consists of a self-executable jar and is compatible with systemd services. Now, I am trying to integrate it with jenkins.

What I want:

I want jenkins to be able to :

  • stop the service.
  • replace the jar.
  • restart the service.

Problem:

Up to now, only sudoers can start/stop services. I don't want jenkins to be a sudoer (it seems messy).

Current structure:

I have a user myapp which has a /home/myapp folder. The generated jar is called myapp and is placed at /home/myapp. The user myapp is the owner of the generated jar:

myapp@myserver:~/backend$ ll
total 53900
drwxrwxr-x 2 myapp myapp     4096 Apr 25 17:09 ./
drwxr-xr-x 6 myapp myapp     4096 Apr 25 17:08 ../
-rw-rw-r-- 1 myapp myapp      511 Apr 20 16:13 application.properties
-rwxr--r-- 1 myapp myapp 55175294 Apr 20 19:06 backend-1.0-SNAPSHOT.jar*
lrwxrwxrwx 1 myapp myapp       24 Apr 20 19:20 myapp -> backend-1.0-SNAPSHOT.jar*
-rw-r--r-- 1 myapp myapp      179 Apr 20 19:26 myapp.service

I placed a ssh key so that jenkins can log as myapp@myserver.

As myapp is the owner of the jar, I think that there might be an option that allows the user myapp to call systemctl start/stop myapp. Actually, I can call systemctl status myapp but not start/stop (root password is asked).

Any suggestions?

已采纳解答

原帖链接 · brent · CC BY-SA 3.0

sudo is the way to go. Create a new group (appadmin for example), put your jenkins user in it, and using visudo add a new entry with a limited list of commands, for example:

Cmnd_Alias MYAPP_CMNDS = /bin/systemctl start myapp, /bin/systemctl stop myapp
%appadmin ALL=(ALL) MYAPP_CMNDS

If you want the appadmin group to be able to operate the service without entering a password first (useful if the user is only authenticated by an SSH key for example),

Cmnd_Alias MYAPP_CMNDS = /bin/systemctl start myapp, /bin/systemctl stop myapp
%appadmin ALL=(ALL) NOPASSWD: MYAPP_CMNDS

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

资料来源与适用范围

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

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

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