systemd : mkdir & ExecStartPre의 권한 문제


37

이 (단축 된) 시스템 서비스 파일에 문제가 있습니다.

[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

하자 음식은 사용자 이름과 수 FOO 이미 내 데몬 존재하는 그룹 이름 /usr/local/bin/FOOd.

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

...
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
...

왜 mkdir이 ExecStartPre에서 실패하고 어떻게 해결할 수 있습니까? (아니, mkdir에 sudo를 사용할 수 없습니다 ...)


참고 : 데비안 8
Matt

오류 메시지를 영어로 번역 해 주시겠습니까?
Thushi

1
... Jun 03 16:18:49 PC0515546 mkdir [2469] : / bin / mkdir : / var / run / FOOd / 디렉토리를 만들 수 없습니다 : 권한 없음 Jun 03 16:18:49 PC0515546 systemd [1] : FOOd.service : 제어 프로세스 종료, 코드 = 종료 상태 = 1 ...
Matt

답변:


56

당신은 추가해야합니다

PermissionsStartOnly=true

[Service]. FOOd물론 사용자 는에 디렉토리를 만들 권한이 없습니다 /var/run. 매뉴얼 페이지를 인용하려면 :

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


1
정말, 내가 찾던 것입니다.
robert

2
이 옵션은 명령 ExecReload=을 루트 권한 으로 실행합니다. 이것은 당신이 원하는 것이 아닐 수도 있습니다.
Rockallite

@Rockallite 그것이 내가 인용 한 문서가 문자 그대로 말한 것입니다.
embik 2016 년

2
PermissionsStartOnly더 이상 사용되지 않습니다. 참조 : github.com/NixOS/nixpkgs/issues/53852 지금 어떻게해야합니까?
adrelanos

1
@adrelanos 이제 +바로 다음에을 추가하십시오 ExecStartPre=. 예를 들어ExecStartPre=+/bin/mkdir test
Jamie Scott

28

이것은 권한 문제를 설명하거나 수정하는 답변이 아니지만 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

감사합니다 슬프게도 OpenVPN Ubuntu 패키지에서 누락되었습니다!
BaseZen

당사 사이트를 사용함과 동시에 당사의 쿠키 정책개인정보 보호정책을 읽고 이해하였음을 인정하는 것으로 간주합니다.
Licensed under cc by-sa 3.0 with attribution required.