Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ssh a vagrant box without 'vagrant ssh'

I have created a new vagrant box using

vagrant init ubuntu/trusty64
vagrant up

I want to ssh into this without using "vagrant ssh"

ifconfig of the box machine gave me

eth0      Link encap:Ethernet  HWaddr 08:00:27:ca:3e:f9
          inet addr:10.0.2.15  Bcast:10.0.2.255  Mask:255.255.255.0
          inet6 addr: fe80::a00:27ff:feca:3ef9/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:254 errors:0 dropped:0 overruns:0 frame:0
          TX packets:187 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:26220 (26.2 KB)  TX bytes:22208 (22.2 KB)

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

I tried

ssh [email protected]

"who" on box gave

vagrant  pts/0        Jan  5 14:46 (10.0.2.2)

So I tried

ssh [email protected]

But neither worked!

Neither ping works on this ips

like image 597
user3290349 Avatar asked Jan 05 '16 15:01

user3290349


2 Answers

First, you can't access ip's behind an NAT gateway. You need to access the server using the IP address of the NAT gateway and the forwarded port which is 2222 by default for ssh in vagrant. You also need to tell ssh that it should use vagrant's insecure private key:

ssh -i ~/.vagrant.d/insecure_private_key -p 2222 vagrant@localhost

The vagrant ssh command is meant to hide this complexity.

If you've launched multiple vagrant boxes at the same time or you've explicitly chosen a port different from 2222, you need to use the port which is actually in use. You can find that out using:

vagrant ssh-config
like image 170
hek2mgl Avatar answered Oct 21 '22 12:10

hek2mgl


For permanently creating a host alias, you can add an entry to your ~/.ssh/config that looks like:

Host my-vagrant-box
     User vagrant
     HostName localhost
     Port 2222
     IdentityFile /Users/atesgoral/Sandbox/my-vagrant-box/.vagrant/machines/default/virtualbox/private_key
     IdentitiesOnly yes

Then you can simply do:

ssh my-vagrant-box

To determine the host port that's mapped to the SSH port on the guest (the Vagrant box):

vagrant port --guest 22

In this repo, you can find a shell script that you can run inside the directory with the Vagrantfile. It will generate a host entry that can be appended to ~/.ssh/config:

./ssh-config.sh >> ~/.ssh/config
like image 32
Ates Goral Avatar answered Oct 21 '22 13:10

Ates Goral