環境篇:Virtualbox+Vagrant安裝Centos7
1 安裝Vagrant
Vagrant下載地址:https://www.vagrantup.com/
Vagrant百度網盤:https://pan.baidu.com/s/1jqnMml024niqWQPbrGVuJw提取碼:xv8h
傻瓜安裝即可
vagrant -v 查看版本
2 下載virtualbox.box鏡像
virtualbox.box百度網盤:https://pan.baidu.com/s/1EBrysc13fd0qf9EbT5rKdw提取碼:1dk4
3 安裝Virtualbox
virtualbox下載地址:https://www.virtualbox.org
virtualbox百度網盤:https://pan.baidu.com/s/1InyLZ-8Zmjuj7Qyk0Qg1Uw提取碼:ewty
傻瓜安裝即可,注意和Vagrant有版本兼容問題
4 安裝centos7
4.1 添加本地centos/7鏡像
找到下載好的virtualbox.box文件目錄
使用
vagrant box add centos/7 C:\Users\SMZC\Desktop\virtualbox\virtualbox.box
添加vagrant鏡像使用
vagrant box list
查詢鏡像
4.2 創建虛擬機環境
- 新建一個文件夾,並進入命令行cmd
- 使用
vagrant init
命令
- 修改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.
#拉取鏡像centos/7
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
#采用橋接網絡,共享主機網絡
config.vm.network "public_network"
# 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
#虛擬機名字heaton-centos7,內存,核數
config.vm.provider "virtualbox" do |vb|
vb.memory = "4096"
vb.name= "heaton-centos7"
vb.cpus= 2
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
- 在Vagrantfile文件命令行目錄,使用命令
vagrant up
創建啟動虛擬機
4.3 修改root密碼
-
使用
vagrant ssh
命令進入剛剛創建的虛擬機 -
使用
sudo -i
命令進入root用戶 -
使用
vi /etc/ssh/sshd_config
命令修改sshd_config使root用戶可以使用密碼登錄
PasswordAuthentication yes
- 使用
passwd
設置密碼
- 使用
systemctl restart sshd
重啟密碼服務 - 使用
ip add
查看ip即可使用遠程工具登錄root用戶
5 復制虛擬機
- 關閉需要復制的虛擬機
- 進入虛擬機所在目錄
vagrant package --output heaton-centos7.box
- 將上面得到的heaton-centos7.box文件添加進vagrant鏡像中
vagrant box add heaton-centos77 heaton-centos7.box
- 進入一個新目錄,生成新的虛擬機Vagrantfile
vagrant init heaton-centos77
- 啟動,則得到一個一模一樣的虛擬機(注意修改網絡)
vagrant up
6 一次性創建多台centos7
6.1 添加本地centos/7鏡像
找到下載好的virtualbox.box文件目錄
使用
vagrant box add centos/7 C:\Users\SMZC\Desktop\virtualbox\virtualbox.box
添加vagrant鏡像使用
vagrant box list
查詢鏡像
6.2 創建虛擬機環境
- 新建一個文件夾,並進入命令行cmd
- 使用
type nul>Vagrantfile
命令創建Vagrantfile文件
- 修改Vagrantfile文件
- 先查詢本機ip記住前三段,需要在Vagrantfile中指定
- 以下是創建3台的Vagrantfile
boxes = [
{
:name => "master",
:eth1 => "192.168.192.10",
:mem => "2048",
:cpu => "2",
:sshport => 22230
},
{
:name => "slave1",
:eth1 => "192.168.192.11",
:mem => "2048",
:cpu => "2",
:sshport => 22231
},
{
:name => "slave2",
:eth1 => "192.168.192.12",
:mem => "2048",
:cpu => "2",
:sshport => 22232
}
]
Vagrant.configure(2) do |config|
config.vm.box = "centos/7"
boxes.each do |opts|
config.vm.define opts[:name] do |config|
config.vm.hostname = opts[:name]
config.vm.network :public_network, ip: opts[:eth1]
config.vm.network "forwarded_port", guest: 22, host: 2222, id: "ssh", disabled: "true"
config.vm.network "forwarded_port", guest: 22, host: opts[:sshport]
config.vm.provider "vmware_fusion" do |v|
v.vmx["memsize"] = opts[:mem]
v.vmx["numvcpus"] = opts[:cpu]
end
config.vm.provider "virtualbox" do |v|
v.customize ["modifyvm", :id, "--memory", opts[:mem]]
v.customize ["modifyvm", :id, "--cpus", opts[:cpu]]
v.customize ["modifyvm", :id, "--name", opts[:name]]
end
end
end
end
- 在Vagrantfile文件命令行目錄,使用命令
vagrant up
創建啟動虛擬機
6.3 修改root密碼
-
使用命令進入剛剛創建的虛擬機,如下為上面創建的3台虛擬機,可以選擇多開cmd,或者依次處理。
vagrant ssh master
vagrant ssh slave1
vagrant ssh slave2
-
使用
sudo -i
命令進入root用戶 -
使用
vi /etc/ssh/sshd_config
命令修改sshd_config文件,使root用戶可以使用密碼登錄
PasswordAuthentication yes
- 使用
passwd
設置密碼
- 使用
systemctl restart sshd
重啟密碼服務 - 即可使用遠程工具登錄Vagrantfile上指定ip的root用戶。