ユーザーがパスワードなしでsystemctl / systemdサービスを実行できるようにする

ユーザーがパスワードなしでsystemctl / systemdサービスを実行できるようにする

appSevice Linuxには、次のコマンドを使用して起動および停止すると機能するサービスがあります。

sudo systemctl start appSevice.service;
sudo systemctl stop  appSevice.service;

しかし、JAVAコードでこれを実行しようとすると、次のようになります。

Runtime.getRuntime().exec(new String[]{"systemctl", "stop", "appService.service"});

...動作しません。次のエラーが発生します。

>  Failed to stop appService.service: Interactive authentication required

これは私のサービスです。

[Service]
Type=simple
ExecStart=/opt/soft/v1/launchAppService.ksh start
User=Jms-User
Restart=on-abort

このエラーを防ぎ、パスワードを提供せずにサービスを実行する方法はありますか?

ベストアンサー1

3つの方法があります。

  1. 糸を入れて抜いてくださいappService.service。その後、次のように制御できます。~/.config/systemd/system/User=
systemctl --user start appService.service
systemctl --user stop appService.service
  1. ポルケットルールを追加します。私の考えでは、この質問はあなたが探している質問に非常に近いです。systemdは、グループ内の権限のないユーザーで始まります。。 debian/ubuntu(polkit < 106)を使用している場合は、次のようになります。
/etc/polkit-1/localauthority/50-local.d/service-auth.pkla
---
[Allow yourname to start/stop/restart services]
Identity=unix-user:youname
Action=org.freedesktop.systemd1.manage-units
ResultActive=yes

Arch / Redhat(polkit> = 106)を使用している場合は、次のことが機能します。

/etc/polkit-1/rules.d/service-auth.rules
---
polkit.addRule(function(action, subject) {
    if (action.id == "org.freedesktop.systemd1.manage-units" &&
        subject.user == "yourname") {
        return polkit.Result.YES;
    } });
  1. sudosudo依存関係NOPASSWD:の設定が不要で間接的に呼び出されるように設計されていないため、これはあまり好きではありません。これがPolkitが設計された理由です。
/etc/sudoers.d/sysctl
---
youname ALL = NOPASSWD: /bin/systemctl

これがデプロイしたいソフトウェアであれば、確かにpolkitソリューションを使用してグループ化します(リンクされた回答に従って)。つまり、ユーザー名をハードコードする必要はありませんが、お気に入りのユーザーをグループに追加して機能を取得できます。

おすすめ記事