How to use vagrant in a proxy environment?
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?
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"
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
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
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 !
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.