# What do I do when Claude Code needs sudo?

Claude Code ran sudo and it failed or hung. Use sudo -n and sudo -l, and give the account a rule for one command instead of a password in the chat.

Source: https://valakes.com/learn/claude-code-sudo/

Last updated: 2026-11-29

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.

Commands checked on Ubuntu 24.04.5 LTS (the ubuntu:24.04@sha256:562111b88caa79c71391c0c4d3c2114a7d5c35915c20561fd893cb1e07929e6b container image), 29 November 2026.

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](https://valakes.com/learn/sudo-for-one-command/) 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.

```console
$ 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:

```console
$ 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:

```console
$ 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](https://valakes.com/learn/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:

```console
$ 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`:

```console
$ 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:

```console
$ 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:

```console
$ 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](https://valakes.com/learn/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:

```console
$ 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:

```console
$ 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](https://valakes.com/learn/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](https://valakes.com/learn/sudo-for-one-command/) 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](https://valakes.com/learn/auto-approve-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.

## In Valakes

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](https://valakes.com/features/password-auto-fill/)

[Read the docs: Credentials and auto-fill](https://docs.valakes.com/credentials/)

## References

- [sudo(8) at sudo.ws, the options -n, -l and -S](https://www.sudo.ws/docs/man/sudo.man/)
- [sudoers(5) at sudo.ws, command specs and the listpw option](https://www.sudo.ws/docs/man/sudoers.man/)
- [Claude Code documentation, tools reference, Bash tool behavior and timeouts](https://code.claude.com/docs/en/tools-reference)
- [Claude Code documentation, permissions](https://code.claude.com/docs/en/permissions)
- [Codex documentation, agent approvals and security](https://learn.chatgpt.com/docs/agent-approvals-security)

## Related articles

- [How do I give an AI agent SSH access with the least damage?](https://valakes.com/learn/ai-agents-on-servers/): Give the agent its own account on the server, with no sudo, no password in the prompt and nothing secret in its reach. Let it read first, allow root for one exact command at most, make every change wait for a person, and keep a record of what ran.
- [How do I let a user run one command with sudo?](https://valakes.com/learn/sudo-for-one-command/): Write a rule that names the command with its full path and exact arguments, put it in its own file under /etc/sudoers.d/, check it with visudo -cf, and install it with mode 440. Do not use wildcards in the arguments, and do not allow editors or any program that can start a shell.
- [How do I run sudo over SSH when it says "a terminal is required"?](https://valakes.com/learn/sudo-over-ssh/): Add -t, as in ssh -t app sudo whoami, so ssh gives the remote command a terminal and sudo can ask for the password. In a script, give the account a NOPASSWD rule for that one command instead of sending the password into sudo -S.
- [Which commands are safe to auto-approve for an AI agent on a server?](https://valakes.com/learn/auto-approve-agent-commands/): Only exact commands that read and cannot start another program. find, sed, awk, a redirection and ssh look like reads and are not, so a rule that allows them by name allows what they can do.
