Linux
帶有 sudo 的 ssh 不啟動服務
我想通過 ssh 使用 sudo 執行命令,而不禁用“requiretty”sudo 選項。根據 /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
通常,“ssh -t”工作正常。我可以執行類似 whoami 的命令:
ssh -t 'bnadmin@devgreg' 'sudo whoami' root Connection to hostname closed.
除了奇怪的消息“與主機名的連接已關閉”之外,它工作正常。當我試圖觸發應該被妖魔化的服務時,問題就來了。如果我通過 ssh 執行它們,它們只是沒有啟動。
例如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.
除了 tomcat,我還有任何 init.d 啟動腳本的問題。如果我在 sudoers 文件中註釋掉 requiretty 並且不使用 -t ssh 選項,那麼它會成功啟動。
$ 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
所以我的問題是如何在不禁用 requiretty sudo 選項的情況下通過 ssh 觸發後台程序?
嘗試在命令前加上
nohup
, 如sudo nohup /usr/local/tomcat/bin/startup.sh
.