"sudo: sorry, you must have a tty to run sudo" when using sudo in a remote script

1,045

With the first one there is no tty for ssh since stdin is not connected to the terminal, it is a here file. In fact if I try to run a similar command (on Debian) I get the following error:

Pseudo-terminal will not be allocated because stdin is not a terminal.

To get it to work you can do something like:

ssh -tt 192.168.1.100 <<EOF
sudo -S id
password
EOF

Although this is not a good idea since the password will be in plain text.

Update

I stumbled across an easy solution to this that avoids encoding the password in plain text, you can use a graphical program to enter the password:

ssh -X 192.168.1.100 <<EOF
SUDO_ASKPASS=/usr/lib/ssh/x11-ssh-askpass sudo -A id
EOF

Of course the ssh-askpass program must be installed in the given location and you must be running an X session on the machine you are working on. There are a few variations on the ssh-askpass program which should also work (Gnome/KDE versions). Also a graphical sudo replacement program like gksu or kdesudo should do the job too.

Share:
1,045

Related videos on Youtube

Kumar
Author by

Kumar

Updated on September 18, 2022

Comments

  • Kumar
    Kumar over 1 year

    I am trying to write Observe pattern in Java in multithread program to develop own logging program. My program is used in any Java class and can create the Logger class, and write a message to the logger. Logger class will call the logger manager class , which is a Singleton class and it also have the private class which is thread, on execution a scheduler runs, So I kept that execution in the private constructor.

    private void LoggerManager() { 
        System.out.println("IN CONSTRUCTOR"); 
        executorThread.submit(new CreateLoggerFileForSpecifiedTime());
    }
    
    static LoggerManager getInstance() {
        if (LOGMANAGER == null) {
            synchronized (LoggerManager.class) {
                if(LOGMANAGER == null){
                    System.out.println("IN MANAGER");
                    LOGMANAGER = new LoggerManager();
                }
            }
        }
        return LOGMANAGER;
    }
    

    Next there is a method in the LoogerManager "writeMessageToLog".

    void writeMessageToLog(String componantName, String message, 
                         Calendar messageCreationTime) { 
        LoggerDetails logDetails = new LoggerDetails(componantName, message,
                         messageCreationTime); 
        LogInitiater logIntiater = new LogInitiater(logDetails, noticeOfLoggerChange, 
                         noticeOfMessageAdded); 
        executorThread.submit(logIntiater); 
    } 
    

    LogIntitiater is a thread which is added the LogWriter to two Subject's, One subject is a scheduler I have pasted above i.e a Timer runs for every specified time and notfiies LogWriter the need to create a new log file while the other purpose is when a message is added in the queue. Code is not working as expected: * scheduler code is not running, I tried with execute as well. That private constructor is not being called * thread initiater is not always called * observe pattern: I have used J2SE interfaces i.e Observable and Observe and they are not working as expected

    Please help me.

    • Kumar
      Kumar over 11 years
      I accepted all the answer which resolved my question. How can I accept the answer without solving my problem
    • Narendra Pathai
      Narendra Pathai over 11 years
      fine I was just saying. By the way are you trying to make Logger asynchronous here?
    • Kumar
      Kumar over 11 years
      Ya, Yes you are correct. Is there any other solution. Please let me know
    • Isuru Gunasekara
      Isuru Gunasekara almost 10 years
      I can't change the /etc/sudoers to remove the requiretty and I'm required to use a password to elevate with sudo.
    • Isuru Gunasekara
      Isuru Gunasekara almost 10 years
      also, I am already using the -t option on my ssh, it just that the embedded script within the EOF tags isn't respecting the forced tty
  • Kumar
    Kumar over 11 years
    Above solution works fine on stand alone. But I need aditional task as well, Need to write the message in the log file. And Every 10 minutes I need to change the log file as well. And Message should be in the Order of the request comes, and nothing should be stooped at the file creation time.
  • Narendra Pathai
    Narendra Pathai over 11 years
    yes :) but there are some cases like queue full that I have not handelled
  • Kumar
    Kumar over 11 years
    Thank you Narendra, it worked, I have added your classes to root of my classes , it started working. Thank you very much for your help
  • Narendra Pathai
    Narendra Pathai over 11 years
    That's what we are here to do :) Glad I could help you @Kumar
  • Isuru Gunasekara
    Isuru Gunasekara almost 10 years
    That doesn't work for me either. I get tcgetattr: Inappropriate ioctl for device
  • Graeme
    Graeme almost 10 years
    @Andy, yes, it will since there is no actual terminal but sudo still tries to read the password form it. It is possible to use -S to make it read the password from stdin (not recommended though), see my edit.
  • Isuru Gunasekara
    Isuru Gunasekara almost 10 years
    It also outputs the password in clear text and still doesn't run the command.
  • Graeme
    Graeme almost 10 years
    @Andy, I had the same effect, yes. It did run the command though.
  • Isuru Gunasekara
    Isuru Gunasekara almost 10 years
    The answer suggested at serverfault.com/questions/479553/… works for a simple command (like sudo id), but for my more complex scripts, it doesn't interpret correctly.
  • Isuru Gunasekara
    Isuru Gunasekara almost 10 years
    Displaying the password in clear text either in the script or in the stdout doesn't work for me. I'm fine with it prompting me for my password again to elevate my priveleges "[sudo] password for user:"
  • Graeme
    Graeme almost 10 years
    @Andy, for this I was just trying to show why the here file approach wasn't working and why it isn't really a good idea anyway. I can't suggest a better solution without knowing more about what you are trying to do. Although you can also have something like ssh -t host 'sudo id; echo "non-root command"; sudo echo "another root command"' in addition to using bash -c. The trick is usually just to make sure things are correctly quoted.
  • Isuru Gunasekara
    Isuru Gunasekara almost 10 years
    I understand. Thanks for the suggestion. My script was already written to use ssh root@host, but they took root access away and forced me to use sudo. so I'm trying to substitute in the sudo commands for a variety of system checks. simple things, like looking for certain users in the /etc/sudoers file. Most of the script runs fine as myself, but for those few places I need to escalate, I have to use sudo.
  • Graeme
    Graeme almost 10 years
    @Andy, updated with a possible way to work around this.