LoginSignup
1
3

More than 5 years have passed since last update.

macOS High SierraにHomebrew CaskでVagrantをインストールしてCentOS7をつくってみる方法

Posted at
  • 環境
    • OS : macOS High Sierra バージョン 10.13.6
    • Homebrew : 1.8.0
    • Homebrew Cask : 1.8.0

Homebrewの準備をする

# doctorで健康診断する
$ brew doctor
Your system is ready to brew.

# updateして新しくする
$ brew update
Updated 2 taps (homebrew/core and homebrew/cask).
==> New Formulae
<省略>

# バージョンを確認する
$ brew -v
Homebrew 1.8.0
Homebrew/homebrew-core (git revision e8065; last commit 2018-10-23)
Homebrew/homebrew-cask (git revision 26980; last commit 2018-10-23)

Vagrantをインストールする

# Vagrantはどんなものあるのか見てみる...と、Homebrew Caskにあった。
$ brew search vagrant
==> Formulae
vagrant-completion

==> Casks
vagrant                    vagrant-manager            vagrant-vmware-utility

# 初めてなのでVagrantだけインストールする
$ brew cask install vagrant
Updating Homebrew...
To restore the stashed changes to /usr/local/Homebrew run:
  'cd /usr/local/Homebrew && git stash pop'
==> Auto-updated Homebrew!
Updated 1 tap (homebrew/cask).

==> Satisfying dependencies
==> Downloading https://releases.hashicorp.com/vagrant/2.2.0/vagrant_2.2.0_x86_6
######################################################################## 100.0%
==> Verifying SHA-256 checksum for Cask 'vagrant'.
==> Installing Cask vagrant
==> Running installer for vagrant; your password may be necessary.
==> Package installers may write to any location; options such as --appdir are i
Password:
installer: Package name is Vagrant
installer: Installing at base path /
installer: The install was successful.
🍺  vagrant was successfully installed!

# バージョンをみてみる
$ vagrant version
Installed Version: 2.2.0
Latest Version: 2.2.0

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

使う前にこの用語は何?とかどんなコマンド使うの?とかうっすら見ておく

VirtualBox用のCentOS7のBoxを追加する

  1. Vagrant Cloud からCentOS7のboxを選ぶ。
  2. 選んだら追加する。
# VirtualBoxを持っているのでVirtualBox用を選んで追加する。
$ 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 #<------------ここでVirtualBox用を選択
==> box: Adding box 'centos/7' (v1809.01) for provider: virtualbox
    box: Downloading: https://vagrantcloud.com/centos/boxes/7/versions/1809.01/providers/virtualbox.box
    box: Download redirected to host: cloud.centos.org
==> box: Successfully added box 'centos/7' (v1809.01) for 'virtualbox'!

# boxが追加されたことを確認してみる
$ vagrant box list
centos/7 (virtualbox, 1809.01)

Vagrantfileをつくる

# Vagrantfileを入れるディレクトリをつくる
$ mkdir -p vagrant/centos7

# 移動する
$ cd vagrant/centos7/

# Vagrantfileをつくる
$ 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 mana  staff  3015 10 23 23:59 Vagrantfile

# 初めてなので中身を見てみる
$ cat Vagrantfile 
# -*- mode: ruby -*-
# vi: set ft=ruby :

# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure("2") do |config|
  # The most common configuration options are documented and commented below.
  # For a complete reference, please see the online documentation at
  # https://docs.vagrantup.com.

  # Every Vagrant development environment requires a box. You can search for
  # boxes at https://vagrantcloud.com/search.
  config.vm.box = "centos/7"

  # Disable automatic box update checking. If you disable this, then
  # boxes will only be checked for updates when the user runs
  # `vagrant box outdated`. This is not recommended.
  # config.vm.box_check_update = false

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine. In the example below,
  # accessing "localhost:8080" will access port 80 on the guest machine.
  # NOTE: This will enable public access to the opened port
  # config.vm.network "forwarded_port", guest: 80, host: 8080

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine and only allow access
  # via 127.0.0.1 to disable public access
  # config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1"

  # 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"

  # Create a public network, which generally matched to bridged network.
  # Bridged networks make the machine appear as another physical device on
  # your network.
  # config.vm.network "public_network"

  # 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"

  # Provider-specific configuration so you can fine-tune various
  # backing providers for Vagrant. These expose provider-specific options.
  # Example for VirtualBox:
  #
  # config.vm.provider "virtualbox" do |vb|
  #   # Display the VirtualBox GUI when booting the machine
  #   vb.gui = true
  #
  #   # Customize the amount of memory on the VM:
  #   vb.memory = "1024"
  # end
  #
  # View the documentation for the provider you are using for more
  # information on available options.

  # Enable provisioning with a shell script. Additional provisioners such as
  # Puppet, Chef, Ansible, Salt, and Docker are also available. Please see the
  # documentation for more information about their specific syntax and use.
  # config.vm.provision "shell", inline: <<-SHELL
  #   apt-get update
  #   apt-get install -y apache2
  # SHELL
end

つくったCentOS7を起動してみる

$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'centos/7'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'centos/7' is up to date...
==> default: Setting the name of the VM: centos7_default_1540307478346_24305
==> 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/mana/Dropbox/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`.

CentOS7に接続してみる

$ vagrant ssh
[vagrant@localhost ~]$ pwd
/home/vagrant
[vagrant@localhost ~]$ ls -l
total 0
[vagrant@localhost ~]$ logout
Connection to 127.0.0.1 closed.

CentOS7を停止する

halt :(日本語)停止する、立ち止まる

$ vagarant halt
-bash: vagarant: command not found
mananoMacBookAir:centos7 mana$ 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`
1
3
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
1
3