teaching claude code to sudo
claude code runs shell commands for you. it can edit files, run tests, install packages - but the moment something needs sudo, it hits a wall. the terminal prompt for a password is interactive, and claude's shell isn't. the command just hangs or fails with "authentication failed."
the obvious fix is passwordless sudo. add NOPASSWD: ALL to sudoers and call it a day. but that means any process running as your user can do anything as root with zero friction. on a shared machine that's a non-starter. even on a personal workstation it felt wrong.
what i actually wanted
i wanted claude to be able to run sudo commands, but with my password still required. not typed interactively - just retrieved securely and provided automatically. same security boundary, no interactive prompt.
SUDO_ASKPASS
turns out sudo has a built-in mechanism for exactly this. if you set the SUDO_ASKPASS environment variable to point at an executable, then sudo -A will call that program to get the password instead of prompting the terminal. it's been in sudo for years - designed for graphical environments where there's no terminal to prompt.
the key insight: sudo -A doesn't bypass authentication. it just changes where the password comes from. your password is still checked against PAM, still subject to rate limiting and lockout policies. the only difference is the input method.
secure-askpass
i'm using GlassOnTin/secure-askpass, which does the password retrieval part well. you store your sudo password once, it encrypts it with your SSH key using age (a modern encryption tool), and saves the ciphertext to ~/.sudo_askpass.age. when sudo -A calls the askpass program, it decrypts the password using your SSH key and hands it to sudo.
the encryption chain: your sudo password is encrypted with your ed25519 SSH key. to decrypt it, you need the private key. if someone gets the .age file without your SSH key, they get nothing. if someone has your SSH key, they already have SSH access to your machine anyway - the sudo password isn't the weak link.
how it fits together
ageencrypts my sudo password with my ed25519 SSH key- the encrypted blob lives at
~/.sudo_askpass.age SUDO_ASKPASSis set in/etc/environmentso every session picks it up- claude code runs
sudo -A <command>instead ofsudo <command> - sudo calls the askpass script, which decrypts and returns the password
- sudo authenticates normally
from claude's perspective, sudo -A just works. from my perspective, my password is still required - it's just stored encrypted rather than typed interactively.
the alternatives and why i skipped them
NOPASSWD in sudoers - no password required at all. too permissive. any process running as my user gets free root access.
whitelisting specific commands - anthropic's official recommendation. add specific commands to sudoers with NOPASSWD. fine if you know exactly what you'll need ahead of time, but i don't. half the point of claude code is that it figures out what commands to run.
copy-pasting commands manually - run the command yourself, paste the output back. defeats the purpose of having an AI coding assistant that can execute things.
SUDO_ASKPASS with a simple script - just echo "mypassword". works but your password is sitting in a plaintext file. only marginally better than NOPASSWD.
gotchas
sudo -A, not sudo. regular sudo still tries the terminal prompt. the -A flag is what triggers the askpass mechanism. claude needs to know to use it.
set it in /etc/environment, not just your shell config. i initially put SUDO_ASKPASS in my fish config. worked great in my terminal. completely invisible to claude code, which spawns bash subshells that never source fish config. /etc/environment is parsed by PAM at login and applies to every session - interactive fish, non-interactive bash, everything. that's the one that actually matters.
SSH key must be available. the decryption depends on your SSH private key. if your key has a passphrase and isn't loaded in ssh-agent, the askpass script can't decrypt. i removed my SSH key passphrase on this machine since the drive is LUKS-encrypted anyway - the disk encryption is the outer layer.
age is required for ed25519. the tool uses age for encryption when you have ed25519 keys (which is the modern default). older RSA keys use openssl instead. either way, it's a single package.
eli5
imagine you have a really good assistant who can do anything in your workshop, but the tool cabinet is locked and only opens with a voice password. your assistant can't speak, so every time they need a tool, they have to walk over and get you to say the password. secure-askpass is like giving your assistant a recording of your voice (encrypted password) locked in a box that only their fingerprint (SSH key) can open. they can get into the tool cabinet themselves, but only because you set it up - and if someone steals the recording, they can't play it without the fingerprint.