从 Tomcat servlet 运行 sudo 命令 [英] Running sudo command from Tomcat servlet
问题描述
有没有办法让 tomcat 7 服务器(在 Ubuntu 服务器上运行)在命令行中执行 sudo 命令?在这种特定情况下,我想让从另一台服务器关闭系统成为可能.我必须使用什么代码?我是否必须以 root 身份运行 tomcat 服务器,或者我可以在运行时获得这些权限并且仅针对此操作?感谢您的帮助:)
Is there a way to let a tomcat 7 server (running on an Ubuntu Server) execute a sudo command in a command line? In this specific case I want to make it possible to shut down the system from another server. What code would I have to use? And do I have to run the tomcat server as root or can i get these privileges at runtime and only for this action? Thanks for your help:)
推荐答案
无需以 root 身份运行 Tomcat,您只需将 tomcat 用户添加到 /etc/sudoers
以允许他运行超级用户命令,就像sudo shutdown now
.这种方式允许您指定可由用户执行的所需命令子集.例如.允许他只运行 shutdown
:
No need to run Tomcat as root, you can just add tomcat user to /etc/sudoers
to allow him run superuser commands, like sudo shutdown now
. This way allows you to specify desired subset of commands that can be executed by user. E.g. to allow him to run only shutdown
:
tomcat ALL=NOPASSWD: /sbin/shutdown
要从 Java 代码运行 shell 命令,您可以调用 Runtime.exec()
或使用 ProcessBuilder
.
To run shell commands from Java code you can call one of Runtime.exec()
or use ProcessBuilder
.
附言还可以尝试搜索 /etc/shutdown.allow
文件,该文件允许其中列出的任何用户运行关闭命令.不过我没用过.
P.S. Also try googling about /etc/shutdown.allow
file which allows running shutdown command by any user that is listed in it. But I've never used it.
这篇关于从 Tomcat servlet 运行 sudo 命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!