我想在 Ubuntu 中以非特权用户身份运行应用程序作为服务。以下是创建用户的命令:
sudo groupadd -r mygroup
sudo useradd -r -s /bin/false -g mygroup myuser
sudo chown -R mygroup:myuser /path/to/my/app
以下是我的服务文件:
[Unit]
Description=Myapp server
After=syslog.target
After=network.target
[Service]
WorkingDirectory=/path/to/my/app
ExecStart=/usr/bin/java -jar /path/to/my/app/myapp.jar
User=myuser
Type=simple
StandardOutput=syslog
StandardError=syslog
SuccessExitStatus=143
TimeoutStopSec=120
Restart=always
[Install]
WantedBy=multi-user.target
但是,当我启动该服务时,日志中出现以下错误:
Changing to the requested working directory failed: Permission denied
Failed at step CHDIR spawning /usr/bin/java: Permission denied
我尝试将用户添加到 sudo 组并将sudo usermod -aG sudo myuser
sudo 添加到 ExecStart,但还是不起作用。
Changing to the requested working directory failed: Permission denied
Failed at step CHDIR spawning sudo: Permission denied
答案1
chown 的概要如下:
chown [OPTION]... [OWNER][:[GROUP]] FILE...
您为其提供了 mygroup:myuser 作为参数,这是错误的。使用:
sudo chown -R myuser:mygroup /path/to/my/app