Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Vagrant SSH Agent Forwarding Working 1.4.3

This was working on Vagrant 1.4.3 (Mac).


File: ~/.ssh/config

Host vagrant.*
ForwardAgent yes

File: Vagrantfile

# -*- mode: ruby -*-
# vi: set ft=ruby :


Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|

  config.vm.hostname = "example" = "precise64"
  config.vm.box_url = ""

  # If nfs isn't working:
  config.vm.synced_folder ".", "/home/vagrant/example", type: "nfs" :private_network, ip: ""

  config.vm.provision :shell, :path => "deployment/"
  config.ssh.forward_agent = true

  config.vm.provider "virtualbox" do |vb| = "example"
    vb.memory = 1024
    vb.customize ["modifyvm", :id, "--cpus", "2"]
    vb.customize ["modifyvm", :id, "--ioapic", "on"]

  config.vm.provision :ansible do |ansible|
    ansible.playbook = "deployment/provisioning/playbook.yml"
    ansible.inventory_path = "deployment/provisioning/hosts-vagrant"
    ansible.verbose = false

File: ansible.cfg

ssh_args = -o ControlMaster=auto -o ControlPersist=60s -o ForwardAgent=yes

File: deployment/

echo "Setting up agent forwarding..."
mkdir -p /root/.ssh
chmod 0700 /root/.ssh
printf "Host \n    StrictHostKeyChecking no\n" > /root/.ssh/config
chmod 0600 /root/.ssh/config
while [[ $SSH_AUTH_SOCK == "" && $ppid != "1" ]]; do
    f=`ls /tmp/ssh*/agent.$ppid 2>/dev/null`
    if [[ -z "$f" ]]; then
        ppid=`cat /proc/$ppid/status | grep PPid | awk '{print $2}'`
        export SSH_AUTH_SOCK="$f"
if [[ -z "$SSH_AUTH_SOCK" ]]; then
    echo "Could not find running ssh agent.\n" 1>&2
    exit 1
echo "Updating root_ssh_agents"
agent_contents="Defaults    env_keep += \"SSH_AUTH_SOCK\""
touch $agent_file && chmod 0440 $agent_file && echo $agent_contents > $agent_file
echo "Testing git connection"
ssh -T

echo "Updating ssh config for vagrant user"
sudo su vagrant -c "mkdir -p /home/vagrant/.ssh"
sudo su vagrant -c "printf \"Host\n    StrictHostKeyChecking no\n\" > /home/vagrant/.ssh/config"
sudo su vagrant -c "sudo chmod 0700 /home/vagrant/.ssh"
sudo su vagrant -c "sudo chmod 0600 /home/vagrant/.ssh/config"

exit 0


ssh -T

Hi KyleJamesWalker! You've successfully authenticated, but GitHub does not provide shell access.

This no longer seems to be working on 1.5.


This comment has been minimized.

Copy link
Owner Author

KyleJamesWalker commented Mar 14, 2014

With 1.4.3 I did not have to run ssh-add but not on 1.5 I do so I added the following line to my provisioning script to make sure the keys are passed in.

    - name: Make sure ssk keys are passed to guest.
      local_action: command ssh-add

This comment has been minimized.

Copy link
Owner Author

KyleJamesWalker commented Mar 14, 2014

I also had to change my hosts-vagrant file from:




default         ansible_ssh_host=

[local]       ansible_connection=local

but my local ansible section is still being passed over.

local_action works though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.