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 reload
  • 注:如果在重啓或者啓動過程中提示嘗試登錄失敗一般爲公鑰祕鑰對應不上。可通過類似這樣的ssh [email protected]ssh命令直接登錄使用密碼登錄即可。所有賬號默認密碼均爲: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



作者:獨自邁向前方
鏈接:https://www.jianshu.com/p/2207f730e64e
來源:簡書
著作權歸作者所有。商業轉載請聯繫作者獲得授權,非商業轉載請註明出處。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章