我可以从 shell 运行以下命令,不会出现问题
sudo -u ktrl /home/andrew/.cargo/bin/ktrl -d /dev/input/event3
但是,当我尝试提供服务时:
/etc/systemd/system/ktrl.service
[Unit]
Description=ktrl
[Service]
User=ktrl
Environment=HOME=/opt/ktrl
ExecStart=/home/andrew/.cargo/bin/ktrl -d /dev/input/event3
[Install]
WantedBy=multi-user.target
我收到以下错误systemctl status ktrl.service
Loaded: loaded (/etc/systemd/system/ktrl.service; enabled; vendor preset: disabled)
Active: failed (Result: exit-code) since Sat 2020-06-27 10:35:08 EDT; 5s ago
Process: 23335 ExecStart=/home/andrew/.cargo/bin/ktrl -d /dev/input/event3 (code=exited, status=203/EXEC)
Main PID: 23335 (code=exited, status=203/EXEC)
CPU: 5ms
Jun 27 10:35:08 localhost.localdomain systemd[1]: Started ktrl.
Jun 27 10:35:08 localhost.localdomain systemd[23335]: ktrl.service: Failed to execute command: Permission denied
Jun 27 10:35:08 localhost.localdomain systemd[23335]: ktrl.service: Failed at step EXEC spawning /home/andrew/.cargo/bin/ktrl: Permission denied
Jun 27 10:35:08 localhost.localdomain systemd[1]: ktrl.service: Main process exited, code=exited, status=203/EXEC
Jun 27 10:35:08 localhost.localdomain systemd[1]: ktrl.service: Failed with result 'exit-code'.
我正在以安德鲁的身份执行命令sudo -u ktrl /home/andrew/.cargo/bin/ktrl -d /dev/input/event3
。但是,即使我切换到 ktrlsudo su ktrl
并运行命令/home/andrew/.cargo/bin/ktrl -d /dev/input/event3
它仍然有效
运行结果ll -d
:
在/home/andrew
drwxr-xr-x. 39 andrew andrew 4.0K Jun 27 12:06 .
在/home/andrew/.cargo
drwxrwxr-x. 4 andrew andrew 4.0K Jun 25 16:11 .
在/home/andrew/.cargo/bin
drwxrwxr-x. 2 andrew andrew 4.0K Jun 25 16:11 .
ls -l
运行结果/home/andrew/.cargo/bin
total 1552
-rwxrwxrwx. 1 andrew andrew 1579608 Jun 25 16:11 ktrl
答案1
该问题是由 SELinux 引起的。我禁用了它,现在一切正常。
编辑:更好的解决方案是将 bin 移动到/usr/local/bin/ktrl
并相应地更新 ktrl.service 文件。这样它仍然可以在启用 SELinux 的情况下工作。