Shell provision script not creating .bash_aliases

Shell provision script not creating .bash_aliases

I'm using Vagrant with VirtualBox to create an Ubuntu guest VM, on a Windows 10 host. Here's my Vagrantfile:

# vi: set ft=ruby :

Vagrant.configure(2) do |config|
  config.vm.box = "ubuntu/trusty64"

  config.vm.network "forwarded_port", guest: 8080, host: 8100

  config.vm.provision "shell", path: "./bootstrap.sh"
end

And here's the bootstrap.sh file.

printf "Working directory is: %s." $(pwd)

if [ ! -f '~/.bash_aliases' ]; then
    printf "# This is a comment." > ~/.bash_aliases ;
fi;

ls -la

Yet from some reason, it never creates the .bash_aliases in the vagrant user's directory. I can SSH into the box and run the exact same command and it works. How do I create the .bash_aliases file in vagrant user's directory?

答案1

Figured it out. There are at least two solutions.

Solution 1 - Run with privileged set to false

In Vagrantfile, change this line...

config.vm.provision "shell", path: "./bootstrap.sh"

to this:

config.vm.provision "shell", path: "./bootstrap.sh", privileged: false

Vagrant will run as the vagrant user, not root (sudo).

Solution 2 - Create file directly at /home/vagrant/.bash_aliases

Change...

if [ ! -f '~/.bash_aliases' ]; then
    printf "# This is a comment." > ~/.bash_aliases ;
fi;

to this:

if [ ! -f /home/vagrant/.bash_aliases ]; then
    printf "# This is a comment." > /home/vagrant/.bash_aliases ;
fi;

This writes directly to the vagrant user's directory.

I went with Solution 2.

相关内容