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

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.

Last updated

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

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:

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

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

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

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

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

$ 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 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 covers that case. Passwords for ssh itself are covered in saving an 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 describes it.

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

References

The app is free.

Get notified at launch

macOS is coming to the MacĀ AppĀ Store. iOS and Android later.