If I run this script, how do I pass super user permissions to it? I wrote this just to setup new machines with the basics. I don't want to run every command with elevated permissions, but the commands that do have sudo
I want to run with them.
How do I have some commands run with sudo
and others run as the regular user?
#!/bin/sh
# If Linux, install nodeJS
if $(uname) = 'Linux';
then
export IS_LINUX=1
# Does it have aptitude?
if -x "which apt-get";
then
export HAS_APT=1
# Install NodeJS
sudo apt-get install --yes nodejs
fi
# Does it have yum?
if -x "which yum" ;
then
export HAS_YUM=1
# Install NodeJS
sudo yum install nodejs npm
fi
# Does it have pacman?
if -x "which pacman" ;
then
export HAS_PACMAN=1
# Install NodeJS
pacman -S nodejs npm
fi
fi
# If OSx, install Homebrew and NodeJS
if $(uname) = 'Darwin' ;
then
export IS_MAC=1
if test ! "$(which brew)"
then
echo "================================"
echo " Installing Homebrew for you."
echo "================================"
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
export HAS_BREW=1
elif -x 'which brew' ;
then
export HAS_BREW=1
brew update
fi
# Install NodeJS
brew install --quiet node
fi
# Does it have python?
if -x "which python" ;
then
export HAS_PYTHON=1
if -x "which pip" ;
then
pip list --outdated | cut -d ' ' -f1 | xargs -n1 pip install -U
export HAS_PIP=1
fi
fi
# Does it have node package manager?
if -x "which npm" ;
then
export HAS_NPM=1
else
echo "NPM install failed, please do manually"
fi
# Does it have ruby gems?
if -x "which gem" ;
then
export HAS_GEM=1
fi
The rest of the bash script (that I didn't include for length) installs packages from an array using npm, apt, yum, brew, or pacman, depending on the machine. It only installs simple things like git
, wget
, etc.
-
Related: sudo in non-interactive script.G-Man Says 'Reinstate Monica'– G-Man Says 'Reinstate Monica'2018年02月08日 19:42:37 +00:00Commented Feb 8, 2018 at 19:42
1 Answer 1
First time sudo
is invoked password is prompted for. Then, depending on configuration, if invoked within N minutes (default 5 minutes IIRC), one do not need to enter password again.
You could do something like:
sudo echo >/dev/null || exit 1
or perhaps something like:
sudo -p "Become Super: " printf "" || exit 1
at start of script.
If you want to prevent anyone from doing sudo ./your_script
you should check EUID as well (bash):
if [[ $EUID -eq 0 ]]
then
printf "Please run as normal user.\n" >&2
exit 1
fi
or something like:
if [ "$(id -u)" = "0" ]
...
In any case also check out which shell you targetr. I.e.
- https://wiki.debian.org/DashAsBinSh
- https://wiki.ubuntu.com/DashAsBinSh
- https://lwn.net/Articles/343924/
etc.
To "keep it alive" one could do something like:
while true; do
sleep 300
sudo -n true
kill -0 "$$" 2>/dev/null || exit
done &
-
Great idea! How do I then allow only some parts of the bash script to run as
sudo
and others to run as a normal user? While maintaining the ability to usesudo
without it timing out?AtHeartEngineer– AtHeartEngineer2016年02月10日 23:04:16 +00:00Commented Feb 10, 2016 at 23:04 -
@Tyler.Exposure: Expanded a bit. You allow any command to be run as
sudo
by prepending it bysudo
... I might not get what you are asking.Runium– Runium2016年02月11日 21:52:42 +00:00Commented Feb 11, 2016 at 21:52 -
Awesome, ok I think sleep -n works!!! Thank you!!!AtHeartEngineer– AtHeartEngineer2016年02月12日 22:40:44 +00:00Commented Feb 12, 2016 at 22:40