软件安装

  • 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演示

  • 移除box指令:vagrant box remove box名称

Box移除指令演示

手动下载Box并添加

  • 为什么要手动下载?某些时候网络很慢又急需的时候
  • 这里提供一个centos7的镜像百度网盘分享,分享码:70tj
  • 下图为本地Box添加演示(由于我并未下载该文件故有 could not be found 错误提示

添加本地Box演示

初始化配置与Box使用

  • 首先强烈建议修改虚拟机镜像安装地址(因为window上、他默认放在C盘。当然后期再改也没问题,如果你不嫌麻烦的话)

  • 为什么要改这个地址?请看下图

    centos7镜像基本信息

    VirtualBox虚拟机

  • 还需要安装一个vagrant插件。看下图:

    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: 80config.vm.network "forwarded_port", guest: 443, host: 443config.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 reload
  • 注:如果在重启或者启动过程中提示尝试登录失败一般为公钥秘钥对应不上。可通过类似这样的ssh vagrant@192.168.1.10ssh命令直接登录使用密码登录即可。所有账号默认密码均为:vagrant

实现root账号的免密登录

  • 查看本机是否已生成公钥、秘钥。如果没有则按下图第4个命令生成(如果没有特别需要可一路回车即可)

    演示

  • 查看虚拟机是否有该文件(如果没有则创建)

    authorized_keys

  • 将本机的公钥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无法免密登录问题

  1. 查看Vagrantfile文件配置项config.ssh.insert_key是否为true
  2. 通过如下指令查看IdentityFile指向的文件是否存在,并且文件内容是否为存贮在虚拟机中authorized_keys文件中的公钥对应的私钥
# vagrant 指令
vagrant ssh-config# 响应内容如下Host defaultHostName 127.0.0.1User rootPort 2222UserKnownHostsFile /dev/nullStrictHostKeyChecking noPasswordAuthentication no# 检查该键指向的文件IdentityFile E:/Vagrant/centos7/.vagrant/machines/default/virtualbox/private_keyIdentitiesOnly yesLogLevel 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.
  • 如果该状态是异常的其实你已经有了镜像只是他无法关联那么解决方法如下:

    1. 通过vagrant up生成以下文件。立刻通过进程管理器关闭````ruby.exe```(大概)这个名的进程(注:如果不关闭,那么他将重新生成一个新的镜像)

      .vagrant目录下所需的文件

    2. 我们打开如下文件的值替换掉上图中的id文件的内容。此时通过vagrant status指令即可查看到正常的状态提示

      uuid值

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 portconfig.vm.network "forwarded_port", guest: 80, host: 80config.vm.network "forwarded_port", guest: 3306, host: 3306config.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

作者:独自迈向前方
链接:https://www.jianshu.com/p/2207f730e64e
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

Vagrant开发环境搭建相关推荐

  1. MonkeyRunner在Windows下的Eclipse开发环境搭建步骤(兼解决网上Jython配置出错的问题)...

    网上有一篇shangdong_chu网友写的文章介绍如何在Eclipse上配置MonkeyRunner,做了挺好的一个描述,但经过我的试验在我的环境上碰到了Jython解析器出错的问题,且该文章缺少P ...

  2. 蓝鲸智云统一开发环境搭建指南

    腾讯官方的<蓝鲸智云统一开发环境搭建指南> 蓝鲸应用统一开发环境指南 一:背景 蓝鲸应用开发需要依赖django,celery,mysql-client等第三方库,而像celery又依赖其 ...

  3. Docker java项目发布/开发环境搭建

    Docker java项目发布/开发环境搭建 线上部署项目/开发环境搭建命令大全 关闭windows10 指定应用 安装Docker 安装 docker-compose 关于docker容器导致硬盘耗 ...

  4. 我的全栈之路-Python基础之Python概述与开发环境搭建

    我的全栈之路-Python基础之Python概述与开发环境搭建 我的全栈之路 1.1 信息技术发展趋势 1.2 浅谈计算机系统架构 1.2.1 计算机系统架构概述 1.2.2 计算机硬件系统 1.2. ...

  5. Android环境搭建和Android HelloWorld—Android开发环境搭建

    Android_Android开发环境搭建-搭建Android的开发环境 1.我考虑先下载JDK7.0,在JDK的安装中,考虑一般SDK都是向上兼容的,于是选择了最高的版本7.0 这里是我总结的详细的 ...

  6. qt工程在linux系统里颜色显示错误_【飞凌嵌入式RK3399开发板试用体验】+QT开发环境搭建测试(二)...

    作者:飞扬的青春 在拿到开发板之后,已经体验了Android操作系统,接下来就是体验Linux下的开发,本次以QT的一个小案例来测试下. 首先是自己先搭建了一个Ubuntu18.04的虚拟机,使用真机 ...

  7. Go:分布式学习利器(1) -- 开发环境搭建 + 运行第一个go程序

    文章目录 为什么要学习 go 开发环境搭建 -- MAC 运行第一个go程序 go 函数的返回值设置 go 函数的命令行参数 为什么要学习 go 在如下几个应用场景的需求下产生了go: 超大规模分布式 ...

  8. 智能合约开发环境搭建及Hello World合约

    如果你对于以太坊智能合约开发还没有概念(本文会假设你已经知道这些概念),建议先阅读入门篇. 就先学习任何编程语言一样,入门的第一个程序都是Hello World.今天我们来一步一步从搭建以太坊智能合约 ...

  9. iPhone开发环境搭建全过程 iPhone手机开发内容,中文手册

    http://3g.edu.csdn.net/kecheng/iphone.html  iPhone手机开发内容 http://www.docin.com/p-34874880.html# iPhon ...

最新文章

  1. MyBatis学习总结(10)——批量操作
  2. tomcat 目录配置 appBase和docBase 简介
  3. zookeeper安装包_构建高可用ZooKeeper集群
  4. http://www.ybtsoft.com/
  5. Raw264.7培养经验分享
  6. [转]Struts2.3.16.1+Hibernate4.3.4+Spring4.0.2 框架整合
  7. 【bzoj1758】[Wc2010]重建计划
  8. 【渝粤教育】国家开放大学2018年秋季 0633-21T化工CAD 参考试题
  9. widevine level1测试视频的生成方法
  10. GEM TSU Interface Details and IEEE 1588 Support
  11. 计算机网络常用操作系统,计算机目前常用的操作系统有哪些?
  12. 使用广义α方法(the generalized-α method)求解时变动力学问题
  13. 美国华盛顿州通过 ArcBlock 支持的区块链法案
  14. 仿生象鼻机械臂的创新设计与应用研究
  15. python之迭代器和生成器全解--包含实现原理及应用场景
  16. 全国书画艺术之乡-----通渭
  17. android listview 图片闪烁,listView异步加载图片导致图片错位、闪烁、重复的问题的解决...
  18. 云上PDF怎么删除页眉页脚_Word文档如何删除页眉页脚横线?如何取消文档页眉横线?...
  19. 牛客网第二场I--car(简单图论)
  20. 粒子滤波 PF(Particle filter)算法

热门文章

  1. 论文被 scoop(抢先发表)是怎样一种体验?
  2. 4.2 Python图像的图像恢复-组合滤波器
  3. java中的null的基本含义
  4. 智能制造转型:企业应如何避免陷入转型盲区,减少资金浪费
  5. php object array in,PHP的ArrayObject是否具有in_array等价物?
  6. linux的DNS企业级域名解析
  7. C语言编译键盘钢琴,键盘小钢琴[C语言][自持]
  8. APP客户端性能专项测试手册
  9. C++最佳实践之编程建议
  10. Eclipse 代码样式模板的设置