vagrant ssh fail when using synced_folder - vagrant

I want to mount a local directory relative to my vagrant file inside the vm. However, when I do that,
vagrant ssh
No longer works -- private key fails. I am not sure why ssh suddenly fails.
help? (if it matters: I am trying to mount the directory my Java artifacts compile to).
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure("2") do |config|
# Use Ubuntu 14.04 Trusty Tahr 64-bit as our operating system
config.vm.box = "ubuntu/trusty64"
# Configurate the virtual machine to use 2GB of RAM
config.vm.provider :virtualbox do |vb|
vb.customize ["modifyvm", :id, "--memory", "1024"]
vb.cpus = 1
end
config.vm.network :forwarded_port, guest: 8080, host: 8080
config.vm.synced_folder "target/", "/home/vagrant"
end

As you try to sync directly on /home/vagrant in the VM, you need to make sure your target directory contains a .ssh folder with the authorized_keys
When you vagrant up the VM, vagrant will ssh and create this directory for you but when going through the sync_folder part, it will replace all the content from /home/vagrant with your host target/ so loosing what it did create before.
If you really really want to sync on /home/vagrant what you could do is run first without the sync, copy all files that have been created (.ssh/, .bash ...) into your target directory and then you should be able to rerun with the sync on /home/vagrant. (Note: I did not try that and honestly would not recommend to sync on /home/vagrant directly as if you install other soft from provisioning or other, you might run into issues later)

Related

Vagrant Vagrantfile config

I have this vagranfile for box "centos/7"
Vagrant.configure("2") do |config|
config.vm.box = "centos/7"
config.vm.network :private_network, ip: "192.168.33.10"
config.vm.synced_folder ".", "/home/vagrant"
config.vm.provision "shell", path: "./conf/bitrix-env.sh"
config.vm.boot_timeout = 100
config.vm.provider :virtualbox do |vb|
vb.gui = true
vb.memory = "1024"
end
end
vagrant up - command works clear, but scripts in provision don't install
after i tried to start vagrant provision, but had an error:
SSH is getting permission denied errors when attempting to connect
to the IP for SSH. This is usually caused by network rules and not being
able to connect to the specified IP. Please try changing the IP on
which the guest machine binds to for SSH.
How can i fix this and install all provisions?
The problem is due to the below line in your vagrantfile
config.vm.synced_folder '.', '/home/vagrant'
The authorized_keys file for the vagrant user is located in /home/vagrant/.ssh inside the vagrant machine, which enables to ssh into the vagrant box.
As you are mounting your current directory to /home/vagrant, all the contents of /home/vagrant are over-written and there is no authorized key file.
Change the mount path to anything except /home/vagrant, and you will able to ssh into the machine. As example
config.vm.synced_folder '.', '/home/vagrant/somepath'

How to use aliases in Vagrant

I'm trying to create aliases that I can use in Vagrant any time I run the VM. I've found several sources on the web about it, but can't get it working. I tried making a .bash_profile in my synced folder, but that didn't work. I noticed if I run the command alias name="command" this will work, but only for the current session. Anyone know how to do this? I'm using macOS. Thanks for your help!
Here is my Vagrantfile:
# -*- mode: ruby -*-
# vi: set ft=ruby :
unless Vagrant.has_plugin?("vagrant-vbguest")
warn "\nWARNING: The vagrant-vbguest plugin should be installed or your shared folders might not mount properly!"
warn "You can do this by running the command 'vagrant plugin install vagrant-vbguest'.\n\n"
end
VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.box = "pype_vm"
config.vm.box_url = "https://.../pype_vm.json"
config.vm.network "private_network", ip: ""
config.vm.boot_timeout = 600
config.vm.provider "virtualbox" do |v|
# This forces VirtualBox to use the host's DNS resolver instead of
# VirtualBox's
v.customize ["modifyvm", :id, "", "on"]
# This enables the PAE/NX option, which resolved at least one user's
# issues with the VM hanging on boot
v.customize ["modifyvm", :id, "--pae", "on"]
# The RHEL VM was created with 2GB of memory to facilitate provisioning,
# but this is causing issues with certain workstations. This reduces
# the amount of memory allocated to the VM but should not impact development
# performance. The number is in MB and can be increased if desired.
v.memory = 1024
end
# Share an additional folder to the guest VM.
config.vm.synced_folder File.dirname(__FILE__), "/pype"
end
The details depend on the specific of the guest being run, but some notes:
Assuming the default user account is active for vagrant ssh, ensure that any dotfiles you wish to override are copied to /home/vagrant.
If overriding .bashrc, ensure that the remote shell is started with the interactive flag (if this is true, echo $- will include i).
If overriding .bash_profile, ensure that the remote shell is started as a login shell (if this is true, echo $- will include l).

How to exclude (ignore) certain folders in vagrant rsync?

I wish to run npm install in my vagrant virtual box.
But whenever I ran the npm install command, my rsync will execute. Since my host computer does not have node_modules installed, it simply remove the folder completely for me.
What do I need to do so that my vagrant rsync will ignore the node_modules folder?
I cannot have node_modules being rsynced into the guest machine because my host and guest are two different systems. So far my vagrantfile looks like this.
Vagrant.configure("2") do |config|
config.vm.box = "laravel/homestead"
config.vm.hostname = "localhost"
config.vm.network "forwarded_port", guest: 8000, host: 8000
config.vm.synced_folder "./", "/home/vagrant/app"
config.vm.provider "virtualbox" do |v|
v.name = "web2"
v.memory = "4096"
v.cpus = 2
v.customize ["setextradata", :id, "VBoxInternal2/SharedFoldersEnableSymlinksCreate/SHARE_NAME", "1"]
end
config.vm.synced_folder "./", "/home/vagrant/app", type: "rsync", rsync_auto: true, rsync_exclude: ['node_modules*']
config.vm.provision :shell, path: "provision/setup.sh"
end
And I execute vagrant rsync via
vagrant rsync-auto
rsync__exclude as well as rsync__auto takes 2 _
You need to rewrite your line as
config.vm.synced_folder "./", "/home/vagrant/app", type: "rsync", rsync__auto: true, rsync__exclude: ['./node_modules*']
Step 3 on http://perrymitchell.net/article/npm-symlinks-through-vagrant-windows/ worked better for me in the end...
From the page:
The steps are simple:
Delete the node_modules directory if it exists.
Create a directory called, say "node_modules_projectname" in the VM's home directory (~) (Some articles and posts recommend making the
directory in /tmp, but obviously this is cleared on reboot, so it may
not be an optimal experience for this type of thing).
Link a local node_modules dir from within the project's directory
ln -s ~/node_modules_projectname /path/to/your-project/node_modules
Install the packages in the project directory:
npm install

Vagrant 'permission denied' on Windows

I am having trouble accessing files through Vagrant on Windows. I have been using it on OS X for quite some time and have my Vagrantfile setup correctly which works every time.
I have sent my colleague the same Vagrant file, he is on Windows and receives 'Permission Denied' when trying to access files through the browser.
Just to be clear, the permission errors are returned by the server when accessing 'dev.local' in the browser and not from Vagrant itself... it will be a configuration error on Windows or within the VM.
The VM is CentOS 6.5
Vagrantfile:
# -*- mode: ruby -*-
# vi: set ft=ruby :
# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.box = "chef/centos-6.5"
config.vm.network "private_network", ip: "192.168.33.21"
config.vm.network :forwarded_port, guest: 80, host: 8080
config.vm.provision :shell, :path => "install.sh"
config.vm.hostname = "dev.local"
config.vm.synced_folder ".", "/home", id: "vagrant", :nfs => false, :mount_options => ["dmode=777","fmode=777"]
config.ssh.insert_key = false
config.ssh.username = "vagrant"
config.ssh.password = "vagrant"
end
Can any Windows Vagrant users shed any light on this?
It was VBGuestAdditions out of date. The permission error was caused by not being able to sync to my local folder (which contained an index.php) so it was using the servers /home folder which didn't contain anything and since viewing directory structure is disabled it returned permission errors.
I had 4.X.X installed, and VirtualBox is on 5.X.X Here is the fix:
run command: vagrant plugin install vagrant-vbguest
the run vagrant up which may still throw an error as the plugin fails to copy a file.
vagrant ssh to get into the box and run the following command:
sudo ln -s /opt/VBoxGuestAdditions-5.X.X/lib/VBoxGuestAdditions /usr/lib/VBoxGuestAdditions
Replace 5.X.X with the version of your VirtualBox.
logout and run vagrant reload
do a happy dance

vagrant Vagrantfile mount point ignored

I have a Vagrantfile as below running under libvirt. When the box boots, the project directory is mounted under "/vagrant" not "/path/to/source". It works fine on another machine under VirtualBox.
Any ideas? Symlinking /vagrant to the actual place I want the mount is hacky.
vagrant up --debug shows that it isn't even attempting it. The port forwarding works OK though.
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure("2") do |config|
config.vm.box = "mybox"
file = File.open("#{Dir.home}/.mybox_key", "rb")
key = file.read
config.vm.network :forwarded_port, guest: 80, host: 8080
config.vm.synced_folder "./", "/path/to/source/", type: "nfs"
end
If you're running an older version of Vagrant that doesn't support this option (e.g. 1.3.5), it may fail silently instead of trying to setup the NFS share. Ensure you're running at least Vagrant 1.5.1.

Resources