Vagrant - Can't SSH into specific machine from command line - ssh

I am trying to SSH into a specific vagrant box from the command line:
vagrant ssh winbox password
'winbox' is what the box appears to be called in 'vagrant box list'. However, upon the entry of that command I get:
The machine with the name 'winbox' was not found configured for
this Vagrant environment.
If I just do 'vagrant ssh' It all works fine so I'm pretty sure I'm getting the box name wrong. The same box name appears in the vagrant file for the box:
Vagrant.configure("2") do |config|
config.vm.box = "winbox"
config.ssh.username = "vagrant"
config.ssh.password = ******
config.vm.network :forwarded_port, guest: 4444, host: 4440
config.vm.network :forwarded_port, guest: 5555, host: 5555
end
Is there any way to check the name of the currently running vagrant box?
It is important that I log into the box all in one line because I am basically using Java to execute command line code from within a JUNIT test and it's not possible to first go vagrant ssh and then enter a password with it.

you only need the box name information when you initiate the new VM.
so either when you run vagrant init winbox or when you change the config.vm.box parameter in your Vagrantfile.
After you have initialized the VM (vagrant up) you can forget about the box, you don't need it anymore (it still needs to be present in your Vagrantfile but you don't reference it anymore)
If you run a single VM in your Vagrantfile, you can just run vagrant ssh to get into the VM.
what you may be confused about the vagrant ssh <VMName> is when you run vagrant multi-machine so in this case a single Vagrantfile will spin multiple VMs and you will need to indicate the name of the VM (not the box) to ssh into the correct VM

Related

"ssh_exchange_identification: read: Connection reset by peer" happens while tryng to log in vagrant box over ssh

I was trying to deploy 4 vagrant box of two different OS. two of them are Ubuntu and other two are Centos. My vagrant file configuration is below:-
config.vm.define "ubuntu" do |ubuntu|
ubuntu.vm.hostname="ubuntu"
ubuntu.vm.box="bento/ubuntu-17.10"
ubuntu.vm.network "private_network", ip:"192.168.33.10"
end
config.vm.define "centos" do |centos|
centos.vm.hostname="centos"
centos.vm.box="bento/centos-7.4"
centos.vm.network "private_network", ip:"192.168.33.20"
end
config.vm.define "server1" do |server1|
server1.vm.hostname="server1"
server1.vm.box="bento/ubuntu-17.10"
server1.vm.network "private_network", ip:"192.168.33.30"
end
config.vm.define "server2" do |server2|
server2.vm.hostname="server2"
server2.vm.box="bento/centos-7.4"
server2.vm.network "private_network", ip:"192.168.33.40"
end
After successfully executing executing vagrant up I checked the vagrant status and found all box were running ok.
ubuntu running (virtualbox)
centos running (virtualbox)
server1 running (virtualbox)
server2 running (virtualbox)
however when I tried to login to each machine using vagrant ssh ubuntu , vagrant ssh centos , vagrant ssh server1 ,vagrant ssh server2 commands , every machine could be logged in except server1. While I tried to access server2 using vagrant ssh server1 that error showed:-
"ssh_exchange_identification: read: Connection reset by peer"
I using vagrant 2.2.4 on my elementary OS Loki system. Seeking help from experienced.TIA
The issue has solved. I dont no what was wrong actually! However, I tried following simple steps and fortunately it worked: 1. vagrant destroy 2. vagrant up

SSH into Vagrant box as another user using key?

I like using the "vagrant" user to provision my Vagrant VM using Ansible. But once I get the box set up, I'd like to be able to log in to it using either the vagrant account or my personal account "smith." However, when I log in as smith, I'd like to not be prompted for my password. Is there a way to set this up? I've created the smith account on the Vagrant box and I've copied my public and private SSH keys from my .ssh directory on the OS X management host to user smith's .ssh directory on the VM. Right now I can log in by doing "ssh smith#192.168.2.100" but I get prompted for my password. Is there any way to configure both machines so that I can log in using my SSH keys?
Thanks.
# Vagrantfile
DEV_GID = 1001
Vagrant.configure(2) do |config|
config.vm.box = "debian/jessie64"
config.vm.network "forwarded_port", guest: 80, host: 8080
config.vm.network "forwarded_port", guest: 443, host: 8443
config.vm.network "private_network", ip: "192.168.2.100"
config.vm.synced_folder "website/", "/srv/http/example.com",
owner: "root",
group: DEV_GID,
mount_options: ["dmode=775"]
config.vm.provision "shell", path: "provision.sh"
end
Instead of copying all the files in the .ssh directory, you should create the file
/home/smith/.ssh/authorized_keys
(or wherever your home folder in the vagrant box is) in your vagrand box and copy-paste your id_rsa.pub inside this file. By copying-paste I mean open id_rsa.pub with an editor, copy all of it, and paste it in .ssh/authorized_keys file. You should also change the directory permissions to 0600.
Your private id_rsa file is not needed in the vagrant box, it is only needed in the host you use to ssh into that vagrant box.

Vagrant - cannot log into my Box

I created a Vagrant Box, played with it, made some custom dev.
But then I wanted to login without playing "vagrant ssh myvagrantbox".
For that I appended the "~/.ssh/id_rsa.pub" from my host to the autorised keys of myvagrantbox.
Then I made a "Vagrant halt" / "vagrant up".
But now Vagrant says it cannot connect because of a timeout.
Vagrant says "myvagrantbox" is running but I can't connect with "vagrant ssh myvagrantbox".
Any idea ?
A better solution than destroying, beginning from zero again ?
Enable the GUI mode, as your box might be stuck at the bootloader.
For this just add this to your Vagrantfile :
config.vm.provider :virtualbox do |vb|
vb.gui = true
end
To complete what I wanted to do:
I wanted to connect to my Vagrant box with ssh, without playing "vagrant ssh myvagrantbox".
The solution was quite simple:
ssh vagrant#127.0.0.1 -p 2222 -i ~/.vagrant.d/insecure_private_key
Vagrant uses ssh by default on port 2222 and not port 22. I was forgotting the port and was stuck with a password.
Hopes this will help someday...

Vagrant ssh connect to host 127.0.0.1:2222 port 22: Bad file number

Whenever I try to connect to my local Vagrant, I get this error when I run ssh vagrant#127.0.0.1:2222 from the Windows git bash:
ssh: connect to host 127.0.0.1:2222 port 22: Bad file number
It was working previously, so I'm not sure what could have caused this. When I try to do an SFTP connection in PHPStorm 8, I get this error:
Connection to '127.0.0.1' failed.
SSH_MSG_DISCONNECT: 2 Too many authentication failures for vagrant
I've tried vagrant destroy with vagrant box remove laravel/homestead and then recreating the box from a backup I had that previously worked using vagrant box add laravel/homestead homestead.box but I still get the same errors.
I'm on Windows 7.
What can I do to get access to my vagrant box commandline again?
Try command:
ssh -p 2222 vagrant#127.0.0.1
The answer by outboundexplorer above is the correct one I believe.Here is my step-by-step approach on how I did this:
Step 1: Find out exactly what SSH settings to use
Ensure the vagrant box is running (you've done vagrant up that is)
From the command line, go to your project directory (the one where the Vagrantfile is located) and run vagrant ssh-config.
You'll get an output like this:
Host default
HostName 127.0.0.1
User ubuntu
Port 2222
UserKnownHostsFile /dev/null
StrictHostKeyChecking no
PasswordAuthentication no
IdentityFile C:/Projects/my-test-project/.vagrant/machines/default/virtualbox/private_key
IdentitiesOnly yes
LogLevel FATAL
Step 2: Setting up PHPStorm to SFTP to the Vagrant box
Based on the config settings shown above, I set up the following SFTP remote deployment server:
SFTP host: 127.0.0.1
Port: 2222
Root path: /home/ubuntu/my-test-project (this is the folder inside the Vagrant box where the files will be uploaded to, change to whatever suits your needs)
User name: ubuntu
Auth type: Select "Key pair (OpenSSH or PuTTY)"
Private key file: Point to the IdentityFile path shown (C:/Projects/....)
... and that was it.
I got this same failure when using PHpStorm to SSH into the VirtualBox guest machine that i had set up with Vagrant. Everything worked fine before I upgraded to Windows 10. After upgrading, first of all i had to upgrade to VirtualBox and Vagrant latest versions to get everything to work on Windows 10.
But then i couldn't ssh into the guest machine using the PhpStorm ssh client. After much reading, everything seemed to suggest that I had too many ssh-keys installed on my Windows machine, but checking regedit just showed that I only had a couple of keys which should be less than the suggested max 5 keys (as default). In the end i did vagrant ssh which didn't allow me to ssh into the guest machine, but it did reconfirm the ssh details for me. I then realized that after all the new installs it didn't want me to use the C:\Users\Andy\.vagrant.d\insecure_private_key key but instead use a key that it had placed within the project itself at C:/Users/Andy/CodeLab5/vagrant/.vagrant/machines/default/virtualbox/private_key.
Everything is working as it should again now :)
Make sure your vagrant is up and running by command : vagrant up
and then do vagrant ssh. It will connect to vagrant localhost

How to use ssh agent forwarding with "vagrant ssh"?

Rather than create a new SSH key pair on a vagrant box, I would like to re-use the key pair I have on my host machine, using agent forwarding. I've tried setting config.ssh.forward_agent to TRUE in the Vagrantfile, then rebooted the VM, and tried using:
vagrant ssh -- -A
...but I'm still getting prompted for a password when I try to do a git checkout. Any idea what I'm missing?
I'm using vagrant 2 on OS X Mountain Lion.
Vagrant.configure("2") do |config|
config.ssh.private_key_path = "~/.ssh/id_rsa"
config.ssh.forward_agent = true
end
config.ssh.private_key_path is your local private key
Your private key must be available to the local ssh-agent. You can check with ssh-add -L, if it's not listed add it with ssh-add ~/.ssh/id_rsa
Don't forget to add you public key to ~/.ssh/authorized_keys on the Vagrant VM. You can do it copy-and-pasting or using a tool like ssh-copy-id
Add it to the Vagrantfile
Vagrant::Config.run do |config|
# stuff
config.ssh.forward_agent = true
end
See the docs
In addition to adding "config.ssh.forward_agent = true" to the vagrant file make sure the host computer is set up for agent forwarding. Github provides a good guide for this. (Check out the troubleshooting section).
I had this working with the above replies on 1.4.3, but stopped working on 1.5. I now have to run ssh-add to work fully with 1.5.
For now I add the following line to my ansible provisioning script.
- name: Make sure ssk keys are passed to guest.
local_action: command ssh-add
I've also created a gist of my setup: https://gist.github.com/KyleJamesWalker/9538912
If you are on Windows, SSH Forwarding in Vagrant does not work properly by default (because of a bug in net-ssh). See this particular Vagrant bug report: https://github.com/mitchellh/vagrant/issues/1735
However, there is a workaround! Simply auto-copy your local SSH key to the Vagrant VM via a simple provisioning script in your VagrantFile. Here's an example:
https://github.com/mitchellh/vagrant/issues/1735#issuecomment-25640783
When we recently tried out the vagrant-aws plugin with Vagrant 1.1.5, we ran into an issue with SSH agent forwarding. It turned out that Vagrant was forcing IdentitiesOnly=yes without an option to change it to no. This forced Vagrant to only look at the private key we listed in the Vagrantfile for the AWS provider.
I wrote up our experiences in a blog post. It may turn into a pull request at some point.
Make sure that the VM does not launch its own SSH agent. I had this line in my ~/.profile
eval `ssh-agent`
After removing it, SSH agent forwarding worked.
The real problem is Vagrant using 127.0.0.1:2222 as default port-forward.
You can add one (not 2222, 2222 is already occupied by default)
config.vm.network "forwarded_port", guest: 22, host:2333, host_ip: "0.0.0.0"
"0.0.0.0" is way take request from external connection.
then
ssh -p 2333 vagrant#192.168.2.101 (change to your own host ip address, dud)
will working just fine.
Do thank me, Just call me Leifeng!
On Windows, the problem is that Vagrant doesn't know how to communicate with git-bash's ssh-agent. It does, however, know how to use PuTTY's Pageant. So, as long as Pageant is running and has loaded your SSH key, and as long as you've set config.ssh.forward_agent, this should work.
See this comment for details.
If you use Pageant, then the workaround of updating the Vagrantfile to copy SSH keys on Windows is no longer necessary.