사용자 그룹에 사용자 정의 서비스를 시작/중지할 수 있는 권한을 부여하는 방법은 무엇입니까?

사용자 그룹에 사용자 정의 서비스를 시작/중지할 수 있는 권한을 부여하는 방법은 무엇입니까?

내 루트 디렉터리에 서비스가 있고 사용자 그룹에 서비스를 실행할 수 있는 관리자 권한을 부여하고 싶습니다.

이 서비스는 다음에 존재합니다./root/home/custom_service/service.service

나는 chgrp admin ./home/custom_service/노력했다chmod g+rx ./home/custom_service/

권한을 확인 ls -l ./home/custom_service/하면-rw-r-xr-- 1 root admin 449 May 30 11:23 service.service

내 testUsr 계정(그룹 관리자에 있음)에서 서비스를 실행하려고 하면 결과는 다음과 같습니다.

난 달린다:

systemctl start service.service

결과:

==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-units ===
Authentication is required to manage system services or units.
Authenticating as: scarycall
Password:
polkit-agent-helper-1: pam_authenticate failed: Permission denied
==== AUTHENTICATION FAILED ===
Failed to start service.service: Access denied
See system logs and 'systemctl status service.service' for details.

참고: 루트에서 서비스를 실행할 수 있습니다.


고쳐 쓰다:

이것은 내 polkit 규칙 파일입니다.

Array.prototype.includes = function(variable) {
    for (var i = 0; i < this.length; i++) { if (this[i] === variable) { return true; } }
    return false;
}

polkit.addRule(function(action, subject) {
    var allowed = {
        units: [
            // Here you can add units that you want to allow admin users to manage.
            "service.service"
        ],
        actions: [
            "org.freedesktop.systemd1.manage-units"
        ],
        verbs: [
            "start", "stop", "restart"
        ]
    }
    var unit_name = action.lookup("unit");
    
    polkit.log("Action" + action);
    polkit.log("Unit=" + unit_name);
    polkit.log("Action ID=" + action.id);
    polkit.log("Verb=" + action.lookup("verb"));
    polkit.log("Subject=" + subject);
    
    if (allowed.actions.includes(action.id) &&
        allowed.units.includes(unit_name) &&
        allowed.verbs.includes(action.lookup("verb")) &&
        subject.isInGroup("admin")
    ) {
        return polkit.Result.YES;
    }
});

내가 실행 중인 시스템에는 작업을 통해 단위나 동사를 전달하지 않는 systemd 버전 219가 있습니다. 이 규칙의 로그는 다음과 같습니다.

/etc/polkit-1/rules.d/10-insight-service.rules:23: Action[Action id='org.freedesktop.systemd1.manage-units
/etc/polkit-1/rules.d/10-insight-service.rules:24: Unit=undefined
/etc/polkit-1/rules.d/10-insight-service.rules:25: Action ID=org.freedesktop.systemd1.manage-units
/etc/polkit-1/rules.d/10-insight-service.rules:26: Verb=undefined
/etc/polkit-1/rules.d/10-insight-service.rules:27: Subject=[Subject pid=13762 user='testUsr' groups=admin seat='' session='2072' local=false active=true]

단위 및 동사 세부정보는 다음과 같이 v226까지 추가되지 않았습니다. https://github.com/systemd/systemd/commit/88ced61bf9673407f4b15bf51b1b408fd78c149d

해결하다: 내가 실행 중인 시스템은 이전 버전의 systemd를 실행하기 때문에 작업에 대한 단위 또는 동사 세부 정보를 지원하지 않습니다. 그래서 저는 sudo 권한을 사용하기로 결정했고 효과가 있었습니다.

sudoer 파일에 다음을 추가했습니다.

%admin ALL= NOPASSWD: /bin/systemctl start service.service
%admin ALL= NOPASSWD: /bin/systemctl stop service.service
%admin ALL= NOPASSWD: /bin/systemctl restart service.service
%admin ALL= NOPASSWD: /bin/systemctl status service.service

답변1

admin그룹의 사용자가 특정 시스템 장치를 시작, 중지 또는 다시 시작할 수 있도록 허용하는 Polkit 정책을 만들 수 있습니다 . 예를 들어.

// Allow user in admin group to manage specific systemd units
Array.prototype.includes = function(variable) {
    for (var i = 0; i < this.length; i++) { if (this[i] === variable) { return true; } }
    return false;
}

polkit.addRule(function(action, subject) {
    var allowed = {
        units: [
            // Here you can add units that you want to allow admin users to manage.
            "backup.service",
            "nginx.service",
            "backup-rotate.service"
        ],
        actions: [
            "org.freedesktop.systemd1.manage-unit-files"
        ],
        verbs: [
            "start", "stop", "restart"
        ]
    }
    var unit_name = action.lookup("unit");
    if (allowed.actions.includes(action.id) &&
        allowed.units.includes(unit_name) &&
        allowed.verbs.includes(action.lookup("verb")) &&
        subject.isInGroup("admin")
    ) {
        return polkit.Result.YES;
    }
});

정책 파일을 다른 이름으로 저장하면 /etc/polkit-1/rules.d/60-units.rules새 규칙이 즉시 적용됩니다.

관련 정보