LoginSignup
0
0

More than 5 years have passed since last update.

Vagrant管理系コマンドを実行してみた。

Posted at

勝手にVagrant管理系コマンドと言っております。

Vagrantには、initやupが基本的なコマンドとして使われますが、他にもコマンドがありますが実際に実行してみないとよく分からないことが多々あります。
そこで、今回は管理系っぽいコマンドたちを実際に実行してみました。
一部、実行できてないものは後日更新します。

Vagrant管理系コマンド

vagrant connect

ホストOSの環境にアクセスするためのコマンド。vagrant shareを実行した場所のみアクセス可能。

vagrant global-status

マシンにログインしているユーザが使用するすべてのVagrant環境のステータスを出力する。
idは、マシン上のVagrant環境を管理するための番号で、vagrant destroyのパラメータに指定可能です。


$ vagrant global-status
id       name    provider   state    directory                           
-------------------------------------------------------------------------
4483bf8  default virtualbox poweroff /Users/xxxx/Vagrant/CentOS7       
37a038a  default virtualbox poweroff /Users/xxxx/Vagrant/Redmine       
b53102a  default virtualbox running  /Users/xxxx/Vagrant/Jenkins       
2693df4  default virtualbox running  /Users/xxxx/Vagrant/CentOS7-2     

The above shows information about all known Vagrant environments
on this machine. This data is cached and may not be completely
up-to-date (use "vagrant global-status --prune" to prune invalid
entries). To interact with any of the machines, you can go to that
directory and run Vagrant, or you can use the ID directly with
Vagrant commands from any directory. For example:
"vagrant destroy 1a2b3c4d"

vagrant login

HashiCorpのVagrant Cloudにログインします。Vagrantは、ログインせずに使用可能ですが、Vagrant shareやログインして使用可能となるBoxファイルを使用する際にログインが必要です。

vagrant port

ゲストOSのポートとホストOSのマッピングしているポートを表示します。


$ vagrant port
The forwarded ports for the machine are listed below. Please note that
these values may differ from values configured in the Vagrantfile if the
provider supports automatic port collision detection and resolution.

    22 (guest) => 2200 (host)

vagrant powershell

ゲストOSに対してpowershellを使用するためのプロンプトを開く。
Windows環境のみサポートなので、今回は試せませんでした。

vagrant provision

後日更新

vagrant rdp

後日更新

vagrant reload

ゲストOSを再起動します。(haltとupを実施します)
実際にコマンドを実行したらエラーが出てますが、これはローカルの問題だと思います。


$ vagrant reload
==> 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: 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: No guest additions were detected on the base box for this VM! Guest
    default: additions are required for forwarded ports, shared folders, host only
    default: networking, and more. If SSH fails on this machine, please install
    default: the guest additions and repackage the box to continue.
    default: 
    default: This is not an error message; everything may continue to work properly,
    default: in which case you may ignore this message.
==> default: Configuring and enabling network interfaces...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
==> default: Mounting shared folders...
    default: /vagrant => /Users/xxxx/Vagrant/CentOS7
Vagrant was unable to mount VirtualBox shared folders. This is usually
because the filesystem "vboxsf" is not available. This filesystem is
made available via the VirtualBox Guest Additions and kernel module.
Please verify that these guest additions are properly installed in the
guest. This is not a bug in Vagrant and is usually caused by a faulty
Vagrant box. For context, the command attempted was:

mount -t vboxsf -o uid=1000,gid=1000 vagrant /vagrant

The error output from the command was:

mount: unknown filesystem type 'vboxsf'

vagrant resume

中断していたゲストOSを再開します。
エラーになる・・・。
後日更新

vagrant share

後日更新

vagrant ssh

Vagrant環境(ゲストOS)にsshでログインします。


$ vagrant ssh
Last login: Sat Sep 22 15:00:14 2018 from 10.0.2.2
[vagrant@localhost ~]$ 

vagrant ssh-config

Vagrant環境にvagrant ssh ではなく、sshで接続するための設定情報を出力してくれます。


$ vagrant ssh-config
Host default
  HostName 127.0.0.1
  User vagrant
  Port 2200
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile /Users/xxxx/.vagrant.d/insecure_private_key
  IdentitiesOnly yes
  LogLevel FATAL

設定方法は、次のとおり。
vagrant ssh-configの標準術力をconfigファイルに出力


$ vagrant ssh-config > config

sshでログイン(defaultは、configファイルのHostの値)


$ ssh default -F config
Last login: Sun Sep 23 14:24:10 2018 from 10.0.2.2
[vagrant@localhost ~]$ ls

vagrant status

Vagrant(ゲストOS)の状態を出力します。


$ vagrant status
Current machine states:

default                   running (virtualbox)

The VM is running. To stop this VM, you can run `vagrant halt` to
shut it down forcefully, or you can run `vagrant suspend` to simply
suspend the virtual machine. In either case, to restart it again,
simply run `vagrant up`.

vagrant suspend

Vagrant(ゲストOS)を中断します。


$ vagrant suspend
==> default: Saving VM state and suspending execution...

vagrant statusで状態を確認すると・・・。


$ vagrant status
Current machine states:

default                   saved (virtualbox)

To resume this VM, simply run `vagrant up`.

vagrant validate

Vagrantfileに誤りがないかチェックします。


$ vagrant validate
Vagrantfile validated successfully.

試しに、config.vm.boxをconfig.vmv.boxに変更して再実行してみた。


 vagrant validate
There are errors in the configuration of this machine. Please fix
the following errors and try again:

vm:
* A box must be specified.

Vagrant:
* Unknown configuration section 'vmv'.

ちゃんと問題箇所を検出し、vmvというのは設定でないことを教えてくれました。

vagrant version

インストールしているVagrantのバージョンと最新のバージョンを出力します。


$ vagrant version
Installed Version: 2.1.5
Latest Version: 2.1.5

You're running an up-to-date version of Vagrant!

参考

Vagrantにssh接続するためのconfigファイルをvagrantのある場所に置く
Vagrantのprovisionでローカル開発環境をupと同時に整える (`・ω・´)ゞ
vagrantの便利に使えるプラグイン6選

0
0
0

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