(: October 18, 2018)
Step by step guide on using existing virtual machines with Vagrant
If you have encountered the same problem, this solution is for you:
Prerequisites:
- Installed vagrant: Install Latest Vagrant on Ubuntu 18.04 / Debian 9 & Kali Linux
- Vagrant Instance already added to VirtualBox, VMware or any other provider you use.
- SSH service on Vagrant instance set to start on boot up
Step 1: Generate SSH Keys
Generate ssh private and public keys using the ssh-keygen
command.
My vagrant appliance working directory is ~/vagrant/centos-asterisk-server
. This is where SSH keys will reside.
mkdir -p ~/vagrant/centos-asterisk-server/.ssh
Generate private/private RSA key pair with ssh-keygen
ssh-keygen
I will use the following settings:
Enter file in which to save the key : /home/$USER/vagrant/centos-asterisk-server/.ssh/id_rsa
Enter passphrase (empty for no passphrase): <Press Enter/Return key>
Enter same passphrase again: <Press Enter/Return key>
This will successfully generate RSA 2048 keys. You can set a passphrase or later Change or update SSH key Passphrase on Linux / Unix
Step 2: Configure Vagrant
Edit Vagrantfile and specify the location of generated private key
vim ~/vagrant/centos-asterisk-server/Vagrantfile
We’ll add it just below config.vm.box = ""
line.We’ll also set “config.ssh.forward_agent” to True.
My username and password that vagrant we’ll login in with are the default:
username: vagrant password: vagrant
If you have different username and password, specify them on Vagrantfile
nano Vagrantfile
Add:
Save the changes and quit.

Copy RSA public key to the vagrant instance. We’ll start the Virtual Machine with VirtualBox directly.
Make sure ssh is running and port forwarding is configured. Follow steps below
- Open Oracle VirtualBox ( GUI)
- Right click on the name of Virtual Machine and go to settings > Network > Adapter 1 ( NAT) > Port Forwarding
Make sure SSH port forwarding is set, if not add it by clicking on the + sign
Name: ssh
Protocol: TCP
Host ip: 127.0.0.1
Host Port: 2200
Guest port: 22
- Copy rsa public key to Virtual Machine
- Stop Virtual Machine ( power off) and start it with vagrant
cd /home/josepy/vagrant/centos-asterisk-server/ vagrant up
You can confirm that key was copied to guest os. Do
Now start Vagrant instance using vagrant up command. It should start without any error,