1. はじめに
本記事では、Scientific LinuxのBoxファイルの作成手順について記述しています。
2. 前提条件
下記の環境でBoxファイルを作成します。
<作成環境>
項目 | 内容 |
---|---|
プロバイダー | VirtualBox |
メモリ | 512MB |
rootパスワード | vagrant |
OS | Scientific Linux 6.5 (64bit版) |
周辺機器 | オーディオを有効化(A) と USBコントローラを有効化(U)をオフにする |
ポートフォワーディング | 有効にする |
*) ポートフォワーディングを有効にするために、下記の作業を行ってください。
(1) VirtualBoxで「設定(S)」をクリックします。
(2) 「ネットワーク」をクリックし、「アダプター1」をクリックします。
(3) 「割り当て(A)」で「NAT」を選択し、「ポートフォワーディング(P)」をクリックします。
(4) 「新規ルールを追加」をクリックし、新規ルールを追加します。
<新規ルール>
項目 | 内容 |
---|---|
名前 | SSH |
プロトコル | TCP |
ホストIP | 空欄 |
ポストポート | 2222 |
ゲストIP | 空欄 |
ゲストポート | 22 |
3. Boxファイルの作成
(1) ネットワークの設定
下記のコマンドを実行して、eth0を起動時に立ち上がるようにします。
$ sed -i 's/ONBOOT=no/ONBOOT=yes/g' /etc/sysconfig/network-scripts/ifcfg-eth0
$ /etc/init.d/network restart
(2) yumの更新 + OpenSSH Clientのインストール
下記のコマンドを実行して、yumの更新とOpenSSH Clientのインストールをします。
$ yum -y update
$ yum -y install openssh-clients
$ reboot
(3) vagrantユーザーの作成
Vagrantが使用するvagrantユーザーを下記の手順で作成します。
1. ユーザー作成
$ groupadd vagrant -g 500
$ useradd vagrant -g 500 -u 500
$ echo "vagrant" | passwd --stdin vagrant
2. sudoerの設定
$ cat << __EOF__ >> /etc/sudoers.d/vagrant
%vagrant ALL=(ALL) NOPASSWD: ALL
Defaults:%vagrant !requiretty
__EOF__
3. ssh設定
$ su vagrant
$ mkdir ~/.ssh
$ chmod 700 ~/.ssh
$ curl -s https://raw.github.com/mitchellh/vagrant/master/keys/vagrant.pub > ~/.ssh/authorized_keys
$ chmod 600 ~/.ssh/authorized_keys
4. VirtualBoxのバージョンの保存
$ echo "(VirtualBoxのバージョン)" > ~/.vbox_version
vagrantユーザーを作成したら以下のコマンドを実行して、rootユーザーに切り替えます。
$ exit
(4) Virtual Box Guest Additionsのインストール
VirtualBox VMから「Devices」-「Install Guest Additions CD Image...」をクリックします。
クリック後、下記のコマンドを実行します。
$ yum -y install gcc perl kernel-devel
$ mount /dev/cdrom /mnt
$ sh /mnt/VBoxLinuxAdditions.run
Virtual Box Guest Additionsを有効にするために、再起動します。
$ reboot
再起動したら、使わないライブラリを削除します。
$ yum -y remove gcc perl kernel-devel
$ yum clean all
(5) sshの設定
viエディタで/etc/ssh/sshd_configを開きます。
/etc/ssh/sshd_configを開いたら、下記の通りに変更します。
# Disable legacy (protocol version 1) support in the server for new
# installations. In future the default will change to require explicit
# activation of protocol 1
Protocol 2
# Logging
# obsoletes QuietMode and FascistLogging
SyslogFacility AUTHPRIV
RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys
#AuthorizedKeysCommand none
#AuthorizedKeysCommandRunAs nobody
# To disable tunneled clear text passwords, change to no here!
PermitEmptyPasswords no
PasswordAuthentication no
# Change to no to disable s/key passwords
ChallengeResponseAuthentication no
# GSSAPI options
GSSAPIAuthentication yes
GSSAPICleanupCredentials yes
# Set this to 'yes' to enable PAM authentication, account processing,
# and session processing. If this is enabled, PAM authentication will
# be allowed through the ChallengeResponseAuthentication and
# PasswordAuthentication. Depending on your PAM configuration,
# PAM authentication via ChallengeResponseAuthentication may bypass
# the setting of "PermitRootLogin without-password".
# If you just want the PAM account and session checks to run without
# PAM authentication, then enable this but set PasswordAuthentication
# and ChallengeResponseAuthentication to 'no'.
UsePAM no
# Accept locale-related environment variables
AcceptEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES
AcceptEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT
AcceptEnv LC_IDENTIFICATION LC_ALL LANGUAGE
AcceptEnv XMODIFIERS
X11Forwarding no
PrintMotd no
PrintLastLog yes
UseDNS no
# no default banner path
Banner none
# override default of no subsystems
Subsystem sftp /usr/libexec/openssh/sftp-server
AllowGroups vagrant
AllowUsers vagrant
sshの設定を反映させるために、sshを再起動します。
$ /etc/init.d/sshd restart
(6) ネットワーク関連情報の削除
仮想マシンを起動した際に紐付けられたMACアドレス、eth、DHCP情報を削除します。
$ rm -rf /etc/udev/rules.d/70-persistent-net.rules
$ mkdir /etc/udev/rules.d/70-persistent-net.rules
$ chmod 0600 /etc/udev/rules.d/70-persistent-net.rules
$ rm -f /var/lib/dhclient/dhclient-eth0.leases
(7) 仮想マシンのシャットダウン
Boxファイルを作成するために、仮想マシンをシャットダウンします。
$ halt
(8) Boxファイルを作成する
下記のコマンドを実行して、Boxファイルを作成します。
$ vagrant package --base "vagrant-scientific65-x86_64" --output "vagrant-scientific65-x86_64.box"