What do I do when Claude Code needs sudo?
Do not paste the password into the chat and do not write NOPASSWD ALL. Run the command with sudo -n so it fails at once, read what the account may run with sudo -l, and give the account a sudoers rule for that one command.
Last updated
Commands checked on Ubuntu 24.04.5 LTS (the ubuntu:24.04@sha256:562111b88caa79c71391c0c4d3c2114a7d5c35915c20561fd893cb1e07929e6b
container image), .
Run the command again with sudo -n. It fails at once with “a password is required” instead of waiting for someone to type. Then read what the account may run with sudo -l, and give the account a sudoers rule for that one command. Do not put the password in the chat, and do not write NOPASSWD: ALL.
The account in the examples is agent. It is in the sudo group and has a password that the agent does not know. I run the commands as an administrator with sudo -u agent, the way the sudo article tests a rule. The Claude Code pages on tools and permissions and the Codex page on approvals do not say how a sudo password prompt is handled, so this is about sudo itself. It holds for any agent that runs commands with nobody at that shell. Each command ran on the system named in the check line.
$ sudo --version | head -1
Sudo version 1.9.15p5
What sudo prints when no one can type
A person at a terminal sees [sudo] password for agent: and types. An agent’s command has nobody to type. setsid starts a command with no terminal attached, so it shows what sudo says in that case:
$ sudo -u agent setsid --wait sudo touch /run/maintenance < /dev/null
sudo: a terminal is required to read the password; either use the -S option to read from standard input or configure an askpass helper
sudo: a password is required
When a terminal is attached, sudo prints its prompt and waits for a password. timeout stops the command after three seconds, and echo shows its exit status:
$ sudo -u agent timeout 3 sudo touch /run/maintenance; echo "exit $?"
[sudo] password for agent:
sudo: a password is required
exit 124
Status 124 is how timeout reports that it had to stop the command. Without it, nothing ends the wait but the tool that started the command. The Claude Code documentation gives its Bash tool a default timeout of two minutes, after which a foreground command moves to the background. For those two minutes the command sits at a prompt that nobody can answer. The message of the first command also appears in an ordinary SSH session, which sudo over SSH covers.
Make it fail at once with sudo -n
The option -n (non-interactive) tells sudo never to ask. If it would need a password, it stops with an error and an exit status:
$ sudo -u agent sudo -n touch /run/maintenance; echo "exit $?"
sudo: a password is required
exit 1
Tell the agent to write sudo -n in front of every privileged command, so a missing right shows up as a short error and not as a hang.
See what the account may run with sudo -l
You list the rights of another account with -l -U:
$ sudo -l -U agent
Matching Defaults entries for agent on dev:
env_reset, mail_badpass,
secure_path=/usr/local/sbin\:/usr/local/bin\:/usr/sbin\:/usr/bin\:/sbin\:/bin\:/snap/bin,
use_pty
User agent may run the following commands on dev:
(ALL : ALL) ALL
The agent can run sudo -n -l as itself, and gets this:
$ sudo -u agent sudo -n -l
sudo: a password is required
Listing needs a password too, as long as every entry of the account asks for one. Once the account has a rule with NOPASSWD, the agent can list its rights without one, as you will see below.
Why the password does not go into the prompt
A password you type into the chat is text that goes to the model, and the agent has to write it into a command to use it, such as sudo -S. That makes at least two copies, in places you cannot clean afterwards. The Valakes Help says an external agent sends what it reads, command output included, to its model provider. And a password does not say which command it is for. The account above can run anything as root, and whoever holds the password can too.
Why NOPASSWD: ALL is root for the agent
The other quick fix removes the question. This rule lets the account run every command as root without asking:
$ echo 'agent ALL=(ALL) NOPASSWD: ALL' | sudo tee /etc/sudoers.d/agent-all
agent ALL=(ALL) NOPASSWD: ALL
$ sudo -u agent sudo -n id
uid=0(root) gid=0(root) groups=0(root)
$ sudo rm /etc/sudoers.d/agent-all
uid=0 is root. The agent can now read /etc/shadow, change the SSH configuration and install its own key, and nothing stops it, because nothing asks. The last command removes the demo rule. The sudo group that the account started in gives the same right, with a password asked first.
Give the account a rule for one command
Write a rule that names one command with its exact arguments. The article on sudo for one command explains the format, why wildcards break it and how to check the file with visudo -cf. Here touch stands in for a restart. The last step takes the account out of the sudo group, so the rule is its only right:
$ echo 'agent ALL=(root) NOPASSWD: /usr/bin/touch /run/maintenance' > /tmp/agent-maint
$ sudo visudo -cf /tmp/agent-maint
/tmp/agent-maint: parsed OK
$ sudo install -m 440 -o root -g root /tmp/agent-maint /etc/sudoers.d/agent-maint
$ sudo gpasswd -d agent sudo
Removing user agent from group sudo
Now the agent can see its rights, run the allowed command and get a plain refusal for anything else:
$ sudo -u agent sudo -n -l
Matching Defaults entries for agent on dev:
env_reset, mail_badpass,
secure_path=/usr/local/sbin\:/usr/local/bin\:/usr/sbin\:/usr/bin\:/sbin\:/bin\:/snap/bin,
use_pty
User agent may run the following commands on dev:
(root) NOPASSWD: /usr/bin/touch /run/maintenance
$ sudo -u agent sudo -n touch /run/maintenance
$ stat -c '%U %n' /run/maintenance
root /run/maintenance
$ sudo -u agent sudo -n touch /etc/other
sudo: a password is required
The agent can read this list and know what is open to it before it tries. That is the setup the article on AI agents on servers recommends for the account as a whole.
Or do the privileged step yourself
A step that happens once does not need a rule. Run it in your own terminal, as yourself, and tell the agent it is done. The agent continues with reads that check the result. The password and the root right stay with you.
Mistakes people make
You type the password into the chat “just this once”. It is now in the conversation, and the agent can use it for anything.
You add the agent’s account to the sudo group so that it can install things. That is ALL with a password, and it is the reason you feel pushed to paste the password.
You write NOPASSWD: ALL because the first rule did not work. Run sudo -n -l as the agent first and see what the account really has. A rule matches the path and the arguments exactly, so a command written with another path or one more argument is refused.
You allow an editor, find or a shell in the rule. The sudo article shows why each one is a root shell. Whether such a command is safe to run without asking at all is a separate question, and the article on auto-approving agent commands takes it up.
In Valakes
Valakes can answer a sudo prompt for you or for an agent. It ships a rule for sudo, and when a credential is bound to it with Auto-fill on, the agent writes {{cred:name}} where the password goes and Valakes answers the prompt without showing the value. A command that uses a credential on a production host always waits for your approval. When a command waits for your approval, the card has Take over, which moves the command into your composer so you run it yourself.
If a server keeps a sudo password, you can bind a saved credential to sudo for that host. The agent writes a placeholder, and Valakes answers the prompt without showing it the value. Feature: Password auto-fill
References
- sudo(8) at sudo.ws, the options -n, -l and -S www.sudo.ws
- sudoers(5) at sudo.ws, command specs and the listpw option www.sudo.ws
- Claude Code documentation, tools reference, Bash tool behavior and timeouts code.claude.com
- Claude Code documentation, permissions code.claude.com
- Codex documentation, agent approvals and security learn.chatgpt.com
Related articles
The app is free.
Get notified at launchmacOS is coming to the Mac App Store. iOS and Android later.