← back

Allowing Claude to sudo

2026-08-14

Sometimes I want Claude to do something that requires sudo access. For example, managing Systemd services. Claude cannot run sudo directly, since the normal command takes input from terminal stdin. However, you can use the SUDO_ASKPASS environment variable to allow sudo to run an arbitrary password command.

I created a script for this, and placed it in a local directory:

#!/bin/sh
exec kdialog --password "$1" --title "sudo"

Then in ~/.claude/settings.json:

{
  "env": {
    "SUDO_ASKPASS": "/home/legu/.local/bin/sudo-askpass"
  }
}

Now, I can ask Claude to sudo a command, and have a dialog pop up to grant it access.

Since Claude’s environment is non-interactive, it doesn’t need to add the -A argument, it is implied. It also means that granting sudo for one command will not grant it for another.