Log in to Vagrant using the SSH command

Uncategorized

When logging into Vagrant, it’s common to move to the folder where ‘vagrant init’ was run (the directory containing the Vagrantfile) and execute the ‘vagrant ssh’ command.

However, there may be cases where you want to access Vagrant using the SSH command, such as when accessing through other applications.

Given this background, this will serve as a note on how to log into the Vagrant environment using the SSH command.

スポンサーリンク

Points of struggle

The local IP is defined in the Vagrantfile as follows:

config.vm.network "private_network", ip: "192.168.33.19"

Therefore, I assumed that SSH connection could be made using the following format, but this did not work.

# ssh vagrant@192.168.33.19 -p 2200
ssh: connect to host 192.168.33.19 port 2200: Connection refused

Solution

First, move to the folder where vagrant init was run (the directory containing the Vagrantfile).
In this case, let’s assume the Vagrantfile is located in the following folder.

# cd /Users/username/vagrant-sample/

Here, run the ‘vagrant ssh-config’command.
If Vagrant is not running, execute vagrant up in the target folder to start the environment.

When executed, the output should look something like this:

# vagrant ssh-config
Host default
  HostName 127.0.0.1
  User vagrant 
  Port 2200
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile /Users/username/vagrant-sample/.vagrant/machines/default/virtualbox/private_key
  IdentitiesOnly yes
  LogLevel FATAL

By incorporating the following items into the SSH command and executing it, you should be able to log in via SSH.

  • HostName (Required)
  • User (Required)
  • Port (Required)
  • IdentityFile (Optional)


When accessing with the IdentityFile, you can log in without entering a password.

$ ssh vagrant@127.0.0.1 -p 2200 -i /Users/username/vagrant-sample/.vagrant/machines/default/virtualbox/private_key

If you access without the IdentityFile, you will be prompted to enter a password.

$ ssh vagrant@127.0.0.1 -p 2200

The initial password for the “vagrant” user should be “vagrant”

It seems that access via “private_network” was not possible, and the key was the combination of HostName + Port defined in the ssh-config.
With this, you should now be able to log into the Vagrant environment using the SSH command!

Supplement

I was curious about how multiple Vagrant environments are distinguished.
In conclusion, it seems they are differentiated by the Port.

■ Config file for Environment A

$ cd /Users/username/vagrant-sample-A
$ vagrant ssh-config
Host default
  HostName 127.0.0.1
  User vagrant 
  Port 2201
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile /Users/username/vagrant-sample-A/.vagrant/machines/default/virtualbox/private_key
  IdentitiesOnly yes
  LogLevel FATAL


■ Config file for Environment B

$ cd /Users/username/vagrant-sample-B
$ vagrant ssh-config
Host default
  HostName 127.0.0.1
  User vagrant 
  Port 2202
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile /Users/username/vagrant-sample-B/.vagrant/machines/default/virtualbox/private_key
  IdentitiesOnly yes
  LogLevel FATAL

Therefore, when accessing Environment A and Environment B, it seems that simply replacing the Port number for each environment will allow proper connection.


■ SSH command to access Environment A

$ ssh vagrant@127.0.0.1 -p 2201 -i /Users/username/vagrant-sampleA/.vagrant/machines/default/virtualbox/private_key

■ SSH command to access Environment B

$ ssh vagrant@127.0.0.1 -p 2202 -i /Users/username/vagrant-sampleB/.vagrant/machines/default/virtualbox/private_key
タイトルとURLをコピーしました