Linux 如何从终端打开 xterm 窗口并从 xterm 在后台运行命令?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10862715/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Open xterm window from a terminal and run a command in background from xterm?
提问by Rajasekhar
My application tries to execute roots command "sudo ifup eth0" and "sudo ifdown eth0". But it returned an error "sudo: sorry, you must have a tty to run sudo". So, it requires a tty to execute the sudo commands. So, I tried to execute the commands by opening tty sessions
我的应用程序尝试执行 root 命令“sudo ifup eth0”和“ sudo ifdown eth0”。但它返回了一个错误“sudo:抱歉,你必须有一个 tty 才能运行 sudo”。因此,它需要一个 tty 来执行 sudo 命令。所以,我尝试通过打开 tty 会话来执行命令
gnome-terminal --command="sudo ifdown eth0" &
xterm -e "sudo ifdown eth0" &
then it worked fine. But I am not able to send the command from newly created gnome-terminal or xterm. i.e., if I close the newly created gnome or xterm windows before they had executed the commands, then the commands were terminated immediately.
然后它工作正常。但是我无法从新创建的 gnome-terminal 或 xterm 发送命令。即,如果我在执行命令之前关闭新创建的 gnome 或 xterm 窗口,则命令会立即终止。
Can you give suggestion how to disable the windowfrom closing by the user or how to make it invisible to the user?
您能否提出建议如何禁用用户关闭窗口或如何使其对用户不可见?
Note: you can test this by using system-config-networkcommand instead of ifdown and ifup
注意:您可以使用system-config-network命令代替 ifdown 和 ifup 进行测试
采纳答案by Dmitri Chubarov
I would suggest not to use xterm or gnome-terminal to provide a terminal for sudo, but to deal with the "sorry, you must have a tty to run sudo"message directly.
我建议不要使用 xterm 或 gnome-terminal 为 sudo 提供终端,而是直接处理“对不起,您必须有一个 tty 才能运行 sudo”消息。
There is a requiretty
option in the sudoers
file that makes sudo demand a terminal. If this option is unset with !requiretty
and the command is executed with the NOPASSWD option sudo should run without the need to open a new terminal window. There are more details in this serverfault post.
文件中有一个requiretty
选项sudoers
使 sudo 需要终端。如果未设置此选项!requiretty
并且使用 NOPASSWD 选项执行命令,则 sudo 应该运行而无需打开新的终端窗口。此 serverfault帖子中有更多详细信息。
That is how sudo is used for instance in cron scripts.
这就是 sudo 在 cron 脚本中的使用方式。
Since requiretty option provides additional security in an environment where sudo is used not only in cron scripts but to let remote users issue commands with elevated privileges, the action of !requiretty can be restricted.
由于 requiretty 选项在 sudo 不仅在 cron 脚本中使用而且让远程用户发出具有提升权限的命令的环境中提供额外的安全性,因此可以限制 !requiretty 的操作。
User_Alias LOCAL_USERS = john, mary
Cmnd_Alias NETWORK_SCRIPTS = /sbin/ifup, /sbin/ifdown
Defaults!NETWORK_SCRIPTS !requiretty
LOCAL_USERS ALL = NOPASSWD: NETWORK_SCRIPTS
回答by Nicholas Wilson
In general, sudo or su need to prompt for a password, or programs could escalate their privileges without user intervention. If you application needs to elevate for some purpose, you will need to use an xterm or similar. There are difficulties though in getting the return code back (konsole might need --nofork
and gnome-terminal might need --disable-factory
, but the options sadly vary by version), and it's not easy to get it right on every system. Most unixes and linux distributions provide xterm, but some old Fedora/RHEL/CentOS provide X without xterm, so it's another dependency to think about.
通常,sudo 或 su 需要提示输入密码,否则程序可以在没有用户干预的情况下提升其权限。如果您的应用程序出于某种目的需要提升,您将需要使用 xterm 或类似工具。虽然在获取返回码方面存在困难(konsole 可能需要--nofork
,gnome-terminal 可能需要--disable-factory
,但遗憾的是选项因版本而异),并且在每个系统上都不容易得到它。大多数 unix 和 linux 发行版都提供 xterm,但一些旧的 Fedora/RHEL/CentOS 提供没有 xterm 的 X,因此这是另一个需要考虑的依赖项。
The command launched by xterm -e sudo -- ...
can then do the standard double-fork and setsid. Once the user has entered his password in the xterm, it goes away immediately, but your command runs in the background with elevated privileges. It can connect back to the original program using a socket or fifo to run as a root co-process.
然后启动的命令xterm -e sudo -- ...
可以执行标准的双叉和 setid。一旦用户在 xterm 中输入了他的密码,它就会立即消失,但您的命令会在后台以提升的权限运行。它可以使用套接字或 fifo 连接回原始程序以作为根协进程运行。
The daemon
or disown
commands or similar might be useful if you want to wrap an existing application in a double-fork & setsid (eg, xterm -e sudo -- daemon system-config-network
or perhaps xterm -e sudo -- bash -c "system-config-network & disown -a"
).
该daemon
或者disown
,如果你想要包装的双叉和setsid现有的应用程序(例如,命令或类似的可能是有用的xterm -e sudo -- daemon system-config-network
或可能xterm -e sudo -- bash -c "system-config-network & disown -a"
)。
回答by Micha? ?rajer
If you run your code within X session, then you can use gksudo
instead of sudo
:
如果您在 X 会话中运行代码,则可以使用gksudo
代替sudo
:
gksudo -m "Your message" /command/to/run
It will prompt user for password (if needed) using nice GUI interface. No need to xterm or gnome-terminal.
它将使用漂亮的 GUI 界面提示用户输入密码(如果需要)。不需要 xterm 或 gnome-terminal。
Effect will be more secure than allowing particular command to run without any password and solution will be more consistent to what users are used to.
效果将比允许在没有任何密码的情况下运行特定命令更安全,并且解决方案将更符合用户习惯。