I have this bash script. I run it with the following command: sudo ./install.sh
#!/bin/bash MYSQL_PASS="password" USER="500" SUPERUSER="0" sudo -u ${USER} echo -e "Installing docker-compose from GitHub Latest release..." && sudo -u ${SUPERUSER} mkdir -p /opt/bin && sudo -u ${SUPERUSER} curl -L https://github.com/docker/compose/releases/download/1.5.2/docker-compose-`uname -s`-`uname -m` > /opt/bin/docker-compose && sudo -u ${SUPERUSER} chmod +x /opt/bin/docker-compose && sudo -u ${USER} echo -e "docker-compose installed, verifying..." && sudo -u ${USER} docker-compose -v && sudo -u ${USER} echo -e "Creating folder structure..." && sudo -u ${USER} mkdir -p ~/mysql ~/sqlbackup ~/work/lemp ~/www && sudo -u ${USER} echo -e "Cloning git repo into "~/work/lemp"..." && sudo -u ${USER} git clone https://github.com/DJviolin/LEMP.git ~/work/lemp && sudo -u ${USER} echo -e "Showing working directory..." && sudo -u ${USER} ls -al ~/work/lemp && sudo -u ${USER} echo -e "Starting docker images and containers generation..." && sudo -u ${USER} echo -e " # Set MySQL Root Passwordn MYSQL_ROOT_PASSWORD=${MYSQL_PASS}" > ~/work/lemp/mariadb/mariadb.env && sudo -u ${USER} cat ~/work/lemp/mariadb/mariadb.env && sudo -u ${USER} docker-compose build ~/work/lemp && sudo -u ${USER} echo -e "LEMP stack has built...nRun the service with ./service-start.sh command." && sudo -u ${USER} echo -e "All done! Exiting..."
Calling the user core
as 500
and root
as 0
not working. I’ve got the following error:
sudo: unknown user: 500 sudo: unable to initialize policy plugin
The reason why I want to use user or group numbers, because I want to make it multiplatform (would be nice if it can choose between 500 or 1000, because I read some linux distros’ default user is 1000, but in my case is 500).
Can we detect which user started to run this script as sudo
? Because probably this is the way to go on a multi-user linux.
EDIT:
This is the script without the need of the superuser privileges, making a new path variable under the user’s home folder:
#!/bin/bash set -e echo -e "Installing docker-compose from GitHub Latest release..." mkdir -p ~/bin curl -L https://github.com/docker/compose/releases/download/1.5.2/docker-compose-`uname -s`-`uname -m` > ~/bin/docker-compose chmod +x ~/bin/docker-compose export PATH="~/bin:$PATH" echo -e "docker-compose installed, verifying..." docker-compose -v echo -e "Creating folder structure..." mkdir -p ~/mysql ~/sqlbackup ~/work/lemp ~/www echo -e "Cloning git repo into "~/work/lemp"..." git clone https://github.com/DJviolin/LEMP.git ~/work/lemp echo -e "Showing working directory..." ls -al ~/work/lemp echo -e "Starting docker images and containers generation..." echo -e " # Set MySQL Root Passwordn MYSQL_ROOT_PASSWORD=`openssl rand -base64 37 | sed -e 's/^(.{37}).*/1/g'`" > ~/work/lemp/mariadb/mariadb.env cat ~/work/lemp/mariadb/mariadb.env echo -e "LEMP stack has built...nRun the service with ./service-start.sh command." echo -e "All done! Exiting..."
Advertisement
Answer
From the sudo
man pages:
-u user, –user=user
Run the command as a user other than the default target user (usually root ). The user may be either a user name or a numeric user ID (UID) prefixed with the ‘#’ character (e.g. #0 for UID 0). When running commands as a UID, many shells require that the ‘#’ be escaped with a backslash (‘’). Some security policies may restrict UIDs to those listed in the password database. The sudoers policy allows UIDs that are not in the password database as long as the targetpw option is not set. Other security policies may not support this.
Because #
is also used to begin comments, you’ll either want to escape it with a backslash or quote it. For example sudo -u "#${USER}"
or sudo -u #${USER}