linux - sorry, you must have a tty to run sudo

06
2014-04
  • AlwaysALearner

    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
    exitCode=1
    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?

  • Answers
  • Ask and Learn

    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)


  • Related Question

    linux - Can I have sudoedit check `$EDITOR` like visudo does?
  • Anachrome

    As far as I know, the major difference is that visudo has a simple parser for seeing if /etc/sudoers (and other sudoers files) is goodly; however, I also noticed that, when I set EDITOR=emacs, sudoedit honours the change, however, visudo does not. From what I can tell, this is because visudo does not honour $EDITOR values unless they are listed in sudoers. What I want to know is if I can make sudoedit do the same thing, so as to not allow things like EDITOR=deleverything.sh.

    Another thing I noticed, which seems related, is that as root (after su-ing as user jef), (with the line export EDITOR=emacs in /home/anachrome/.bashrc), both visudo and sudoedit use emacs as the editor, but when not su'd into root, only sudoedit uses it. I'm curious as to why this is (I presume it has something to do with sudo not actually resetting the evironment variables when sudoing as yourself (e.g. root $ sudo -u root), but this is just me assuming).

    EDIT: The second question is apparently just because $EDITOR wasn't one of the variables kept by sudo.

    And incidentally, I'm using Arch Linux, in case that's relevant.


  • Related Answers
  • Mika Fischer

    For me on Arch, neither visudo nor sudoedit honor $EDITOR but they do both honor $VISUAL, so maybe that's worth a try.

  • Mat

    Also check that the desired editor is actually installed. Specifying EDITOR=vim when all you've got is vi.tiny won't give much joy.