# How do I run sudo over SSH when it says "a terminal is required"?

ssh app sudo whoami fails with a terminal error. Use ssh -t, sudo -S, or a NOPASSWD rule for one command, and see why an SSH key does not skip sudo.

Source: https://valakes.com/learn/sudo-over-ssh/

Last updated: 2026-11-29

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.

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

Add `-t`: `ssh -t app sudo whoami`. By default ssh runs a remote command without a terminal, and sudo has nowhere to ask for the password. With `-t` ssh allocates one and sudo prints its prompt. For a script nobody watches, write a `NOPASSWD` rule for the one command, and do not pipe a password into `sudo -S`.

## The error

Logging in with a key works. Running sudo on the other end does not:

```console
$ ssh app whoami
deploy
$ ssh app sudo whoami
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
$ ssh app sudo --version | head -1
Sudo version 1.9.15p5
```

This is Ubuntu 24.04 and sudo 1.9.15. The first line is an entry in the diagnostics section of the sudo manual: sudo needs the password and has no mechanism to read it. The manual adds that remote commands run through ssh have no terminal by default, and that passing `-t` to ssh allocates one so that sudo can read the password. The second line says that no password was read, so sudo gave up.

## Why ssh -t works

Ask the remote side for its terminal:

```console
$ ssh app tty
not a tty
$ ssh -t app tty
/dev/pts/0
Connection to app.example.test closed.
```

The `ssh(1)` manual says that `-t` forces pseudo-terminal allocation, and `-T` turns it off. Given a command, ssh asked for no terminal here, and the first `tty` says so. With `-t` there is one, and sudo prints `[sudo] password for deploy:` and waits for you to type. I cannot show that run here, because nobody types in this check, and a script that waits for a typist would hang. Use `-t` when you are at the keyboard.

## sudo -S reads the password from standard input

The `-S` option writes the prompt to standard error and reads the password from standard input, so no terminal is needed:

```console
$ printf 'demo-pass\n' | ssh app sudo -S whoami
[sudo] password for deploy: root
```

It works, and the password now lives wherever you got it from. Two places show up in practice. The first is your shell history, which keeps every line you type. Here an interactive bash reads one line with the password in it:

```console
$ printf 'echo demo-pass\nexit\n' | HISTFILE=/tmp/h bash -i > /dev/null 2>&1
$ cat /tmp/h
echo demo-pass
exit
```

The second is `ps`. When the password is part of the command that runs on the server, any user there can read it while the command runs:

```console
$ ssh app "echo demo-pass | sudo -S sleep 3" > /dev/null 2>&1 &
$ sleep 1
$ ssh app 'ps -eo args | grep "[s]udo -S"'
bash -c echo demo-pass | sudo -S sleep 3
sudo -S sleep 3
$ wait
```

The `printf` form above keeps the password off the remote command line, because it travels on standard input. It stays in the line you typed, and in any script that contains it.

## A rule for one command

For a script, write a sudoers rule that lets the account run one command with its exact arguments without a password. [The article on sudo for one command](https://valakes.com/learn/sudo-for-one-command/) shows how to write it, check it and avoid wildcards. The server of this run has a rule for `touch /run/maintenance` and nothing else:

```console
$ ssh app sudo -n touch /run/maintenance
$ ssh app "stat -c '%U %n' /run/maintenance"
root /run/maintenance
$ ssh app sudo -n cat /etc/shadow
sudo: a password is required
```

The command needed neither `-t` nor `-S` nor a password, and the file belongs to root. The `-n` option stops sudo from asking, so a command outside the rule fails at once. The sudo manual says that with `-n`, sudo prints an error and exits when a password is needed.

## Why a key does not remove the sudo password

The two logins check different things. sshd compares your key with the `authorized_keys` file of the account `deploy` and lets you in. That is all the key does. Then sudo decides, from its own rules, whether `deploy` may run a command as root, and unless the rule says `NOPASSWD` it asks for the password of `deploy`. The prompt in the output above names the account: `[sudo] password for deploy:`. The first command of this article went in with a key and no password, and sudo still asked.

That is also why [turning on key login](https://valakes.com/learn/ssh-keys/) changes nothing about sudo. The way to stop sudo from asking is a rule, and the rule should name one command.

## Mistakes people make

You put the password in a script as `echo secret | sudo -S`. It lands in the file, in the history and, when it sits in the remote command, in `ps` on the server.

You write `NOPASSWD: ALL` so that the question goes away. That makes the account root for anyone who gets its key. Name the command.

You look for `requiretty`. The sudoers manual says that flag is off by default. The error says sudo cannot read the password, which is a different thing.

You add `-t` to a cron job or a CI step. Nobody is there to type, and the job waits.

When the one who needs sudo on the server is an AI agent, [Claude Code and sudo](https://valakes.com/learn/claude-code-sudo/) covers that case. Passwords for ssh itself are covered in [saving an SSH password](https://valakes.com/learn/save-ssh-password/).

## In Valakes

If a server keeps a sudo password, you can bind a saved credential to `sudo` for that host. Valakes then answers the sudo prompt from its vault, and the value does not appear on screen. sudo is one of the ten tools Valakes knows by default, and [password auto-fill](https://valakes.com/features/password-auto-fill/) describes it.

## In Valakes

If a server keeps a sudo password, you can bind a saved credential to sudo for that host, and Valakes answers the sudo prompt from its vault without showing 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 -S and -n and the diagnostic "a terminal is required to read the password"](https://www.sudo.ws/docs/man/sudo.man/)
- [sudoers(5) at sudo.ws, the requiretty and use_pty flags](https://www.sudo.ws/docs/man/sudoers.man/)
- [ssh(1) on Ubuntu 24.04, the options -t and -T](https://manpages.ubuntu.com/manpages/noble/man1/ssh.1.html)

## Related articles

- [How do I generate an SSH key and put it on a server?](https://valakes.com/learn/ssh-keys/): Run ssh-keygen -t ed25519, then ssh-copy-id -i ~/.ssh/id_ed25519.pub user@host. The private key stays on your computer, the public key goes into ~/.ssh/authorized_keys on the server, and you log in without a password.
- [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.
- [Can I save an SSH password in the config file?](https://valakes.com/learn/save-ssh-password/): No. ssh_config has no option for a password, and ssh stops with an error when a config file contains one. Load a key into ssh-agent and nothing is asked; for a device that takes only a password, use ControlMaster to ask once per session or sshpass to answer for a script.
- [What do I do when Claude Code needs sudo?](https://valakes.com/learn/claude-code-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.
