Search code examples
sshvagrantssh-agent

SSH forwarding does not work for vagrant


I set up ssh params of Vagrant 1.8.1 as described here

Shortly, I got on host ssh config file:

Host bitbucket.org
  Hostname bitbucket.org
  IdentityFile ~/.ssh/id_bitbucket
  User zuba
  ForwardAgent yes

in Vagrantfile:

config.ssh.forward_agent = true

On host machine ssh-add -L shows the key, while on vagrant box it reports that the agent has no identities and git clone fails due to authentication failure

How to solve this issue?

UPDATE 1:

vagrant ssh -c 'ssh-add -l' shows the key

> vagrant ssh-config
Host p4
  HostName 127.0.0.1
  User vagrant
  Port 2222
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile /home/zuba/.vagrant.d/insecure_private_key
  IdentitiesOnly yes
  LogLevel FATAL
  ForwardAgent yes

UPDATE 2:

found the duplicate post with no answers vagrant ssh agent forwarding only works for inline commands?

UPDATE 3:

Here it is my Vagrantfile:

Vagrant.configure("2") do |config|

  boxes = {
      "p4" => "10.2.2.15",
  }

  boxes.each do |box_name, box_ip|
    config.vm.define box_name do |config|
      config.vm.box = "trusty-64"
      config.vm.box_url = "https://cloud-images.ubuntu.com/vagrant/trusty/current/trusty-server-cloudimg-amd64-vagrant-disk1.box"
      config.vm.hostname = "p4"
      config.vm.network :private_network, ip: box_ip
      config.vm.network "forwarded_port", guest: 3000, host: 3000
      config.vm.network "forwarded_port", guest: 3001, host: 3001
      config.vm.network "forwarded_port", guest: 3002, host: 3002
      config.vm.network "forwarded_port", guest: 3003, host: 3003
      config.vm.network "forwarded_port", guest: 6379, host: 6379 # Redis

      config.vm.provider "virtualbox" do |vb|
        vb.gui = false
        vb.name = "p4"

        # Use VBoxManage to customize the VM. For example to change memory:
        vb.customize ["modifyvm", :id, "--memory", "1024"]
      end

      config.vm.synced_folder "../..", "/home/vagrant/my_src"
      config.ssh.forward_agent = true # to use host keys added to agent

      # provisioning
      config.vm.provision :shell, :inline => "sudo apt-get update"

      config.vm.provision "chef_solo" do |chef|
        chef.log_level = "info"
        chef.environment = "development"
        chef.environments_path = "environments"
        chef.cookbooks_path = ["cookbooks", "site-cookbooks"]
        chef.roles_path = "roles"
        chef.data_bags_path = "data_bags"
        chef.json.merge!(JSON.parse(IO.read("nodes/#{box_ip}.json")))
      end

      config.exec.commands '*', directory: '/home/vagrant'
      config.exec.commands 'apt-get', prepend: 'sudo'
      config.exec.commands %w[rails rspec rake], prepend: 'bundle exec'
    end
  end
end

Solution

  • Finally I found that post which helped me to figure out what prevented vagrant from using agents key.

    I ssh-add the key in one GNU screen session, while doing vagrant ssh in another screen session. That is why ssh-agent was kinda 'inaccessible' to the vagrant.

    When I added the key and ssh-ed vagrat in the same screen session, everything started working