VirtualBoxをインストール
Vagrantには仮想化ソフトウェアは含まれていないので、VirtualBoxをインストールしておきます。VirtualBoxは無料です。
Vagrantをインストール
公式サイトのDownloadからdmgファイルをダウンロードしてインストールします。
CentOS7環境を作成
boxを新規追加
まずは環境の元になるboxを呼ばれるもの(仮想マシンのイメージファイル)をMacに新規追加します。
centos/7 という公式boxがあるようなのでそれを使うことにします。
参考)Discover Vagrant Boxes - Vagrant Cloud
# vagrant box list There are no installed boxes! Use `vagrant box add` to add some.
まだboxはありません。 そこで、centos/7 を追加します。 VirtualBoxを使っているのでproviderは 3 を選択。
# vagrant box add centos/7 ==> box: Loading metadata for box 'centos/7' box: URL: https://vagrantcloud.com/centos/7 This box can work with multiple providers! The providers that it can work with are listed below. Please review the list and choose the provider you will be working with. 1) hyperv 2) libvirt 3) virtualbox 4) vmware_desktop Enter your choice: 3 ==> box: Adding box 'centos/7' (v1902.01) for provider: virtualbox box: Downloading: https://vagrantcloud.com/centos/boxes/7/versions/1902.01/providers/virtualbox.box box: Download redirected to host: cloud.centos.org ==> box: Successfully added box 'centos/7' (v1902.01) for 'virtualbox'!
boxが追加されたか確認してみます。
# vagrant box list centos/7 (virtualbox, 1902.01)
追加されています。
初期設定
今回はcentos7というディレクトリを作成して、そこに環境を作成していきます。
# mkdir centos7 # ls -l total 0 drwxr-xr-x 2 norris staff 64 6 18 23:21 centos7
init で初期設定を行います。 コマンドライン引数には前項で追加した centos/7 というboxを指定します。
# cd centos7/ # vagrant init centos/7 A `Vagrantfile` has been placed in this directory. You are now ready to `vagrant up` your first virtual environment! Please read the comments in the Vagrantfile as well as documentation on `vagrantup.com` for more information on using Vagrant. # ls -l total 8 -rw-r--r-- 1 norris staff 3015 6 18 23:22 Vagrantfile
ディレクトリ内に Vagrantfile というファイルが作成されていれば完了です。 ちなみに、Vagrantfile 仮想マシンのIPアドレスなどの各種設定が記載されています。
status で仮想マシンの状態を確認できます。
# vagrant status Current machine states: default not created (virtualbox) The environment has not yet been created. Run `vagrant up` to create the environment. If a machine is not created, only the default provider will be shown. So if a provider is not listed, then the machine is not created for that environment.
default というのが仮想マシンの名前です。 not created になっていますので、まだ仮想マシンは作成されていない状況です。
仮想マシンを起動
初期設定は完了しているので、ひとまず仮想マシンを起動してみます。 up で仮想マシンが起動します。
norris@2019/06/18 23:36:32 centos7 # vagrant up Bringing machine 'default' up with 'virtualbox' provider... ==> default: Checking if box 'centos/7' version '1902.01' 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: 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: default: Vagrant insecure key detected. Vagrant will automatically replace default: this with a newly generated keypair for better security. default: default: Inserting generated public key within guest... default: Removing insecure key from the guest if it's present... default: Key inserted! Disconnecting and reconnecting using new SSH 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: Rsyncing folder: /Users/norris/Vagrant/centos7/ => /vagrant
状態を確認してみます。
# 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`.
running になりましたので、仮想マシンが作成されて稼働中ということです。
ほんとうに仮想マシンが作成されているか、VirtualBoxのコンソールを起動してみます。
仮想マシンがちゃんと存在して実行中になっているのでOKです。
仮想マシンにログイン
起動した仮想マシンにログインしてみます。 ssh で仮想マシンにsshで接続できます。
# vagrant ssh [vagrant@localhost ~]$ cat /etc/redhat-release CentOS Linux release 7.6.1810 (Core)
ログインできて、CentOS7になっています。
ログインユーザーは vagrant になっています。
vagrant@localhost ~]$ sudo tail /var/log/messages Jun 18 14:39:18 localhost systemd: Started Session 2 of user vagrant. Jun 18 14:39:21 localhost systemd: Started Session 3 of user vagrant. Jun 18 14:39:21 localhost systemd-logind: New session 3 of user vagrant. Jun 18 14:39:21 localhost systemd-logind: Removed session 3. Jun 18 14:39:21 localhost systemd-logind: Removed session 2. Jun 18 14:39:21 localhost systemd: Removed slice User Slice of vagrant. Jun 18 14:40:22 localhost chronyd[1319]: Selected source 133.243.238.243 Jun 18 14:41:03 localhost systemd: Created slice User Slice of vagrant. Jun 18 14:41:03 localhost systemd: Started Session 4 of user vagrant. Jun 18 14:41:03 localhost systemd-logind: New session 4 of user vagrant. [vagrant@localhost ~]$ sudo su [root@localhost vagrant]# exit exit [vagrant@localhost ~]$ exit logout Connection to 127.0.0.1 closed.
sudo も出来ていますし、root ユーザへの変更もできました。
仮想マシンを停止
halt で仮想マシンを停止します。
# vagrant halt ==> default: Attempting graceful shutdown of VM...
状態を確認してみます。
# vagrant status Current machine states: default poweroff (virtualbox) The VM is powered off. To restart the VM, simply run `vagrant up`
poweroff になって、仮想マシンが停止しています。