0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

how to connect host OS to guest OS on 'vagrant' + 'virtual box'

Last updated at Posted at 2019-03-10

change virtual machine directory

$ cd /g/Dev/Vagrant/ubuntu/

set private network to 'Vagrantfile'

$ vim Vagrantfile

Comment lines have been omitted.

Vagrant.configure("2") do |config|
  config.vm.box = "ubuntu/xenial64"
end

set private network to 'Vagrant.configure("2")' item

Vagrant.configure("2") do |config|
  #
  # Create a private network, which allows host-only access to the machine
  # using a specific IP.
  config.vm.network "private_network", ip: "192.168.33.10"
  #
end

reload vagrant

$ vagrant reload
==> default: Attempting graceful shutdown of VM...
==> default: Checking if box 'ubuntu/xenial64' version '20190308.0.0' is up to date...
==> default: Clearing any previously set forwarded ports...
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
    default: Adapter 2: hostonly
==> default: Forwarding ports...
    default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Running 'pre-boot' VM customizations...
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
    default: The guest additions on this VM do not match the installed version of
    default: VirtualBox! In most cases this is fine, but in rare cases it can
    default: prevent things such as shared folders from working properly. If you see
    default: shared folder errors, please make sure the guest additions within the
    default: virtual machine match the version of VirtualBox you have installed on
    default: your host and reload your VM.
    default:
    default: Guest Additions Version: 5.1.38
    default: VirtualBox Version: 6.0
==> default: Configuring and enabling network interfaces...
==> default: Mounting shared folders...
    default: /vagrant => G:/Dev/Vagrant/ubuntu
==> default: Machine already provisioned. Run `vagrant provision` or use the `--provision`
==> default: flag to force provisioning. Provisioners marked to run always will still run.

change read from home directory '.bashrc'

reopen Git-Bash by System Manager

open 'bash.bashrc'

$ vim "/c/Program Files/Git/etc/bash.bashrc"

add script under '# System-wide bashrc file' comment.

So that you don't have to enter command $ source .bashrc every time

# System-wide bashrc file
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi

create home directory's '.bashrc'

$ vim ~/.bashrc

add script to '.bashrc'

This script prevents the ping return text garbled

function wincmd() {
    CMD=$1
    shift
    $CMD $* 2>&1 | iconv -f cp932 -t utf-8
}
alias ping='wincmd ping'

confirm ping command

reopen Git-Bash

ping command input

$ ping 192.168.33.10

192.168.33.10 に ping を送信しています 32 バイトのデータ:
192.168.33.10 からの応答: バイト数 =32 時間 <1ms TTL=64
192.168.33.10 からの応答: バイト数 =32 時間 <1ms TTL=64
192.168.33.10 からの応答: バイト数 =32 時間 <1ms TTL=64
192.168.33.10 からの応答: バイト数 =32 時間 <1ms TTL=64

192.168.33.10 の ping 統計:
    パケット数: 送信 = 4、受信 = 4、損失 = 0 (0% の損失)、
ラウンド トリップの概算時間 (ミリ秒):
    最小 = 0ms、最大 = 0ms、平均 = 0ms

directories synchronize between host OS and guest OS

open 'Vagrantfile'

$ vim ~/Vagrantfile

Comment lines have been omitted.

Vagrant.configure("2") do |config|
  config.vm.box = "ubuntu/xenial64"
  config.vm.network "private_network", ip: "192.168.33.10"
end

add 'synced_Folders' to 'Vagrant.configure("2")' item

directory "./data" on host OS synchronize to directory "/vagrant_data" on guest OS.

  # Share an additional folder to the guest VM. The first argument is
  # the path on the host to the actual folder. The second argument is
  # the path on the guest to mount the folder. And the optional third
  # argument is a set of non-required options.
  # config.vm.synced_folder "../data", "/vagrant_data"
config.vm.synced_folder "./data", "/vagrant_data"

create "./data" holder on host OS

$ pwd
/g/Dev/Vagrant/ubuntu

$ mkdir data

$ ll
data  ubuntu-xenial-16.04-cloudimg-console.log  Vagrantfile

reload vagrant

$ vagrant reload
==> default: Attempting graceful shutdown of VM...
==> default: Checking if box 'ubuntu/xenial64' version '20190308.0.0' is up to date...
==> default: Clearing any previously set forwarded ports...
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
    default: Adapter 2: hostonly
==> default: Forwarding ports...
    default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Running 'pre-boot' VM customizations...
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: Warning: Connection reset. Retrying...
    default: Warning: Connection aborted. Retrying...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
    default: The guest additions on this VM do not match the installed version of
    default: VirtualBox! In most cases this is fine, but in rare cases it can
    default: prevent things such as shared folders from working properly. If you see
    default: shared folder errors, please make sure the guest additions within the
    default: virtual machine match the version of VirtualBox you have installed on
    default: your host and reload your VM.
    default:
    default: Guest Additions Version: 5.1.38
    default: VirtualBox Version: 6.0
==> default: Configuring and enabling network interfaces...
==> default: Mounting shared folders...
    default: /vagrant => G:/Dev/Vagrant/ubuntu
    default: /vagrant_data => G:/Dev/Vagrant/ubuntu/data
==> default: Machine already provisioned. Run `vagrant provision` or use the `--provision`
==> default: flag to force provisioning. Provisioners marked to run always will still run.

confirm synchronized between host OS and guest OS

create test data file

$ touch ./data/test.data

$ ll ./data
total 0
-rw-r--r-- 1 teruroom 197618 0 3月  10 18:27 test.data

ssh login to guest OS

$ vagrant ssh
Welcome to Ubuntu 16.04.6 LTS (GNU/Linux 4.4.0-142-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

  Get cloud support with Ubuntu Advantage Cloud Guest:
    http://www.ubuntu.com/business/services/cloud

0 個のパッケージがアップデート可能です。
0 個のアップデートはセキュリティアップデートです。

New release '18.04.2 LTS' available.
Run 'do-release-upgrade' to upgrade to it.


Last login: Sun Mar 10 02:30:18 2019 from 10.0.2.2

confirm guest OS "/vagrant_data" directory

vagrant@ubuntu-xenial:~$ ll /vagrant_data/
合計 4
drwxrwxrwx  1 vagrant vagrant    0  3月 10 09:27 ./
drwxr-xr-x 25 root    root    4096  3月 10 09:23 ../
-rwxrwxrwx  1 vagrant vagrant    0  3月 10 09:27 test.data*

login user set 'root'

  • open 'Vagrantfile'
bash
$ vim ~/Vagrantfile
  • add username and password
bash
  config.ssh.username = 'root'
  config.ssh.password = 'vagrant'
  config.ssh.insert_key = 'false' 
  • confirm setting
bash
$ vagrant ssh-config
Host default
  HostName 127.0.0.1
  User root
  Port 2222
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile G:/Dev/Vagrant/ubuntu/.vagrant/machines/default/virtualbox/private_key
  IdentitiesOnly yes
  LogLevel FATAL

login by user 'vagrant'

bash
$ vagrant ssh
Welcome to Ubuntu 16.04.6 LTS (GNU/Linux 4.4.0-143-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

  Get cloud support with Ubuntu Advantage Cloud Guest:
    http://www.ubuntu.com/business/services/cloud

0 個のパッケージがアップデート可能です。
0 個のアップデートはセキュリティアップデートです。

New release '18.04.2 LTS' available.
Run 'do-release-upgrade' to upgrade to it.


Last login: Fri Mar 15 19:54:15 2019 from 10.0.2.2

promote root

bash
$ su root

copy 'authorized_keys' to root directory

bash
$ cp /home/vagrant/.ssh/authorized_keys ~/.ssh/
$ chmod 600 ~/.ssh/authorized_keys
$ ll ~/.ssh/
合計 12
drwx------ 2 root root 4096  3月  9 18:20 ./
drwx------ 7 root root 4096  3月 15 20:01 ../
-rw------- 1 root root  389  3月 15 19:47 authorized_keys

confirm ssh login

  • reload Vagrant
bash
$ vagrant reload
  • ssh command login by vagrant user
bash
$ ssh -p 2222 -v vagrant@127.0.0.1 -i .vagrant/machines/default/virtualbox/private_key
bash
Last login: Fri Mar 15 19:50:25 2019 from 10.0.2.2
$ whoami
vagrant
  • ssh command login by root
bash
$ ssh -p 2222 -v root@127.0.0.1 -i .vagrant/machines/default/virtualbox/private_key
bash
Last login: Fri Mar 15 20:12:19 2019 from 10.0.2.2
# whoami
root
  • normal vagrant command login
bash
$ vagrant ssh
bash
Last login: Fri Mar 15 20:26:32 2019 from 10.0.2.2
# whoami
root
0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?