home / blog

TTY sudo fun

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

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

# This will work
ssh -t example.com 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.

<?php
include('Net/SSH2.php');
$ssh = new Net_SSH2('example.com');
if (!$ssh->login('username', 'password')) {
  echo $ssh->exec("open_init_pty sudo echo "hello world");
}
?>
This entry was posted in geek and tagged . Bookmark the permalink.

Leave a Reply

Your email address will not be published.