1、去官网下载安装vagrant和virtual
Vagrant官网
:https://www.vagrantup.com/ 【默认安装即可】
Virtual Box官网
:https://www.virtualbox.org/ 【默认安装即可】
2、下载box
https://vagrantcloud.com/centos/boxes/7/versions/2004.01/providers/virtualbox.box
3、安装虚拟机
01 `创建D:\VM\docker-centos7文件夹,并进入此目录`
mkdir -p D:\VM\single 【创建文件夹】
cd D:\VM\single 【进入目录,如果跨盘符的话,需要先跳转盘符,输入“D:”后回车】
02 `添加virtualbox`
# 将下载的box复制到D:\VM目录
vagrant box add centos/7 D:\VM\virtualbox.box 【添加virtualbox】
vagrant box list 【查看是否添加成功】
03 `在目录中打开cmd命令`
vagrant init centos/7 【此时会在当前目录下生成Vagrantfile文件】
04 `开启虚拟机`
# 使用默认配置文件生成的虚拟机C:\Users\Administrator\VirtualBox VMs\single_default_1605188295162_7611
# 可使用“vagrant destroy default”命令销毁虚拟机
vagrant up
05 `查看虚拟机信息`
vagrant ssh-config
=======================================================================================
Host default
HostName 127.0.0.1
User vagrant
Port 2222
UserKnownHostsFile /dev/null
StrictHostKeyChecking no
PasswordAuthentication no
IdentityFile D:/VM/single/.vagrant/machines/default/virtualbox/private_key
IdentitiesOnly yes
LogLevel FATAL
========================================================================================
06 `设置允许密码登陆`
vagrant ssh 【进入虚拟机vagrant用户】
sudo -i 【切换root用户】
vi /etc/ssh/sshd_config 【修改sshd配置文件】
PasswordAuthentication yes 【允许密码登录,不能直接追加到文件末尾,会不生效】
systemctl restart sshd 【重启sshd】
passwd 【passwd修改密码为root】
ssh root@127.0.0.1 【使用root登陆】
ssh vagrant@127.0.0.1 【使用vagrant登陆】
4、Vagrantfile文件配置说明
========================================================================================
boxes = [
{
:name => "single",
:eth1 => "192.168.0.20",
:mem => "2048",
:cpu => "2",
:sshport => 22220
}
]
Vagrant.configure(2) do |config|
config.vm.box = "centos/7"
Encoding.default_external = 'UTF-8'
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 "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
========================================================================================
01 `指定使用的box`
config.vm.box = "centos/7"
02 `指定编码方式,否则可能会报错“GBK to UTF-8”`
Encoding.default_external = 'UTF-8'
03 `指定虚拟机的hostname`
config.vm.hostname = "single"
04 `指定虚拟机使用的网络`
# host-only: 仅主机可访问虚拟机,bridge: 局域网均可访问虚拟机
# 配置当前vm的host-only网络,ip、type需要二选一
config.vm.network "private_network"[, type: "dhcp"][, ip: "192.168.56.20"]
# 配置bridge桥接网络,ip、bridge为可选项【不指定bridge时,如果是混合网络的话启动时会提示选择】
config.vm.network "public_network"[, bridge: "Intel(R) Dual Band Wireless-AC 8260"][, ip: "192.168.0.20"]
05 `指定本地映射到虚拟机的端口`
# 默认2222,启动后可通过“telnet 127.0.0.1 2222”连接到虚拟机
# 修改此端口为22220,即使用“telnet 127.0.0.1 22220”连接到虚拟机
config.vm.network "forwarded_port", guest: 22, host: 2222, id: "ssh", disabled: "true"
config.vm.network "forwarded_port", guest: 22, host: 22220
06 `virtualbox其他参数设置`
config.vm.provider "virtualbox" do |vb|
v.customize ["modifyvm", :id, "--memory", "4096"] 【内存数,单位MB】
v.customize ["modifyvm", :id, "--cpus", "2"] 【cpu数】
v.customize ["modifyvm", :id, "--name", "single"] 【主机名】
end
5、使用Xshell连接虚拟机
01 `用127.0.0.1连2222端口`
telnet 127.0.0.1 2222
02 `用虚拟机的ip连22端口`
telnet 192.168.0.20 22
6、设置root账户登陆
sudo -i 【切换root用户】
vi /etc/ssh/sshd_config 【修改sshd配置文件】
PasswordAuthentication yes 【修改PasswordAuthentication】
passwd 【passwd修改密码为root】
systemctl restart sshd 【重启sshd】
ssh root@10.13.11.84 【使用root登陆】
7、生成自己的box
01 `退出虚拟机`
vagrant halt
02 `打包`
vagrant package --output single-centos7.box
03 `添加到box list`
vagrant box add single-centos7 single-centos7.box
04 `生成Vagrantfile文件`
vagrant init single-centos7
05 `启动虚拟机`
vagrant up
8、Vagrant常用命令
# 进入虚拟机
vagrant ssh
# 查看虚拟机状态
vagrant status
# 停止虚拟机
vagrant halt
# 删除虚拟机
vagrant destory
# 修改Vagrantfile文件之后重新加载生效
vagrant reload
# 生成Vagrantfile文件
vagrant init centos/7
# 查看虚拟机信息 Hostname Port IdentityFile
vagrant ssh-config
# 添加到box list
vagrant box add centos/7 D:\VM\virtualbox.box
# 查看box列表
vagrant box list
# 打包
vagrant package --output single-centos7.box
# 销毁虚拟机
vagrant destroy