How to ensure your vagrant is running with administrative privileges

Vagrant Logo

If you have used vagrant on a Windows host, you’ve undoubtedly been bit once or twice when trying to create a symlink inside your vagrant guest inside shared folder on the host, especially if your vagrant guest is linux. The reason is that under windows, creating a symlink (using mklink.exe) requires Administrative privileges. If your ‘vagrant up’ command was not executed from an Administrative Command Prompt, your VM will not be able to create these symlinks on the host filesystem.

The solution to the symlink problem is easy, make sure you run your vagrant commands from an Administrative Command Prompt. But, should you forget, a lot of time can be wasted powering off your vm and restarting under new security context.

Wouldn’t it be nice if that initial ‘vagrant up’ command alerted you that you did not have the proper privileges? Here is how you can make that happen:

Add the following lines to the top of your Vagrantfile. Once added, running ‘vagrant up’ (or most vagrant commands) will alert and terminate execution if you do not have Administrative privileges.

if then
  def running_in_admin_mode?
    (`reg query HKU\\S-1-5-19 2>&1` =~ /ERROR/).nil?

  unless running_in_admin_mode?
    puts "This vagrant makes use of SymLinks to the host. On Windows, Administrative privileges are required to create symlinks (mklink.exe). Try again from an Administrative command prompt."
    exit 1

Note: This code should be safe to use on any host OS (i.e. a cross-platform Vagrantfile), as it checks and executes only if running on Windows.

Be Sociable, Share!
You can leave a response, or trackback from your own site.

Leave a Reply