LoginSignup
0
1

More than 3 years have passed since last update.

【Ubuntu 16.04】VirtualBox+Vagrantを使用したインストール

Last updated at Posted at 2021-04-13

必要なツールおよびソフトウェア

作業ステップ

  • ステップ①:VirtualBox と Vagrant のインストール
  • ステップ②:Ubuntu Linux ゲストOSのインストールと初期設定

ステップ①:VirtualBox と Vagrant のインストール

VirtualBox のインストール

  1. VirtualBox の公式サイト(https://www.virtualbox.org/ )からインストーラーをダウンロードします。
  2. ダウンロードしたインストーラを起動し、表示される手順に従ってインストールを完了させます。 VirtualBoxのバージョン.png

Vagrant のインストール

  1. Vagrant の公式サイト(https://www.vagrantup.com/ )からインストーラーをダウンロードします。
  2. ダウンロードしたインストーラを起動し、表示される手順に従ってインストールを完了させます。
$ vagrant -v
Vagrant 2.2.15

ステップ②:Ubuntu Linux ゲストOSのインストールと初期設定

Ubuntu vagrant box ファイルのダウンロードとインストール

「vagrant box add」コマンドを実行して、Ubuntu の vagrant box ファイルのダウンロードとローカルファイルシステムへのインストールを行います。

  • 第1引数:vagrant box イメージの登録名
  • 第2引数:vagrant box ファイルのURL
$ vagrant box add ubuntu1604 https://cloud-images.ubuntu.com/xenial/current/xenial-server-cloudimg-amd64-vagrant.box
==> box: Box file was not detected as metadata. Adding it directly...
==> box: Adding box 'ubuntu1604' (v0) for provider:
    box: Downloading: https://cloud-images.ubuntu.com/xenial/current/xenial-server-cloudimg-amd64-vagrant.box
    box:
==> box: Successfully added box 'ubuntu1604' (v0) for 'virtualbox'!

「vagrant box list」コマンドで、インストールされたことを確認できます。

$ vagrant box list
・・・
ubuntu1604                   (virtualbox, 0)

Ubuntu ゲスト OS のインストール

「vagrant init」コマンドを実行して、ゲストOS用の Vagrantfile を作成します。
ここでは、インストールする作業ディレクトリを"ubuntu1604"にしています。

$ mkdir ubuntu1604
$ cd ubuntu1604/
$ vagrant init ubuntu1604
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.

仮想マシンとゲスト OS の起動

作業ディレクトリで「vagrant up」コマンドを実行し、Ubuntu ゲスト OS を起動します。

$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'ubuntu1604'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: ubuntu1604_default_1618327809921_28190
==> 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: 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:
    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: Mounting shared folders...
    default: /vagrant => D:/Vagrant/ubuntu1604

ゲスト OS へのログイン

作業ディレクトリで「vagrant ssh」コマンドを実行し、ゲスト OS へログインします。

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

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

0 packages can be updated.
0 of these updates are security updates.

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

初期設定色々

  • Bash シェルのプロンプトを短くする
vagrant@ubuntu-xenial:~$ echo 'export PS1="\[\e[34;1m\]\w\[\e[m\]$ "' >> ~/.profile
vagrant@ubuntu-xenial:~$ . ~/.profile
~$
  • ユーザーパスワードを変更する
~$ sudo passwd vagrant
Enter new UNIX password: ← 新しいパスワードを入力する
Retype new UNIX password: ← 同じパスワードを再入力する
passwd: password updated successfully
  • タイムゾーンを日本標準時(JST)に変更する
~$ sudo timedatectl set-timezone Asia/Tokyo
~$ timedatectl
      Local time: Wed 2021-04-14 00:44:29 JST
  Universal time: Tue 2021-04-13 15:44:29 UTC
        RTC time: Tue 2021-04-13 15:44:29
       Time zone: Asia/Tokyo (JST, +0900)
 Network time on: yes
NTP synchronized: no
 RTC in local TZ: no
  • 【2021.04.16 追記】jq コマンドのインストール
~$ sudo apt -y install jq
Reading package lists... Done
Building dependency tree
(中略)
Setting up jq (1.5+dfsg-1ubuntu0.1) ...
Processing triggers for libc-bin (2.23-0ubuntu11.2) ...

~$ jq --version
jq-1.5-1-a5b5cbe

ゲスト OS からのログアウト

ゲスト OS で「exit」コマンドを実行し、ゲスト OS からログアウトします。

~$ exit
logout
Connection to 127.0.0.1 closed.

ゲスト OS の停止

作業ディレクトリで「vagrant halt」コマンドを実行し、ゲスト OS を停止します。
停止中のゲスト OS は、「vagrant up」コマンドで再起動できます。

$ vagrant halt
==> default: Attempting graceful shutdown of VM...
0
1
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
1