I want to run command with sudo through ssh without disabling "requiretty" sudo option. According to /etc/sudoers:

# Disable "ssh hostname sudo <cmd>", because it will show the password in clear. 
#         You have to run "ssh -t hostname sudo <cmd>".
Defaults    requiretty

Generally, "ssh -t" is working fine. I'm able to run commands like whoami:

ssh -t  'bnadmin@devgreg' 'sudo whoami'
Connection to hostname closed.

Besides the weird message "Connection to hostname closed" it's working fine. The problem comes when I'm trying to trigger services that supposed to be demonized. They are just not starting if I run it through ssh.

For example tomcat:

ssh -t 'myuser@hostname' 'sudo /usr/local/tomcat/bin/startup.sh'
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr/java/latest/
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Connection to hostname closed.
$ ssh -t 'myuser@hostname' 'ps aux | grep java'
myuser  21177  0.0  0.1 106096  1380 pts/2    Ss+  17:25   0:00 bash -c ps aux | grep java
myuser  21185  0.0  0.0 103244   824 pts/2    S+   17:25   0:00 grep java
Connection to hostname closed.

Besides tomcat I have the problem with any init.d startup script. If I comment out requiretty in the sudoers file and don't use -t ssh option, then it's successfully starting.

$ ssh 'myuser@hostname' 'sudo /usr/local/tomcat/bin/startup.sh'
$ ssh 'myuser@hostname' 'ps aux | grep java'
root   21235 90.8 69.3 3706044 701956 ?      Sl   17:30   0:39 /usr/java/latest//bin/java -Djava.util.logging.config.file=/usr/local/tomcat/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -javaagent:/usr/local/tomcat/bin/../spring-instrument-3.1.1.RELEASE.jar -Xms1500m -Xmx2500m -XX:PermSize=256m -Djava.endorsed.dirs=/usr/local/tomcat/endorsed -classpath /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar -Dcatalina.base=/usr/local/tomcat -Dcatalina.home=/usr/local/tomcat -Djava.io.tmpdir=/usr/local/tomcat/temp org.apache.catalina.startup.Bootstrap start
myuser  21279  1.0  0.1 106096  1380 ?        Ss   17:30   0:00 bash -c ps aux | grep java
myuser  21287  0.0  0.0 103244   824 ?        S    17:30   0:00 grep java

So my question is how can trigger background processes with sudo through ssh without disabling requiretty sudo option?


Try prefixing the command with nohup, as in sudo nohup /usr/local/tomcat/bin/startup.sh.

  • Thanks, that worked! Other lesson I learnt is that I actually had to use "sudo nohup" instead of "nohup sudo" as I tried to to in other scripts.
    – Soid
    Oct 29 '13 at 0:55
  • One alternative to nohup would be screen. This would allow you to reattach to the process if you needed access to control the process at a later time.
    – Rob Gibson
    Nov 4 '13 at 1:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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