Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tell if I am inside a Vagrant host?

Whats a bulletproof way to determine if I am running inside a vagrant machine?

Guest OS is Debian Linux, though if there are indicators per-os that would be great to have documented as well.

like image 974
ThorSummoner Avatar asked Mar 05 '15 20:03

ThorSummoner


3 Answers

Provisioning a file that you can check the existence of seems like the most reliable way to handle this.

Another option if you don't want to write files to the box would be to check for the existence of the vagrant user itself:

grep -q '^vagrant:' /etc/passwd && echo 'Vagrant environment: true'

This is not foolproof as others have indicated, and it is possible (although uncommon) to have a vagrant box that uses a different user to connect as.

Checking the user's existence also would not work reliably if you have machines in your environment with a user account called vagrant that are not actually vagrant boxes, but that would also be fairly uncommon.

like image 154
mvermaes Avatar answered Oct 19 '22 21:10

mvermaes


AFAIK, there isn't a way outside of your own customizations. One idea that comes to mind is to touch a file that you then reference from your apps. Add something like config.vm.provision "shell", inline: "touch /etc/is_vagrant_vm" to the bottom of your Vagrantfile and base your conditionals around the existence of that file.

like image 30
Jamey Avatar answered Oct 19 '22 21:10

Jamey


I don't know if there is any bulletproof way for this, but one thing I often do is to config my vagrant environment's shell UI to be different from the shell UI of my host machine. This way I can tell the difference at first glance. It also helps if you want to distinguish among multiple vagrant environments.

To customize the shell UI, oh-my-zsh will come in handy.

like image 1
Thanh Nguyen Avatar answered Oct 19 '22 22:10

Thanh Nguyen