99

My company's network is using proxy. So when I use vagrant up, it showed me a 401 permission error.

How can I do some setting to use vagrant?

Cœur
  • 37,241
  • 25
  • 195
  • 267
ithelloworld
  • 2,207
  • 4
  • 16
  • 16

12 Answers12

106

Install proxyconf:

vagrant plugin install vagrant-proxyconf

Configure your Vagrantfile:

config.proxy.http     = "http://yourproxy:8080"
config.proxy.https    = "http://yourproxy:8080"
config.proxy.no_proxy = "localhost,127.0.0.1"
tmatilai
  • 4,071
  • 20
  • 24
Alejandro Moreno
  • 5,578
  • 2
  • 31
  • 29
  • 4
    `config.env_proxy.*` is deprecated as of version 2.0 and has been replaced by `config.proxy.*`. – Tomalak May 26 '14 at 08:38
  • `config.proxy.https = "https://yourproxy:8080"` is that `https` or `http` in the second line – eldos Jul 19 '14 at 03:45
  • 2
    it could be both. In my company http and https goes through the same proxy which is in http – Alejandro Moreno Jul 31 '14 at 14:07
  • 1
    This doesn't seem to work if the proxy is cntlm running on that machine. There could be a work around, but I just pointed to another machine and it worked. – Seth Feb 25 '15 at 19:19
  • re: eldos, the url for https_proxy is http://server:port/ (https proxy requests are really a http CONNECT request that opens the tunnel, so the proxy server is accessed by http) – Alex Lehmann Mar 05 '15 at 15:42
  • 33
    Ok when I run 'vagrant plugin install vagrant-proxyconf' it hits my proxy ? – Mark Broadhurst Jul 03 '15 at 08:30
  • Answer by l.cotonea is wholistic. Set the proxy first and then install plugin. – Keerthi Ramalingam Jun 08 '16 at 07:01
  • Tried this, but got `NameError: undefined local variable or method 'config' for main:Object` – Adam Burley Feb 23 '18 at 15:36
  • Seems you need to put `Vagrant.configure("2") do |config|` before these three lines and `end` after them, to get it to work – Adam Burley Feb 23 '18 at 17:31
  • 12
    Just one thing to mention, if you are behind a proxy, you can't install plugins. – user3426711 Mar 27 '18 at 12:11
  • @user3426711 you'll have to play around that. In my case I had two networks, one I needed to use for work with the proxy in there, and another one with no proxy which I could use to download whatever I needed – Alejandro Moreno Apr 09 '18 at 12:28
97

If your proxy requires authentication it is better to set the environment variable rather than storing your password in the Vagrantfile. Also your Vagrantfile can be used by others easily who are not behind a proxy.

For Mac/Linux (in Bash)

export http_proxy="http://user:password@host:port"
export https_proxy="http://user:password@host:port"
vagrant plugin install vagrant-proxyconf

then

export VAGRANT_HTTP_PROXY=${http_proxy}
export VAGRANT_HTTPS_PROXY=${https_proxy}
export VAGRANT_NO_PROXY="127.0.0.1"
vagrant up

For Windows use set instead of export.

set http_proxy=http://user:password@host:port
set https_proxy=https://user:password@host:port
vagrant plugin install vagrant-proxyconf

then

set VAGRANT_HTTP_PROXY=%http_proxy%
set VAGRANT_HTTPS_PROXY=%https_proxy%
set VAGRANT_NO_PROXY="127.0.0.1"
vagrant up
rjdkolb
  • 10,377
  • 11
  • 69
  • 89
  • 1
    Also, if you don't want it staying in your environment, you can do VAGRANT_HTTP_PROXY="http://user:password@host:port" vagrant up (without the export or set) for that environment variable on only one command. – maccam912 Jul 07 '15 at 17:33
  • 4
    Great solution as it does not require putting the proxy settings in the Vagrantfile where they clearly do not belong – emrass Oct 27 '15 at 14:07
  • 8
    Windows Powersehell v6.0: $env:http_proxy="http://user:password@host:port" $env:https_proxy="http://user:password@host:port" vagrant plugin install vagrant-proxyconf – Xolani Feb 15 '18 at 07:28
  • 1
    For those using Windows Git Bash, use the Mac/Linux (in Bash) instructions .e.g: export http_proxy="http://user:password@host:port" – Xolani Feb 15 '18 at 07:30
  • For what it's worth, just to note that as of this writing (02/11/2018), and context being running on Windows 10 and plain CMD prompt, the "vagrant box add" command for me required the HTTP_PROXY and HTTPS_PROXY variables to also be set to work correctly. This despite some other commands (vagrant plugin install, vagrant login) working fine with just the lower case variables being set. – W.Prins Nov 02 '18 at 11:57
  • 1
    You forgot (?) to `export VAGRANT_HTTPS_PROXY=${https_proxy}` in the Mac/Linux instructions. – Tom Hundt Jun 03 '20 at 21:46
53

Installing proxyconf will solve this, but behind a proxy you can't install a plugin simply using the command vagrant plugin install, Bundler will raise an error.

set your proxy in your environment if you're using a unix like system

export http_proxy=http://user:password@host:port

or get a more detailed answer here: How to use bundler behind a proxy?

after this set up proxyconf

Community
  • 1
  • 1
pingween
  • 631
  • 5
  • 3
  • 10
    On windows I did what you suggest but used "SET" instead of "export". Worked as expected without making any changes to the Vagrantfile. – Daniel Watrous Jul 01 '14 at 19:15
29

Auto detect your proxy settings and inject them in all your vagrant VM

install the proxy plugin

vagrant plugin install vagrant-proxyconf

add this conf to you private/user VagrantFile (it will be executed for all your projects) :

vi $HOME/.vagrant.d/Vagrantfile

Vagrant.configure("2") do |config|
  puts "proxyconf..."
  if Vagrant.has_plugin?("vagrant-proxyconf")
    puts "find proxyconf plugin !"
    if ENV["http_proxy"]
      puts "http_proxy: " + ENV["http_proxy"]
      config.proxy.http     = ENV["http_proxy"]
    end
    if ENV["https_proxy"]
      puts "https_proxy: " + ENV["https_proxy"]
      config.proxy.https    = ENV["https_proxy"]
    end
    if ENV["no_proxy"]
      config.proxy.no_proxy = ENV["no_proxy"]
    end
  end
end

now up your VM !

quazardous
  • 846
  • 10
  • 15
  • 1
    I like this answer because it reuses the existing HTTP_PROXY env variables that I've already declared on my host OS. Death to cut-paste !! – ripvlan Jul 30 '15 at 15:55
  • Looks like a good way forward. What about disabling it when going outside the proxy? Found that the vagrant plugin punched the guest linux in multiple places to make various different tools/apps work. Is there a simple way to disable? Such that will also be at least flipped from command line flag and a single `vagrant reload` will fix, and could be best to detect proxy settings in/out automatically on connecting to new networks, and either alert the user, or make it seamlessly work. http://tmatilai.github.io/vagrant-proxyconf/ mentioned disabling, but not sure it addresses these points. – arntg Jun 22 '16 at 18:33
  • Not sure to understand but this snippet test the existence of the http_proxy env var to setup the proxy plugin. If you do not use *_proxy env it should be 'not activated' (?) – quazardous Aug 07 '16 at 06:26
11

On a Windows host

open a CMD prompt;

set HTTP_PROXY=http://proxy.yourcorp.com:80
set HTTPS_PROXY=https://proxy.yourcorp.com:443

Substitute the address and port in the above snippets to whatever is appropriate for your situation. The above will remain set until you close the CMD prompt. If it works for you, consider adding them permanently to your environment variables so that you won't have to set them every time you open a new CMD prompt.

stratagem
  • 525
  • 6
  • 7
9

On windows, you must set a variable to specify proxy settings, download the vagrant-proxyconf plugin: (replace {PROXY_SCHEME}(http:// or https://), {PROXY_IP} and {PROXY_PORT} by the right values)

set http_proxy={PROXY_SCHEME}{PROXY_IP}:{PROXY_PORT}
set https_proxy={PROXY_SCHEME}{PROXY_IP}:{PROXY_PORT}

After that, you can add the plugin to hardcode your proxy settings in the vagrant file

vagrant plugin install vagrant-proxyconf --plugin-source http://rubygems.org

and then you can provide config.proxy.xxx settings in your Vagrantfile to be independent against environment settings variables

l.cotonea
  • 2,037
  • 1
  • 15
  • 17
  • Just an additionnal parameter (rubygem from https to http) to avoid error while verifying SSL certificates: `vagrant plugin install vagrant-proxyconf --plugin-source http://rubygems.org` [src](https://github.com/mitchellh/vagrant/issues/2671) – boly38 Feb 24 '15 at 13:53
  • this is better solution than the previous ones, because this one doesn't need anything else. Other solutions (install a plugin) need to set free internet access first in order to do so. – Raul Luna Feb 03 '16 at 19:31
6

You will want to install the plugin proxyconf since this makes configuring the proxy for the guest machines pretty straight forward in the VagrantFile

config.proxy.http     = "http://proxy:8888"
config.proxy.https    = "http://proxy:8883"
config.proxy.no_proxy = "localhost,127.0.0.1"

However, there's quite a few things that could still go wrong. Firstly, you probably can't install vagrant plugins when behind the proxy. If that's the case you should download the source e.g. from rubygems.org and install from source

$ vagrant plugin install vagrant-proxyconf --plugin-source file://fully/qualified/path/vagrant-proxyconf-1.x.0.gem

If you solve that problem you might have the fortune of being behind an NTLM proxy, which means that if you are using *nix on your guest machines then you still have some way to go, because NTLM authentication is not supported natively There are many ways of solving that. I've used CNTLM to solve tht part of the puzzle. It acts as glue between standard authorization protocols and NTLM

For a complete walk through, have a look at this blog entry about setting vagrant up behind a corporate proxy

Rune FS
  • 21,497
  • 7
  • 62
  • 96
  • I would be interested in knowing why this was down voted. It explains how to solve the issue and also how to solve potential other issues you might encounter – Rune FS Oct 24 '15 at 12:43
  • isn't this pretty much the same answer already provided multiple times? in addition, it doesn't say what to do to get vagrant plugins if you're behing corporate proxy, it just states that it would be a problem and then linking elsewhere. – eis Dec 02 '15 at 07:47
  • Looks like now it should be `vagrant plugin install file://fully/qualified/path/vagrant-proxyconf-1.x.0.gem`. [Source](https://www.vagrantup.com/docs/cli/plugin.html#plugin-install) – Martin Apr 18 '17 at 13:59
  • Great idea to use `plugin-source` to install from a local GEM, but I haven't managed to get this working yet on Windows. I am not sure if my syntax is wrong, like `file://C:/path1/path2/vagrant-proxyconf-1.5.2.gem`? I have also tried the approach which @Martin mentioned above, this also didn't work as it's still trying to contact rubygems in that case – Adam Burley Feb 23 '18 at 15:13
  • 2
    This worked for me: `vagrant plugin install C:/folder1/folder2/vagrant-proxyconf-1.5.2.gem --plugin-clean-sources` The key is `--plugin-clean-sources` which causes it to not try to access rubygems – Adam Burley Feb 23 '18 at 15:25
2

The question does not mention the VM Provider but in my case, I use Virtual Box under the same environment. There is an option in the Virtual Box GUI that I needed to enable in order to make it work. Is located in the Virtual Box app preferences: File >> Preferences... >> Proxy. Once I configured this, I was able to work without problems. Hope this tip can also help you guys.

James
  • 587
  • 1
  • 7
  • 21
1

If you actually do want your proxy configurations and plugin installations to be in your Vagrantfile, for example if you're making a Vagrantfile just for your corporate environment and can't have users editing environment variables, this was the answer for me:

ENV['http_proxy']  = 'http://proxyhost:proxyport'
ENV['https_proxy'] = 'http://proxyhost:proxyport'

# Plugin installation procedure from http://stackoverflow.com/a/28801317
required_plugins = %w(vagrant-proxyconf)

plugins_to_install = required_plugins.select { |plugin| not Vagrant.has_plugin? plugin }
if not plugins_to_install.empty?
  puts "Installing plugins: #{plugins_to_install.join(' ')}"
  if system "vagrant plugin install #{plugins_to_install.join(' ')}"
    exec "vagrant #{ARGV.join(' ')}"
  else
    abort "Installation of one or more plugins has failed. Aborting."
  end
end

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.proxy.http     = "#{ENV['http_proxy']}"
  config.proxy.https    = "#{ENV['https_proxy']}"
  config.proxy.no_proxy = "localhost,127.0.0.1"
  # and so on

(If you don't, just set them as environment variables like the other answers say and refer to them from env in config.proxy.http(s) directives.)

eis
  • 51,991
  • 13
  • 150
  • 199
1

Some Special characters in the password create problem in proxy. Either escape them or avoid having special characters in password.

ernitingoel
  • 621
  • 2
  • 9
  • 24
1

In PowerShell, you could set the http_proxy and https_proxy environment variables like so:

$env:http_proxy="http://proxy:3128"
$env:https_proxy="http://proxy:3128"
pour toi
  • 1,026
  • 2
  • 13
  • 25
0

In MS Windows this works for us:

set http_proxy=< proxy_url >
set https_proxy=< proxy_url >

And the equivalent for *nix:

export http_proxy=< proxy_url >
export https_proxy=< proxy_url >
Udo Klein
  • 6,784
  • 1
  • 36
  • 61
csoria
  • 643
  • 5
  • 9