Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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
share|improve this question
add comment

4 Answers 4

up vote 1 down vote accepted

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.

share|improve this answer
add comment

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.

share|improve this answer
    
is it the same as sudo su? –  動靜能量 Jan 17 '13 at 7:48
    
@動靜能量, yes, only without running two programs. –  psusi Jan 17 '13 at 13:49
add comment

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 *'
share|improve this answer
    
This doesn't remember the password for a list or series of commands, it just crams everything into one command. –  Caleb Aug 24 '11 at 21:49
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.