在 Vagrant 上配置期间如何以另一个用户身份执行命令?
How execute commands as another user during provisioning on Vagrant?
Vagrant
在提供期间以 root
的身份执行我的脚本。但是我想在配置期间以另一个用户的身份执行一些命令。
我现在是这样的:
su - devops -c "git clone git://github.com/sstephenson/rbenv.git .rbenv"
su - devops -c "echo 'export PATH=\"$HOME/.rbenv/bin:$PATH\"' >> ~/.bash_profile"
su - devops -c "echo 'eval "$(rbenv init -)"' >> ~/.bash_profile"
su - devops -c "git clone git://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build"
su - devops -c "echo 'export PATH=\"$HOME/.rbenv/plugins/ruby-build/bin:$PATH\"' >> ~/.bash_profile"
su - devops -c "source ~/.bash_profile"
但我想让它变得更好,像这样:
#become devops user here
git clone git://github.com/sstephenson/rbenv.git .rbenv
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
git clone git://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build
echo 'export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"' >> ~/.bash_profile
source ~/.bash_profile
# back to the root user here
这可能吗?谢谢!
当运行配置时,默认情况下 vagrant 将 运行 作为 root 用户,您可以通过将配置设为
来更改此行为
config.vm.provision "shell", privileged: false blablabla
vagrant 将使用定义为 config.ssh.username
的用户,如果未设置,默认为 vagrant 用户
如果您的系统上有多个用户,但仍想使用另一个用户,我会做的是编写一个脚本并以 su -l newuser -c "path to shell script"
的身份执行脚本
如果您想让脚本与其他脚本内联,您可以使用如下命令序列。
sudo -u devops /bin/sh <<\DEVOPS_BLOCK
# Become devops user here
id
whoami
# Back to the root user here
DEVOPS_BLOCK
Vagrant
在提供期间以 root
的身份执行我的脚本。但是我想在配置期间以另一个用户的身份执行一些命令。
我现在是这样的:
su - devops -c "git clone git://github.com/sstephenson/rbenv.git .rbenv"
su - devops -c "echo 'export PATH=\"$HOME/.rbenv/bin:$PATH\"' >> ~/.bash_profile"
su - devops -c "echo 'eval "$(rbenv init -)"' >> ~/.bash_profile"
su - devops -c "git clone git://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build"
su - devops -c "echo 'export PATH=\"$HOME/.rbenv/plugins/ruby-build/bin:$PATH\"' >> ~/.bash_profile"
su - devops -c "source ~/.bash_profile"
但我想让它变得更好,像这样:
#become devops user here
git clone git://github.com/sstephenson/rbenv.git .rbenv
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
git clone git://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build
echo 'export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"' >> ~/.bash_profile
source ~/.bash_profile
# back to the root user here
这可能吗?谢谢!
当运行配置时,默认情况下 vagrant 将 运行 作为 root 用户,您可以通过将配置设为
来更改此行为config.vm.provision "shell", privileged: false blablabla
vagrant 将使用定义为 config.ssh.username
的用户,如果未设置,默认为 vagrant 用户
如果您的系统上有多个用户,但仍想使用另一个用户,我会做的是编写一个脚本并以 su -l newuser -c "path to shell script"
如果您想让脚本与其他脚本内联,您可以使用如下命令序列。
sudo -u devops /bin/sh <<\DEVOPS_BLOCK
# Become devops user here
id
whoami
# Back to the root user here
DEVOPS_BLOCK