温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Linux开发环境搭建工具vagrant的安装是怎样的

发布时间:2022-01-27 10:27:15 来源:亿速云 阅读:126 作者:kk 栏目:开发技术

小编今天带大家了解Linux开发环境搭建工具vagrant的安装是怎样的,文中知识点介绍的非常详细。觉得有帮助的朋友可以跟着小编一起浏览文章的内容,希望能够帮助更多想解决这个问题的朋友找到问题的答案,下面跟着小编一起深入学习“Linux开发环境搭建工具vagrant的安装是怎样的”的知识吧。

Vagrant简介:

Vagrant 是一个基于 Ruby 的工具,用于创建和部署虚拟化开发环境。它使用 Oracle 的开源 VirtualBox 虚拟化系统,使用 Chef 创建自动化虚拟环境

准备工作


要使用Vagrant这个工具,首先需要安装一个虚拟机软件,常见的虚拟机软件包括了virtualbox和VMware,但后者是要收费的,不建议使用,优选选择virtualbox。 关于virtulbox和Vagrant的安装工具,这里就不具体介绍,注意在安装完之后,为了方便使用,需要将vagrant添加到“Path”环境变量中。

使用Vagrant


添加box

首先,安装Linux系统必不可少的是它的系统文件,而这个文件最好从网上下载到本地,可以从box镜像源进行选择下载。由于某种原因,最好使用科学上网的方法,不然会下载不动的。 在E盘下(根据自己情况)新建”vagrant”文件夹,在其目录下新建”box”文件夹,并将下载好的box文件”centos.box”放在”box”文件夹。

 cd vagrant    //进到vagrant文件夹
 vagrant box add --name centos box\centos.box
 //--name指定box的系统名称,在Vagrant的配置文件中将会使用到
 123

而使用命令

 vagrant box list
 1

可以查看已经添加的box,如下图 Linux开发环境搭建工具vagrant的安装是怎样的

配置Vagrantfile

在系统启动之前,需要进行配置,使用命令

 vagrant init centos
 1

对”centos”进行初始,在”vagrant”目录下会生成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://atlas.hashicorp.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.
   # config.vm.network "forwarded_port", guest: 80, host: 8080
 
   # 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
   #
   # View the documentation for the provider you are using for more
   # information on available options.
 
   # Define a Vagrant Push strategy for pushing to Atlas. Other push strategies
   # such as FTP and Heroku are also available. See the documentation at
   # https://docs.vagrantup.com/v2/push/atlas.html for more information.
   # config.push.define "atlas" do |push|
   #   push.app = "YOUR_ATLAS_USERNAME/YOUR_APPLICATION_NAME"
   # end
 
   # 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:可以看到对于常用的配置,这个文件都有列出,并进行了解释。这里对某些配置做简单介绍。 Vagrant.configure("2") do |config|     # 配置开始
 config.vm.box = "centos"               # 对系统'centos'的配置
 
 # 网络设置
 # config.vm.network "forwarded_port", guest: 80, host: 8080
 # 这里是进行端口转发,host: 8080 --> guest: 80
 config.vm.network "public_network"
 # 配置成公共网络,就像实际存在的物理电脑一致,IP地址将有网络中的路由器分配
 
 # 同步共享文件夹
 config.vm.synced_folder "./data", "/home/vagrant"
 # 这里配置两个文件夹,前一个是本地文件夹,后一个是centos系统下的文件夹
 # 因此需要在"vagrant"目录下新建"data"文件夹
 
 # 显示和内存设置
 config.vm.provider "virtualbox" do |vb| # 这里指定虚拟机软件virtualbox
     vb.gui = false                      # 是否显示gui
     vb.memory = "1024"                  # 虚拟机内存设置
 end
 end                                     # 配置结束
 1234567891011121314151617181920以上就是单个虚拟机的配置,使用命令 vagrant up
 1启动该虚拟机,如图 同样,Vagrantfile可以配置多虚拟机启动,其大致机构如下 Vagrant.configure("2") do |config|
 # 虚拟机centos
 config.vm.define :centos do |centos|
     centos.vm.box="centos"
     centos.vm.network "public_network"
 end
 # 虚拟机ubuntu
 config.vm.define :ubuntu do |ubuntu|
     ubuntu.vm.box="ubuntu"
 end
 end
 1234567891011这样的话在启动时,可以指定虚拟机启动,比如 vagrant up centos
 1可以只启动centos系统,不指定的话,会启动全部的虚拟机。 更多的配置选项,可以参考中文文档进行更多的配置学习。登录系统在成功系统之后,输入命令 vagrant ssh
 1后,输入密码,即可登录成功,如图 默认的系统用户名和密码 user: vagrant    
 passwd: vagrant    //管理员密码也是这个
 12使用命令 ip addr
 1获取虚拟机的IP地址,如图 可以看到虚拟在局域网中的地址为”192.168.199.215”,使用该地址,我们也可以从局域网中的其他电脑进行登录,如图关于文件共享就更简单了,之前我们配置了 config.vm.synced_folder "./data", "/home/vagrant"
 1在登录之后,进行一下测试 cd ~
 pwd
 touch guset.txt
 123如图在本地目录下,可以看到 因此,我们可以在Windows系统下选用自己喜欢的编辑器对文件进行编辑,而且能够快速地部署到Linux系统下。系统打包如果想要分享你配置好了的Linux系统,这时需要将该系统进行打包,打包前需要将虚拟机关闭,使用命令 vagrant halt
 1将虚拟机关机。使用命令 vagrant package
 1对系统进行打包,如图 在”vagrant”目录下多了一个.box文件,如图 以上就是良许教程网为各位朋友分享的Linux系统相关内容。想要了解更多Linux相关知识记得关注公众号“良许Linux”,或扫描下方二维码进行关注,更多干货等着你!

Linux有哪些版本

Linux的版本有:Deepin、UbuntuKylin、Manjaro、LinuxMint、Ubuntu等版本。其中Deepin是国内发展最好的Linux发行版之一;UbuntuKylin是基于Ubuntu的衍生发行版;Manjaro是基于Arch的Linux发行版;LinuxMint默认的Cinnamon桌面类似Windows XP简单易用;Ubuntu则是以桌面应用为主的Linux操作系统。

感谢大家的阅读,以上就是“Linux开发环境搭建工具vagrant的安装是怎样的”的全部内容了,学会的朋友赶紧操作起来吧。相信亿速云小编一定会给大家带来更优质的文章。谢谢大家对亿速云网站的支持!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI