软件安装
- VirtualBox虚拟机下载地址:https://www.virtualbox.org/wiki/Downloads
- Vagrant下载地址:https://www.vagrantup.com/downloads.html
- 图形界面操作步骤可参考:http://drupalchina.cn/book/export/html/6389
官方下载和添加 Box
- 添加box指令:
vagrant box add [自定义名称] [box镜像路径]
- 官方box镜像地址: https://app.vagrantup.com/boxes/search?order=desc&page=1&sort=downloads
- 移除box指令:
vagrant box remove box名称
手动下载Box并添加
- 为什么要手动下载?某些时候网络很慢又急需的时候
- 这里提供一个centos7的镜像百度网盘分享,分享码:
qcbq
- 下图为本地Box添加演示(由于我并未下载该文件故有 could not be found
错误提示
)
初始化配置与Box使用
首先强烈建议修改虚拟机镜像安装地址(因为window上、他默认放在C盘。当然后期再改也没问题,如果你不嫌麻烦的话)
-
为什么要改这个地址?请看下图
-
还需要安装一个
vagrant
插件。看下图:
- 使用
vagrant init [box-name]
生成Vagrantfile
文件(box-name
为box名称、默认为base
) - 使用
vagrant up
启动 一般在Vagrantfile
文件同级生成.vagrant
的配置文件夹(如果是首次启动通常比较慢,会生成虚拟机镜像,镜像位置以我们上一步配置的路径地址为准)
-
Vagrantfile
文件常用设置如下:
Vagrant.configure("2") do |config|
config.vm.box = "centos/7" # 使用的Box名称
config.vm.hostname = "centos7" # 自定义的名称
# 登录用户名(默认有vagrant这个用户、在未设置之前root可能登录不了)
config.ssh.username = 'vagrant'
config.ssh.password = "vagrant" # 默认的登录密码 (root用户的默认密码也是这个密码)
# 是否使用秘钥、公钥登录(默认为true,如果设为true那么上面的账号密码是无效的,建议设为true)
config.ssh.insert_key = false
# 以下是需要映射的端口 guest:虚拟机端口 host:本机端口
config.vm.network "forwarded_port", guest: 80, host: 80
config.vm.network "forwarded_port", guest: 443, host: 443
config.vm.network "forwarded_port", guest: 3306, host: 3306
# 为虚拟机分配内网IP地址。 SSH可以直接通过192.168.1.10连接
config.vm.network "public_network", ip:"192.168.1.10"
# 需要共享的目录(即我们可在本机修改"D:/WWW"的文件,而在虚拟机环境中运行"/wwwroot"的代码)
config.vm.synced_folder "D:/WWW", "/wwwroot"
- 修改配置文件需要重启。指令:
vagrant reload
。启动成功后可通过快捷指令连接:vagrant ssh
- 注:如果在重启或者启动过程中提示尝试登录失败一般为公钥秘钥对应不上。可通过类似这样的
ssh [email protected]
ssh命令直接登录使用密码登录即可。所有账号默认密码均为:vagrant
实现root账号的免密登录
-
查看本机是否已生成公钥、秘钥。如果没有则按下图第4个命令生成(如果没有特别需要可一路回车即可)
-
查看虚拟机是否有该文件(如果没有则创建)
- 将本机的公钥
id_rsa.pub
内容复制到虚拟机的authorized_keys
中(如果需要多台免密登录authorized_keys
里的公钥是可以叠加的) - 设置权限
# 设置.ssh目录权限
$ chmod 700 -R .ssh
# 设置authorized_keys权限
$ chmod 600 authorized_keys
- 允许root用户远程登录设置
# 虚拟机编辑ssh配置文件(编辑后需要重启sshd服务,命令:```systemctl reload sshd``` 或 ```service sshd reload```)
vi /etc/ssh/ssh_config
通常的配置:
# 允许使用密码登录
PasswordAuthentication yes
# 允许root认证登录
PermitRootLogin yes
# 允许密钥认证
RSAAuthentication yes
PubkeyAuthentication yes
# 默认公钥存放的位置
AuthorizedKeysFile .ssh/authorized_keys
- 如果找不到以上某些配置项也不要慌。其实你已经开启了,具体参考CentOS7.4踩坑 查看版本命令:
cat /etc/redhat-release
- 这个时候本机就可以通过
ssh
命令远程登录。如果不行请通过cat ~/.ssh/authorized_keys
再次确认你的公钥修改已经正确!
关于 vagrant ssh
无法免密登录问题
- 查看
Vagrantfile
文件配置项config.ssh.insert_key
是否为true
- 通过如下指令查看
IdentityFile
指向的文件是否存在,并且文件内容是否为存贮在虚拟机中authorized_keys
文件中的公钥对应的私钥
# vagrant 指令
vagrant ssh-config
# 响应内容如下
Host default
HostName 127.0.0.1
User root
Port 2222
UserKnownHostsFile /dev/null
StrictHostKeyChecking no
PasswordAuthentication no
# 检查该键指向的文件
IdentityFile E:/Vagrant/centos7/.vagrant/machines/default/virtualbox/private_key
IdentitiesOnly yes
LogLevel FATAL
关于vagrant镜像无法访问问题(将异常的配置文件后缀.temp
去掉即可)
- 发生的原因:通常为
vagrant
启动过程被冲断产生 - 正常的
vagrant
关机状态和镜像文件如下:
# vagrant 指令
vagrant status
# 响应数据
Current machine states:
default poweroff (virtualbox)
The VM is powered off. To restart the VM, simply run `vagrant up`
- 无法访问状态和镜像文件如下:
# vagrant 指令
vagrant status
# 响应数据
Current machine states:
default inaccessible (virtualbox)
关联已存在的镜像问题
- 发生原因:当我们删除
.vagrant
文件夹之后产生 - 如下空镜像状态信息(注:首次启动即为该状态):
# vagrant 指令
vagrant status
# 响应数据
Current machine states:
default not created (virtualbox)
The environment has not yet been created. Run `vagrant up` to
create the environment. If a machine is not created, only the
default provider will be shown. So if a provider is not listed,
then the machine is not created for that environment.
-
如果该状态是异常的其实你已经有了镜像只是他无法关联那么解决方法如下:
- 通过
vagrant up
生成以下文件。立刻通过进程管理器关闭ruby.exe
(大概)这个名的进程(注:如果不关闭,那么他将重新生成一个新的镜像)
- 我们打开如下文件的值替换掉上图中的
id
文件的内容。此时通过vagrant status
指令即可查看到正常的状态提示
- 通过
Vagranfile
# -*- 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.
config.vm.box = "centos"
# 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: 80
config.vm.network "forwarded_port", guest: 3306, host: 3306
config.vm.network "forwarded_port", guest: 9501, host: 9501
# 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 "C:/WWW", "/data/wwwroot", owner:"www", group: "www", :mount_options => ["dmode=777","fmode=777"]
# 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
#
# 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
# Ansible, Chef, Docker, Puppet and Salt 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
官方文档参考
看云中文参考