42

I enter my first sudo command. I enter my password. For a while, I wont have to enter my password for subsequent sudo commands.

Now the question. I am someone who opens a lot of terminals. It would be very convenient if I don't have to enter passwords when I use sudo in the terminals I open after my first sudo, for the short time when I wont have to enter password for sudo in the terminal from which I have used sudo for the first time. (Sorry for the long sentence!)

Is it possible? If not, why? If yes, how?

daltonfury42
  • 5,459
  • 5
  • 31
  • 62
  • 5
    Have you considered that by doing so you're opening the security hole which that mechanism exists to close? The reason that you have to enter your password with sudo is to guard against the circumstance that an attacker somehow has gained access to a login session in your name. If you disable it and someone manages to hijack one of your terminal sessions (are they all on the same console?) or in some other way gains access to a login session in your name without knowing your password, they will be able to run any command as root. Not a very likely scenario, but something you should consider. – Pepijn Schmitz Jul 01 '15 at 15:44
  • 1
    There should be a productivity tag, just to find questions like this. Else, have to follow the old way of keep waiting for that moment of ingenuity to come up with such questions. – saurabheights Jan 18 '21 at 13:36

3 Answers3

64

Sure it is. Run sudo visudo and add this line to your sudoers file:

Defaults        !tty_tickets

As explained in man sudoers:

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

By setting tty_tickets to off (that's what the ! means), you enable a single authentication to be shared by multiple sessions.

terdon
  • 98,183
  • 15
  • 197
  • 293
0

So here is what you want to do to only have sudo ask for your password once per boot:

/etc/sudoers.d/00_prompt_once:

## Only ask for the password once for all TTYs per reboot.
## See https://askubuntu.com/a/1278937/367284 and
##     https://github.com/hopeseekr/BashScripts/
Defaults !tty_tickets
Defaults timestamp_timeout = -1
  • This is essentially disabling sudo completely: most of us work on machines that rarely reboot (either servers, or laptops), which means that your approach would allow anyone who sits in front of the machine to run any command at all, as long as the machine is on and someone has at any time during this boot entered the password. This isn't what the question was asking for, and it really isn't a good idea unless you truly don't care about security at all. – terdon Oct 01 '20 at 08:15
  • While this is true terdon, this means anyone with access to an account, however, for personal computers with one user account and no root password this means that they would have to be on the only account first which means they already have access where they shouldn't. If you use the system as a personal computer I don't see the issue as long as you lock your screen whenever you leave and don't share your private account password. – Jason Ivey Oct 26 '22 at 19:53
0

In your sudoers.d file

sudo EDITOR=vim visudo -f /etc/sudoers/<filename>

Add the following:

Defaults    timestamp_timeout=-1

man sudoers reveals the following:

sudoers uses per-user time stamp files for credential caching. ...  The user may then use sudo without a password for a short period of time  (15 minutes unless overridden by the timestamp_timeout  option). ...   The timestamp_type option can be used to select the type of time stamp record  sudoers will use.

Further down:

 timestamp_timeout
                   Number of minutes that can elapse before sudo will ask for a passwd again.  The timeout may include a fractional component if minute granularity is insufficient, for example 2.5.  The default is 15.  Set
                   this to 0 to always prompt for a password.  If set to a value less than 0 the user's time stamp will not expire until the system is rebooted.  This can be used to allow users to create or delete their own
                   time stamps via “sudo -v” and “sudo -k” respectively.