How to make sudo in 2 tabs ask for the password only once

itermsudoterminal

I have a script to automate starting a few servers on the terminal. These are server processes of the software I am developing. They can be recompiled and changed multiple times a day. The binaries can live in different places (working copies).

My script uses

 set sudo to do shell script "sudo -v "

to ask for the root password once, then creates a new iterm2 terminal window (and some tabs) and tries to start the servers with sudo:

#!/usr/bin/osascript
on run args
    set sudo to do shell script "sudo -v " -- to ask for password once only
    tell application "iTerm"
        create window with default profile
        tell current window
            tell current session
                write text "sudo /some/server"
            end tell
            -- more tabs and more sudos go here
        end tell
    end tell
end run

Everything worked fine on El Capitan, but not on High Sierra.

If the sudo password is not cached, the sudo -v asks for my password and then proceeds — as expected. If I ran sudo before and the password is cached, the sudo -v does not ask for a password, again, as expected.
But in both cases each of the subsequent sudos in the new window does ask for my password, making the whole thing inconvenient.

When I add with administrator privileges to the initial sudo it does ask for my password in a popup dialog but the following sudo calls with write text still ask for the password on the shell.
(I use write text and the terminal to keep an eye on these servers' outputs.)

I can run regular sudo commands just fine and it does cache the password.

How can I use sudo in Applescript in a new iTerm2 window and have it ask for the password only once?

EDIT: Looks like this is not related to Applescript but to iterm and High Sierra. When I open a terminal window with 2 tabs, I need to enter my password in both when I run e.g., sudo ls. After that both tabs have the sudo password cached, but running sudo in one tab does not make the other tab use the cached sudo password.

So the real question is probably: how do I get iTerm2 to cache the sudo password and share that between tabs and windows?

Best Answer

Try adding:

Defaults !tty_tickets

to /etc/sudoers. Tested on 10.13.6 (High Sierra).

From the man page:

If set, users must authenticate on a per-tty basis. With this flag enabled, sudo will use a separate record in the time stamp file for each tty. If disabled, a single record is used for all login sessions. This flag is on by default.