Friday, May 3, 2024
3
rated 0 times [  3] [ 0]  / answers: 1 / hits: 6160  / 2 Years ago, thu, december 16, 2021, 6:05:01

For example if i run sudo apt-get install vlc,it asks me to enter the sudo password.



If i not enter the password for sudo it [sudo] password for avinash: remains for longtime.



How do i set this sudo password waiting time?If this waiting time expires,it automatically shows time expires.Is that possible?



Note: I am not asking about how long sudo remembers the password(RootSudoTimeout - Community Ubuntu Documentation).


More From » command-line

 Answers
0

This is not directly possible from sudo itself, but it is possible with some hackish technique.



sudo_timeout.sh :



#!/bin/bash

timeout=10 #seconds

set -m

echoerr() { echo "$@" 1>&2; }

keep_eye_on() {
pid=$1
time_passed=0
while kill -0 $pid &> /dev/null; do
sleep 1
let time_passed=time_passed+1
if [ $time_passed -ge $timeout ]; then
echoerr "Timeout reached."
kill -9 $pid
exit 1
fi
done
}

if [ -z "$1" ]; then
echoerr "Please specify a process to run!"
exit 1
fi;

sudo $@ &
pid=$!

keep_eye_on $pid &
while true; do
if kill -0 $pid &> /dev/null; then
fg sudo > /dev/null; [ $? == 1 ] && break;
else
break
fi
done


The timeout variable holds the timeout in seconds to wait prior killing the sudo process that is asking for password.



Usage:



./sudo_timeout.sh <command>


Example:



./sudo_timeout.sh ls -al


In case the timeout is reached you get:



alex@MaD-pc:~$ ./sudo_timeout.sh ls -al
[sudo] password for alex: Timeout reached.
./sudo_timeout.sh: line 34: 14583 Killed sudo $@


In case you type in your password prior the timeout, then the command executes normally.



Disclaimer: The above is tested with simple commands like ls and nano, both with and without arguments, but I cannot guarantee that it will work in every case because I haven't thoroughly tested it, it's just something I came up with.


[#27618] Saturday, December 18, 2021, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
allally

Total Points: 487
Total Questions: 106
Total Answers: 110

Location: Laos
Member since Sun, Jul 3, 2022
2 Years ago
;