Vagrant开发环境搭建

软件安装

官方下载和添加 Box

下载添加Box演示
  • 移除box指令:vagrant box remove box名称
Box移除指令演示

手动下载Box并添加

添加本地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: 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 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 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.
  • 如果该状态是异常的其实你已经有了镜像只是他无法关联那么解决方法如下:

    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 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

官方文档参考
看云中文参考

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,088评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,715评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,361评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,099评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 60,987评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,063评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,486评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,175评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,440评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,518评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,305评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,190评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,550评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,880评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,152评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,451评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,637评论 2 335