Search code examples
linuxshellcommand-linextermgnome-terminal

How to Open xterm window from a terminal and run a command in background from xterm?


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

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.

Can you give suggestion how to disable the window from closing by the user or how to make it invisible to the user?

Note: you can test this by using system-config-network command instead of ifdown and ifup


Solution

  • 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.

    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.

    That is how sudo is used for instance in cron scripts.

    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.

       User_Alias LOCAL_USERS = john, mary
       Cmnd_Alias NETWORK_SCRIPTS = /sbin/ifup, /sbin/ifdown
       Defaults!NETWORK_SCRIPTS     !requiretty
       LOCAL_USERS ALL = NOPASSWD: NETWORK_SCRIPTS