2

Is there a way in Linux to make sudo command to remember the password the user entered for in the first of the lines?

For example, for a list of commands that the user has to enter, the some of then requiring a sudo prefix, how can one make sure that if the user copy+pastes the instructions into a terminal all in one go, is only asked the password once?

Example:

mkdir ~/acpiinfo ; cd ~/acpiinfo
sudo acpidump > acpidump.txt
# enter password 
sudo acpixtract acpidump.txt
ls *.dat | while read i; do iasl -d "${i}"; done
pid=`sudo dmidecode -s system-product-name`
vid=`sudo dmidecode -s system-version`
name=$pid.$vid
mkdir "${name}" && cp *.dsl "${name}"/ 
tar czf "${name}.tar.gz" "${name}"/ && ls -l "$( pwd )/${name}".tar.gz

4 Answers 4

9

It should do this by default. From the sudo manpage:

Once a user has been authenticated, a time stamp is updated and the user may then use sudo without a password for a short period of time (5 minutes unless overridden in sudoers).

If your system is not doing this, maybe check /etc/sudoers. From the sudoers manpage:

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 5. Set this to 0 to always prompt for a password. If set to a value less than 0 the user's timestamp will never expire. This can be used to allow users to create or delete their own timestamps via sudo -v and sudo -k respectively.

So it sounds like the timestamp_timeout variable may have been set to 0. Fire up visudo and comment that line out or set it to something of your liking.

1

Double sudo is not necessary:

sudo sh -c "apt-get update && apt-get -y dist-upgrade && apt-get -y autoremove && apt-get autoclean"

This works fine even if one command can take very long.

1

The sudo bash -c 'sudo cmd1; cmd2; sudo cmd3;...' idiom seems to remember the password for a list of commands.

# test cases
sudo bash -c 'sudo ls -ld *; sleep 500; sudo ls -ld *'
sudo -k
sudo bash -c 'sudo ls -ld *; sudo -k; sleep 5; sudo ls -ld *'
1
  • This doesn't remember the password for a list or series of commands, it just crams everything into one command.
    – Caleb
    Commented Aug 24, 2011 at 21:49
1

sudo -s will start a shell as root ( possibly prompting you for your password ). You can then enter several commands without the need to prefix them with sudo. When finished, exit the root shell.

2
  • is it the same as sudo su? Commented Jan 17, 2013 at 7:48
  • @動靜能量, yes, only without running two programs.
    – psusi
    Commented Jan 17, 2013 at 13:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .