0

I have a bash script that opens a mate-terminal with 3 tabs, each tab having a command that requires sudo. The problem is that I have to put in my sudo password on each of the 3 tabs, which is not ideal. Can I make it so I only have put in my sudo password once? Basically, I want to double click the shell script, put in my sudo password once, and have all 3 commands execute. How can I do this? This script looks like this:

#!/bin/sh

mate-terminal \
--tab -e "sudo /sbin/mgetty -s 115200 -D /dev/ttyUSB0" \
--tab -e "sudo tail -f /var/log/mgetty/mg_ttyUSB0.log" \
--tab -e "sudo tail -f /var/log/messages"
Sega dude
  • 1,103
  • 3
  • 12
  • 29
  • 1
    Have you considered modifying `/etc/sudoers` to give the user that runs this script passwordless sudo access for these three commands? – Charles Duffy Nov 08 '16 at 00:23
  • @CharlesDuffy How would I do that? – Sega dude Nov 08 '16 at 00:24
  • 1
    `man sudoers`. Questions about its format are system-administration matters, out-of-scope on StackOverflow. – Charles Duffy Nov 08 '16 at 00:24
  • However, the relevant lines are likely to look something like `segadude ALL=NOPASSWD: /sbin/mgetty -s 115200 -D /dev/ttyUSB0`, and likewise for the others. – Charles Duffy Nov 08 '16 at 00:26
  • 1
    Try adding `sudo --validate` as the first line, after `#!/bin/sh` line but before `mate-terminal` – Paul Nov 08 '16 at 00:26
  • @Paul I don't think that will work. Each tab is a separate virtual terminal, and `sudo` authentication is per-terminal. – Barmar Nov 08 '16 at 00:50
  • Possibly not, thus 'try' – Paul Nov 08 '16 at 00:52
  • @Paul Unfortunately that didn't work. It still asks for my sudo password on each tab. – Sega dude Nov 08 '16 at 00:58
  • Do you mind if the password could be retrieved during the time the scripts are running? If you don't consider this a problem, you could proceed as follows: In your master script, you ask the user for the password and store it in an environment variable. In the tabs, you don't run sudo directly, but run a script which invokes sudo via `expect` (see *man expect*) and have the password provide interactively to sudo. – user1934428 Nov 08 '16 at 07:47
  • @user1934428, ...that's quite unfortunate from a security perspective, however. A hybrid approach that ran the programs *outside* the terminal sending output of each to a socket or named pipe, and then just connected each tab to that socket, would be far more sensible. – Charles Duffy Nov 08 '16 at 14:56
  • I think I am going to go with @CharlesDuffy's first suggestion and edit `/etc/sudoers`. This script is for my own personal use and I am the only one who uses this machine. – Sega dude Nov 08 '16 at 17:56

0 Answers0