systemd: mkdir 및 ExecStartPre의 권한 문제

systemd: mkdir 및 ExecStartPre의 권한 문제

이 (축약된) systemd 서비스 파일에 문제가 있습니다.

[Unit]
Description=control FOO daemon
After=syslog.target network.target

[Service]
Type=forking
User=FOOd
Group=FOO
ExecStartPre=/bin/mkdir -p /var/run/FOOd/
ExecStartPre=/bin/chown -R FOOd:FOO /var/run/FOOd/
ExecStart=/usr/local/bin/FOOd -P /var/run/FOOd/FOOd.pid
PIDFile=/var/run/FOOd/FOOd.pid

[Install]
WantedBy=multi-user.target

허락하다음식사용자 이름은 이고내 데몬에 이미 존재하는 그룹 이름입니다 /usr/local/bin/FOOd.

/var/run/FOOd/데몬을 시작하기 전에 디렉토리를 생성 해야 합니다 . mkdir이 권한으로 인해 디렉터리를 생성할 수 없기 때문에 실패합니다./usr/local/bin/FOOd# systemctl start FOOd.service

...
Jun 03 16:18:49 PC0515546 mkdir[2469]: /bin/mkdir: cannot create directory /var/run/FOOd/: permission denied
Jun 03 16:18:49 PC0515546 systemd[1]: FOOd.service: control  process exited, code=exited status=1
...

ExecStartPre에서 mkdir이 실패하는 이유와 해결 방법은 무엇입니까? (아니요, sudo를 사용하여 mkdir을 실행할 수 없습니다...)

답변1

당신은 추가해야

PermissionsStartOnly=true

도착하다 [Service]. 귀하의 사용자는 확실히 .:의 매뉴얼 페이지를 참조할 FOOd권한이 없습니다 ./var/run

부울 매개변수를 사용합니다. true인 경우 User= 및 유사한 옵션(자세한 내용은 systemd.exec(5) 참조)으로 구성된 권한 관련 실행 옵션은 ExecStart=로 시작된 프로세스에만 적용되며 다양한 다른 ExecStartPre=, ExecStartPost=, ExecReload=, ExecStop=에는 적용되지 않습니다. 및 ExecStopPost= 명령. false인 경우 설정된 모든 명령에 동일한 방식으로 설정이 적용됩니다. 기본값은 거짓입니다.

답변2

이는 권한 문제를 설명하거나 해결하는 답변은 아니지만, 그냥 systemds RuntimeDirectory 옵션을 사용해야 한다고 생각합니다. 인용하다매뉴얼 페이지:

RuntimeDirectory=, RuntimeDirectoryMode=
       Takes a list of directory names. If set, one or more directories by
       the specified names will be created below /run (for system
       services) or below $XDG_RUNTIME_DIR (for user services) when the
       unit is started, and removed when the unit is stopped. The
       directories will have the access mode specified in
       RuntimeDirectoryMode=, and will be owned by the user and group
       specified in User= and Group=. Use this to manage one or more
       runtime directories of the unit and bind their lifetime to the
       daemon runtime. The specified directory names must be relative, and
       may not include a "/", i.e. must refer to simple directories to
       create or remove. This is particularly useful for unprivileged
       daemons that cannot create runtime directories in /run due to lack
       of privileges, and to make sure the runtime directory is cleaned up
       automatically after use. For runtime directories that require more
       complex or different configuration or lifetime guarantees, please
       consider using tmpfiles.d(5).

따라서 해야 할 일은 서비스 파일을 다음으로 변경하는 것뿐입니다.

[Unit]
Description=control FOO daemon
After=syslog.target network.target

[Service]
Type=forking
User=FOOd
Group=FOO
RuntimeDirectory=FOOd
RuntimeDirectoryMode=$some-mode
ExecStart=/usr/local/bin/FOOd -P /run/FOOd/FOOd.pid
PIDFile=/run/FOOd/FOOd.pid

[Install]
WantedBy=multi-user.target

답변3

+전체 권한으로 실행하려는 명령 앞에 추가하세요.

예를 들어:

ExecStartPre=+/bin/mkdir test

"특수 실행 파일 접두사" 섹션을 참조하세요.https://www.freedesktop.org/software/systemd/man/systemd.service.html#ExecStart=

관련 정보