Jump to content

Sudo/zh: Difference between revisions

From NixOS Wiki
Ardenet (talk | contribs)
Created page with "通过简单的配置,将允许属于 <code>wheel</code> 组的所有用户以超级用户身份且无需提供该用户的密码使用 <code>sudo</code> 执行 <code>extraRules</code> 中指定的命令。"
Tags: Mobile edit Mobile web edit
Ardenet (talk | contribs)
Created page with "== 用法 == 为示例用户 <code>myuser</code> 启用 sudo。<syntaxhighlight lang="nix"> users.users.myuser.extraGroups = [ "wheel" ]; </syntaxhighlight>"
 
Line 1: Line 1:
[https://www.sudo.ws Sudo] 允许系统管理员委托权限,授予某些[[Special:MyLanguage/User Management|用户]](或用户组)以 root 或其他用户身份运行命令的能力,同时提供命令及其参数的审计跟踪。
[https://www.sudo.ws Sudo] 允许系统管理员委托权限,授予某些[[Special:MyLanguage/User Management|用户]](或用户组)以 root 或其他用户身份运行命令的能力,同时提供命令及其参数的审计跟踪。
<div lang="en" dir="ltr" class="mw-content-ltr">
== 用法 ==
== Usage ==
为示例用户 <code>myuser</code> 启用 sudo。<syntaxhighlight lang="nix">
Enable sudo-usage for the example user <code>myuser</code>.<syntaxhighlight lang="nix">
users.users.myuser.extraGroups = [ "wheel" ];
users.users.myuser.extraGroups = [ "wheel" ];
</syntaxhighlight>
</syntaxhighlight>
</div>
通过简单的配置,将允许属于 <code>wheel</code> 组的所有用户以超级用户身份且无需提供该用户的密码使用 <code>sudo</code> 执行 <code>extraRules</code> 中指定的命令。
通过简单的配置,将允许属于 <code>wheel</code> 组的所有用户以超级用户身份且无需提供该用户的密码使用 <code>sudo</code> 执行 <code>extraRules</code> 中指定的命令。
<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">

Latest revision as of 12:14, 8 October 2025

Sudo 允许系统管理员委托权限,授予某些用户(或用户组)以 root 或其他用户身份运行命令的能力,同时提供命令及其参数的审计跟踪。

用法

为示例用户 myuser 启用 sudo。

users.users.myuser.extraGroups = [ "wheel" ];

通过简单的配置,将允许属于 wheel 组的所有用户以超级用户身份且无需提供该用户的密码使用 sudo 执行 extraRules 中指定的命令。

security.sudo = {
  enable = true;
  extraRules = [{
    commands = [
      {
        command = "${pkgs.systemd}/bin/systemctl suspend";
        options = [ "NOPASSWD" ];
      }
      {
        command = "${pkgs.systemd}/bin/reboot";
        options = [ "NOPASSWD" ];
      }
      {
        command = "${pkgs.systemd}/bin/poweroff";
        options = [ "NOPASSWD" ];
      }
    ];
    groups = [ "wheel" ];
  }];
  extraConfig = with pkgs; ''
    Defaults:picloud secure_path="${lib.makeBinPath [
      systemd
    ]}:/nix/var/nix/profiles/default/bin:/run/current-system/sw/bin"
  '';
};