TTY sudo fun

requiretty is an option in /etc/sudoers which prevents sudo operations from non-TTY sesssions.

# This will be disallowed
ssh echo "hello world"
sudo: sorry, you must have a tty to run sudo

# This will work
ssh -t echo "hello world"
hello world

If however you’re using SSH via a wrapper you cannot control, like phpseclib, or ssh2 native support in PHP then you cannot simply pass a -t option.

In this case you need the lesser known command open_init_pty which “runs a program under a psuedo terminal”. This is in debian package policycoreutils if you need to install it.

$ssh = new Net_SSH2('');
if (!$ssh->login('username', 'password')) {
  echo $ssh->exec("open_init_pty sudo echo "hello world");

Leave a comment

Your email address will not be published.