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?


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`
mkdir "${name}" && cp *.dsl "${name}"/ 
tar czf "${name}.tar.gz" "${name}"/ && ls -l "$( pwd )/${name}".tar.gz

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.

| improve this answer | |

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:


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.

| improve this answer | |

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 *'
| 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

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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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