ユーザーグループにカスタムサービスを開始/停止する権限を付与する方法は?

ユーザーグループにカスタムサービスを開始/停止する権限を付与する方法は?

私のルートディレクトリにサービスがあり、ユーザーグループにサービスを実行するための管理者権限を与えたいと思います。

このサービスは次に存在します。/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新しいルールがすぐに適用されます。

おすすめ記事