Vagrant with multiple machines and ports

Posted on Wed 06 January 2016

Vagrant environment:


1. Prepare host system

1.1. Install Unubtu 14.04 x64 on host system.

Download page:

Install it.

1.2. Install VirtualBox 5.0.6.

Download page


sudo dpkg -i virtualbox-5.0_5.0.6-103037~Ubuntu~trusty_amd64.deb

1.3. Generating public/private rsa key pair

mkdir .vagrant.d
cp -a .ssh/id_rsa .vagrant.d/

1.4. Install SSH server

sudo apt-get install openssh-server
sudo /etc/init.d/ssh restart

1.5. Install Git

sudo apt-get install git

2. Install VM guest

2.1. Create file in your home directory:

sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
sed -i 's/ONBOOT=no/ONBOOT=yes/g' /etc/sysconfig/network-scripts/ifcfg-eth0
sed -i 's/^\(Defaults.*requiretty\)/#\1/' /etc/sudoers
chkconfig iptables off
rm -f /etc/udev/rules.d/70-persistent-net.rules
useradd vagrant
mkdir -m 0700 -p /home/vagrant/.ssh
mv /root/ /home/vagrant/.ssh/authorized_keys
chmod 600 /home/vagrant/.ssh/authorized_keys
chown -R vagrant:vagrant /home/vagrant/.ssh
echo "vagrant ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers
# install Guest Additions
mount -r /dev/cdrom /media/
yum -y install kernel-devel-`uname -r` kernel-header-`uname -r` gcc perl

2.2. Download CentoOS 6.7 x64 minimal

Download page:

File: CentOS-6.7-x86_64-minimal.iso

2.3. Create new virtual machine

  • Name: centos6
  • Type: Linux
  • Version: Red Hat (64-bit)
  • Other option by default.

Start machine and use ISO CentOS-6.7-x86_64-minimal.iso.
- Install options by default.
- Set root password: vagrant

2.4. Prepare VM for vagrant

Run virtual box centos6 and login.
Use user: root and password vagrant

IMPORTANT: Insert Guest Additions

Get IP from host system

ip a | grep "inet " | grep -v ''

For example you got

IMPORTANT (reminder): Insert Guest Additions before run

VirtualBox window > Devices > Insert Guest Additions CD image

Run these commands on guest system (replace nidlar with your username):

ifup eth0
scp khmel@ ./
scp khmel@ ./
shutdown -h now

3. Configure Vagrant on host system

3.1. Download and install Vagrand

Download page:


sudo dpkg -i vagrant_1.7.4_x86_64.deb

3.2. Create vagrant box

Create directory for new vagrant environment and copy files:

mkdir vagrant-homework

Create vagrant-homework/Vagrantfile

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

Vagrant.configure(2) do |config|
  config.vm.define "appsrv" do |appsrv_config| = "appsrv"
    appsrv_config.ssh.private_key_path = '~/.vagrant.d/id_rsa' "private_network", :ip => '' "forwarded_port", guest: 22, host: 3333, id: "ssh" "forwarded_port", guest: 80, host: 8080
  config.vm.define "monsrv" do |monsrv_config| = "monsrv"
    monsrv_config.ssh.private_key_path = '~/.vagrant.d/id_rsa' "private_network", :ip => '' "forwarded_port", guest: 22, host: 4444, id: "ssh" "forwarded_port", guest: 80, host: 8181 "forwarded_port", guest: 443, host: 4433

Package VM and add vagrant box and add id_rsa file:

vagrant package --output --base centos6
vagrant box add appsrv
vagrant box add monsrv
cp .ssh/id_rsa .vagrant.d/

Start vagrant environment:

cd vagrant-homework
vagrant up

Test ssh connection:

ssh -i ~/.vagrant.d/id_rsa vagrant@ -p 3333
ssh -i ~/.vagrant.d/id_rsa vagrant@ -p 4444