我有这个bash脚本。我使用以下命令运行它: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 Password\n\
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..."
将用户core
称为500
,将root
称为0
无效。我收到了以下错误:
sudo: unknown user: 500
sudo: unable to initialize policy plugin
我之所以想要使用用户名或组号,是因为我想让它成为多平台(如果可以选择500或1000之间会很好,因为我读了一些Linux发行版'默认用户是1000,但是在我的情况下是500)。
我们可以检测哪个用户开始以sudo
的身份运行此脚本吗?因为这可能是一个多用户Linux的方式。
编辑:
这是不需要超级用户权限的脚本,在用户的主文件夹下创建一个新的路径变量:
#!/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 Password\n\
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..."
答案 0 :(得分:6)
来自sudo
man pages:
-u user, - user = user
以默认目标用户(通常为root)以外的用户身份运行该命令。用户可以是用户名或前缀为“#”字符的数字用户ID(UID)(例如,#0表示UID 0)。将命令作为UID运行时,许多shell要求使用反斜杠('\')转义'#'。某些安全策略可能会将UID限制为密码数据库中列出的UID。只要未设置targetpw选项,sudoers策略就允许不在密码数据库中的UID。其他安全策略可能不支持此功能。
因为#
也用于开始注释,所以你要么想用反斜杠转义它,要么引用它。例如sudo -u "#${USER}"
或sudo -u \#${USER}