How to use vagrant in a proxy environment?

前端 未结 12 905
太阳男子
太阳男子 2020-12-07 08:10

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?

相关标签:
12条回答
  • 2020-12-07 08:38

    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"
    
    0 讨论(0)
  • 2020-12-07 08:39

    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.

    0 讨论(0)
  • 2020-12-07 08:40

    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"
    
    0 讨论(0)
  • 2020-12-07 08:40

    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 >
    
    0 讨论(0)
  • 2020-12-07 08:49

    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.)

    0 讨论(0)
  • 2020-12-07 08:51

    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 !

    0 讨论(0)
提交回复
热议问题