Search code examples
bashubuntuterminalsudo

How can I run a command in a separate terminal using sudo without further user interaction


I am trying to automate the running of several tasks, but I need to run them as sudo.

I want to run them in separate terminals so I can watch the output of each.

Here is a sort of minimal example I have setup (because what I am trying to do is more complicated)

Setup two files - note that data is readable as root only and contains 3 lines of example text:

-rw------- 1 root    root      33 Nov 15 09:29 data
-rwxrwxrwx 1 root    root      11 Nov 15 09:30 test.sh*

test.sh looks like:

#!/bin/bash
cat data
read -p "Press enter to continue"

Also I have user level variable called "SESSION_MANAGER" that is setup in the bash startup... which seems to cause some issues (see later example)

So now I want to spawn various terminals running this script. I tried the following:

Attempt 1

xfce4-terminal -e './test.sh'

output:

cat: data: Permission denied
Press enter to continue

Attempt 2 - using sudo at the start

~/src/sandbox$ sudo xfce4-terminal -e './test.sh' 
Failed to connect to session manager: Failed to connect to the session manager: SESSION_MANAGER environment variable not defined

(xfce4-terminal:6755): IBUS-WARNING **: The owner of /home/openbts/.config/ibus/bus is not root!

output:

this is some data
more data
end
Press enter to continue

here you can see that the output of the data file is print ok, but I had some issue with the session manager variable.

Attempt 3 - using sudo in the command

~/src/sandbox$ xfce4-terminal -e 'sudo ./test.sh' 

output:

[sudo] password for openbts: this is some data more data end Press enter to continue

here you can see that everything was well... but I had to enter my password again, which somewhat kills my automation :(

Attempt 4 - start as root

~/src/sandbox$ sudo su
root@openbts:/home/openbts/src/sandbox# xfce4-terminal -e './test.sh'
Failed to connect to session manager: Failed to connect to the session manager: SESSION_MANAGER environment variable not defined

output:

this is some data
more data
end
Press enter to continue

Here, again the output looks good, but I have this SESSION_MANAGER issue... Also the new xfce4-terminal comes out with messed up font/look - I guess this is the root users settings.

Questions

How can I run multiple instances of test.sh each in a new terminal and not have to enter passwords (or interact at all). I can enter the password once at the start of the process (in the original terminal)?

As you can see I got this sort-of working when going in a sudo su, but this issues here are the SESSION_MANAGER variable - not sure if that is an issue, but its very messy looking, but also the xcfe4-terminal looks bad (I guess I can change the root settings to the same as my user settings). So how can I avoid the SESSION_MANAGER issue when running as root?


Solution

  • If you change user-id before you launch your separate terminal, you will see the session-manager issue. So the solution is to run the sudo in the terminal.

    You do not want to type passwords in the sudo. You can do that by adding

    yourname ALL=(ALL) NOPASSWD: ALL
    

    to /etc/sudoers (at least on slackware). You could also try to set the permissions on the files correct so you would not need root all the time.

    Note that adding that line has security implications; you might want to allow just cat without password (in your example), or make even more elaborate rules for sudo. The line I gave is just an example. Personally, I would look at file-permissions.