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

### change virtual machine directory
```bash
$ cd /g/Dev/Vagrant/ubuntu/
```
### set private network to 'Vagrantfile'
```bash
$ vim Vagrantfile
```
Comment lines have been omitted.

```bash
Vagrant.configure("2") do |config|
  config.vm.box = "ubuntu/xenial64"
end
```
### set private network to 'Vagrant.configure("2")' item
```bash
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
```bash
$ 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'
```bash
$ 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

```bash
# System-wide bashrc file
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
```
### create home directory's '.bashrc'
```bash
$ vim ~/.bashrc
```
### add script to '.bashrc'
This script prevents the ping return text garbled

```bash
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
```bash
$ 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'
```bash
$ vim ~/Vagrantfile
```
Comment lines have been omitted.

```bash
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.

```bash
  # 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
```bash
$ pwd
/g/Dev/Vagrant/ubuntu

$ mkdir data

$ ll
data  ubuntu-xenial-16.04-cloudimg-console.log  Vagrantfile
```
### reload vagrant
```bash
$ 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
```bash
$ 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
```bash
$ 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
```bash
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'
 
```shell:bash
$ vim ~/Vagrantfile
```
+ add username and password

```shell:bash
  config.ssh.username = 'root'
  config.ssh.password = 'vagrant'
  config.ssh.insert_key = 'false' 
```

+ confirm setting 

```shell: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'
```shell: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
```shell:bash
$ su root
```
### copy 'authorized_keys' to root directory

```shell: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

```shell:bash
$ vagrant reload
```
+ ssh command login by vagrant user

```shell:bash
$ ssh -p 2222 -v vagrant@127.0.0.1 -i .vagrant/machines/default/virtualbox/private_key
```

```shell:bash
Last login: Fri Mar 15 19:50:25 2019 from 10.0.2.2
$ whoami
vagrant
```
+ ssh command login by root

```shell:bash
$ ssh -p 2222 -v root@127.0.0.1 -i .vagrant/machines/default/virtualbox/private_key
```

```shell:bash
Last login: Fri Mar 15 20:12:19 2019 from 10.0.2.2
# whoami
root
```

+ normal vagrant command login

```shell:bash
$ vagrant ssh
```

```shell:bash
Last login: Fri Mar 15 20:26:32 2019 from 10.0.2.2
# whoami
root
```