Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to disable sudo timeout in the current shell?

Tags:

linux

sudo

Certain applications (for example yay) uses sudo to run some commands as root. This means that I have to sit in front of the terminal and wait until I'm prompted by sudo for a password, then enter it manually to make yay continue. I can avoid this by running a command as sudo first (like sudo -v). That way yay can run commands as sudo without it prompting me for a password.

But some times, applications take so long to install that the sudo session times out (15 minutes) and sudo re-prompts me for a password. Since I usually leave my computer or switch to a different workspace while this happens, the sudo password prompt usually times out which is super annoying.

I don't want to disable the sudo timeout altogether, since it's there for a reason. However, when I expect that a command that uses sudo will take a long time, I would like to be able to disable sudo timing out for the current shell only.

Something like:

sudo --disable-timeout

Now I should be able to run sudo in the current shell, without ever having to re-enter my password, or until I run sudo -k.

Is something like this possible, or will I have to write a wrapper script that regularly refreshes the sudo session for me?

like image 608
Hubro Avatar asked Nov 19 '19 11:11

Hubro


2 Answers

Here is one possible workaround. Write a script like this (e.g. sudo-stay-validated.sh):

#!/bin/bash

while true; do
    sudo -v
    sleep 60
done

Run the script in the terminal where sudo should stay validated:

$ bash sudo-stay-validated.sh

Press Ctrl+Z to place it in the background, then remember to run $ bg to resume the script in the background.

This keeps sudo validated in the current shell until it's closed.

like image 159
Hubro Avatar answered Oct 02 '22 16:10

Hubro


There seems no easy way to do this for the current shell but you can set a timeout globally.

In order to set a different timeout (globally) than the default (=15 min) you can edit /etc/sudoers:

sudo visudo                                       # opens /etc/sudoers for editing
# change the following line:
# Defaults    env_reset
# to:
Defaults        env_reset,timestamp_timeout=30    # timeout in minutes

Or:

cd /etc/sudoers.d
sudo visudo -f username
Defaults        env_reset,timestamp_timeout=30    # timeout in minutes

Special values:

  • -1: no timeout
  • 0: get prompted every single time
like image 37
holzkohlengrill Avatar answered Oct 02 '22 14:10

holzkohlengrill