0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

VagrantでCentOS7インストール

Last updated at Posted at 2018-05-29

環境

Mac OS
Vagrant-1.8.5
Oracle VirtualBox 5.1.2

手順

vagrantで仮想サーバーを構築する際は、Vagrantfileが必要になるので用意する

Vagrantfile
Vagrant.configure("2") do |config|
 
  config.ssh.insert_key = false
 
  config.vm.define "[VM Name]" do |[VM Name]|
    [VM Name].vm.box = "centos/7"
    [VM Name].vm.network :private_network, ip: "192.168.xxx.xxx"
  end

  config.vm.provider "virtualbox" do |vb|
    vb.gui = false             ← vagrant upするたびにウィンドウを開かないようにする
    vb.name = "[VM Name]"
  end

# Synced Folder
#  config.vm.synced_folder "[Host Directory]", "[Guest Directory]", type: "nfs"

end

ちなみに

[VM Name].vm.box = "centos/7"

の設定を変更すると、いろんなサーバーがサクッと作れちゃういます
https://app.vagrantup.com/boxes/search

vagrantfileを用意したディレクトリでvagrant upを実行

> vagrant up
〜
==> [VM Name]: Configuring and enabling network interfaces...

エラーが出ずに終了したら、vagrant sshでサーバーにログインする

> vagrant ssh

ここからは個人的に良くやる初期設定

VMなんでSELinux切る

一時的に切る場合は以下コマンド実行

> setenforce 0

永久的に切る場合は以下のファイルを修正

/etc/selinux/config
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
# SELINUX=enforcing   ← この行をコメントアウト
SELINUX=disabled     ← この行を追加
# SELINUXTYPE= can take one of three two values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected.
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted

SSHの公開鍵認証を設定

サーバー側のSSH設定を変更

〜
#PubkeyAuthentication yes  ← コメント解除

設定を変更したらSSHの再起動

> systemctl restart sshd

ホストマシン側で鍵を作成

Mac
$ ssh-keygen -t rsa
〜
# 鍵の出力先を変更する場合は下記に出力先を入力
Enter file in which to save the key (/Users/hoge/.ssh/id_rsa): [出力先入力]

# パスワードを設定したい場合は下記入力
Enter passphrase (empty for no passphrase): [パスフレーズ]
Enter same passphrase again: [パスフレーズ]

公開鍵をサーバーに設置

Mac
$ cd ~/.ssh
$ scp id_rsa.pub hoge@xxx.xxx.xxx.xxx:/
Server
> cd ~

# authorized_keysがない場合のみ作成
> touch ~/.ssh/authorized_keys

# 先程送った公開鍵を追記する
> cat id_rsa.pub >> authorized_keys

# 権限を設定
> chmod 700 ~/.ssh
> chmod 600 ~/.ssh/authorized_keys
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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?