Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I had already asked this question in Stack Overflow, but I've been asked to post it here. So doing the same.

I ran this command using my java program-

sudo -u <username> -S pwd

I got this output-

command=sudo -u <username> -S pwd
sudo: sorry, you must have a tty to run sudo

I tried editing /etc/sudoers but it already contains

<username>       ALL=(ALL)       NOPASSWD: ALL

Then, I learned that this can be done by commenting out the following code in /etc/sudoers

# Defaults requiretty

Also, by default, when attempting to execute a command as another user using sudo, we have to provide our own password. But this can be changed by making the following change in /etc/sudoers-

Defaults targetpw

My Question is, is it possible to execute my above command in java without making any changes anywhere i.e. by having default settings?

share|improve this question
Is this why you were asking the rather contrived Alternative for sudo? – slhck Aug 19 '13 at 9:00
Seems distro specific if requiretty is enabled by default. According to sudo's own documentation it is "off by default." – mpy Aug 19 '13 at 9:32

I don't know how to run shell commands in Java but have a look at -t option for ssh command

-t force pseudo-tty allocation.

That is what I do when I need to run command as root over ssh(direct root login disable and tty required by sudo)

share|improve this answer
While this approach could solve the problem at first sight it is not clear at all how is it related to the question. Could you please explain in more details? – pabouk Nov 12 '13 at 8:25
+1 for solving the problem without needing to change any default settings. – Luke Jan 31 '14 at 4:54

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.