Last Updated: February 07, 2017
· jabclab

Cloning from GitHub in Vagrant using SSH agent forwarding

For our development environments we are working predominantly in Vagrant so that we all have a consistent dev env.

One challenge which arose was how to clone from GitHub within the Vagrant-created VM. We achieved this using ssh agent forwarding:

1. Add your SSH key which is tied to GitHub account to host ssh-agent


# Add if not already added
[[ -z $(ssh-add -L | grep $key_file) ]] && ssh-add $key_file

2. Enable SSH agent forwarding in your Vagrantfile

config.ssh.forward_agent = true

You will then be able to do the following:

$ vagrant ssh
[vagrant@localhost ~]$ ssh -T git@github.com
Hi xxxx! You've successfully authenticated, but GitHub does not provide shell access.

(as usual you'll have to authorise GitHub's public RSA key so it can be added to the ~/.ssh/known_hosts).

5 Responses
Add your response


Awesome! Works flawlessly.

over 1 year ago ·

Within our Vagrant environment, I run the following bash script to prevent the need to authorize any SSH keys during the provisioning process:


touch /home/vagrant/.ssh/config
chown vagrant:vagrant /home/vagrant/.ssh/config
chmod 600 /home/vagrant/.ssh/config

cat << 'EOF' >> /home/vagrant/.ssh/config

StrictHostKeyChecking no


exit 0

over 1 year ago ·


over 1 year ago ·

how can i execute git clone command during vagrant up without specifying username and password in the vagrantfile itself.
git clone -b develop https://username:password@github.com/repo.git. i added this command in vagrant file and when i do vagrant up, able to clone the repository. But I would like to change this command such that it works for every user who has vagrant file.

over 1 year ago ·

Where should you add this? Host-machine, file etc?


# Add if not already added
[[ -z $(ssh-add -L | grep $key_file) ]] && ssh-add $key_file


over 1 year ago ·
Filed Under