windows 10 安裝 centos/7 虛擬機

windows 10 安裝 centos/7

  1. 下載 vagrant 版本vagrant_2.2.6_x86_64.msi
  • 官網 下載: https://www.vagrantup.com/
    如果下載慢,可以從百度雲盤下載[https://pan.baidu.com/s/12jCAuyJTguqDdyS0xTsYaQ](https://pan.baidu.com/s/12jCAuyJTguqDdyS0xTsYaQ)

  • 下一步,下一步安裝

  • 輸入vagrant.exe 查看是否安裝成功

    Usage: vagrant [options] <command> [<args>]
    
        -v, --version                    Print the version and exit.
        -h, --help                       Print this help.
    
    Common commands:
         box             manages boxes: installation, removal, etc.
         cloud           manages everything related to Vagrant Cloud
         destroy         stops and deletes all traces of the vagrant machine
         global-status   outputs status Vagrant environments for this user
         halt            stops the vagrant machine
         help            shows the help for a subcommand
         init            initializes a new Vagrant environment by creating a Vagrantfile
         login
         package         packages a running vagrant environment into a box
         plugin          manages plugins: install, uninstall, update, etc.
         port            displays information about guest port mappings
         powershell      connects to machine via powershell remoting
         provision       provisions the vagrant machine
         push            deploys code in this environment to a configured destination
         rdp             connects to machine via RDP
         reload          restarts vagrant machine, loads new Vagrantfile configuration
         resume          resume a suspended vagrant machine
         snapshot        manages snapshots: saving, restoring, etc.
         ssh             connects to machine via SSH
         ssh-config      outputs OpenSSH valid configuration to connect to the machine
         status          outputs status of the vagrant machine
         suspend         suspends the machine
         up              starts and provisions the vagrant environment
         upload          upload to machine via communicator
         validate        validates the Vagrantfile
         version         prints current and latest Vagrant version
         winrm           executes commands on a machine via WinRM
         winrm-config    outputs WinRM configuration to connect to the machine
    
    For help on any individual command run `vagrant COMMAND -h`
    
    Additional subcommands are available, but are either more advanced
    or not commonly used. To see all subcommands, run the command
    `vagrant list-commands`.
    
  1. 下載 Oracle VM VirtualBox 版本 VirtualBox-6.0.14-133895-Win.exe

  2. 下載 虛擬機

  3. 通過vagrant 導入下載的虛擬機

    • 找一個文件夾下(這裏我是放在D:\IDE\VM\centos7)打開powerShell軟件 執行vagrant.exe init centos/7 ,當前目錄下會生成一個Vagrantfile 文件

    • 在當前目錄(D:\IDE\VM\centos7)下繼續執行vagrant.exe box add centos/7 D:\IDE\VM\virtualbox.box 命令 ,把百度雲盤下載的虛擬機加載到VirtualBox中,此時當前目錄會出現.vagrant 文件夾

    • 在當前目錄(D:\IDE\VM\centos7)下繼續執行vagrant.exe up 啓動虛擬機

      PS D:\IDE\VM\centos7> vagrant.exe up
      Bringing machine 'default' up with 'virtualbox' provider...
      ==> default: Clearing any previously set forwarded ports...
      ==> default: Clearing any previously set network interfaces...
      ==> default: Preparing network interfaces based on configuration...
          default: Adapter 1: nat
      ==> default: Forwarding ports...
          default: 22 (guest) => 2222 (host) (adapter 1)
      ==> default: Booting VM...
      ==> default: Waiting for machine to boot. This may take a few minutes...
          default: SSH address: 127.0.0.1:2222
          default: SSH username: vagrant
          default: SSH auth method: private key
          default:
          default: Vagrant insecure key detected. Vagrant will automatically replace
          default: this with a newly generated keypair for better security.
          default:
          default: Inserting generated public key within guest...
          default: Removing insecure key from the guest if it's present...
          default: Key inserted! Disconnecting and reconnecting using new SSH key...
      ==> default: Machine booted and ready!
      ==> default: Checking for guest additions in VM...
          default: No guest additions were detected on the base box for this VM! Guest
          default: additions are required for forwarded ports, shared folders, host only
          default: networking, and more. If SSH fails on this machine, please install
          default: the guest additions and repackage the box to continue.
          default:
          default: This is not an error message; everything may continue to work properly,
          default: in which case you may ignore this message.
      ==> default: Rsyncing folder: /cygdrive/d/IDE/VM/centos7/ => /vagrant
      
  4. 安裝結束後,打開Oracle VM VirtualBox程序即可看到安裝好並且已經啓動好的虛擬機了

  5. 後續使用

    1. 進入到虛擬機的路徑(D:\IDE\VM\centos7)下打開powerShell窗口
    2. 使用下面的命令
      vagrant halt 優雅關閉
      vagrant up 正常啓動
  6. 使用xshell 連接到虛擬機

    • 使用centos7 默認的賬號

      • 在centos文件夾下執行vagrant ssh-config 可以查看到連接信息

        Host default
          HostName 127.0.0.1
          User vagrant
          Port 2222
          UserKnownHostsFile /dev/null
          StrictHostKeyChecking no
          PasswordAuthentication no
          IdentityFile D:/IDE/VM/centos7/.vagrant/machines/default/virtualbox/private_key
          IdentitiesOnly yes
          LogLevel FATAL
        
    • 使用root賬號登錄

      • vagrant ssh 進入到虛擬機中
      • 執行命令 sudo -i
      • 執行命令 vi /etc/ssh/sshd_config
      • 修改 PasswordAuthentication yes
      • 修改root密碼,passwd root
      • 重啓ssh systemctl restart sshd
      • 使用賬號root,密碼root進行登錄
  7. box的打包

    • 退出虛擬機 vagrant halt
    • 打包 vagrant package --output first-docker-centos7.box
    • 得到 first-docker-centos7.box
    • 將first-docker-centos7.box添加到其他的vagrant環境中 vagrant box add first-docker-centos7 first-docker-centos7.box
    • 得到Vagrantfile
      vagrant init first-docker-centos7
    • 根據Vagrantfile啓動虛擬機
      vagrant up [此時可以得到和之前一模一樣的環境,但是網絡要重新配置]

1. 如果之前開啓過Hyper-V ,在vagrant.exe up 失敗的話

  • 程序與功能->左側啓用或關閉windows功能 中 把Hyper-V 禁用掉
  • 以管理員權限打開cmd 執行命令bcdedit /set hypervisorlaunchtype off (這一步一定要做的)
  • 重啓電腦

2.提供 vagrant 幾個常用命令

  • vagrant ssh
    進入剛纔創建的centos7中
  • vagrant status
    查看centos7的狀態
  • vagrant halt
    停止/關閉centos7
  • vagrant destroy
    刪除centos7
  • vagrant status
    查看當前vagrant創建的虛擬機

3. 可以修改Vagrantfile文件

Vagrantfile中也可以寫腳本命令,使得centos7更加豐富
但是要注意,修改了Vagrantfile,要想使正常運行的centos7生效,**必須使用vagrant reload **

4. 通過 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://vagrantcloud.com/search.
config.vm.box = "centos/7"
# 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: 8080
# 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 "../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
config.vm.provider "virtualbox" do |vb|
vb.memory = "4000"  ##內存大小
vb.name= "jack-centos7"  ## 名稱
vb.cpus= 2  ##cpu核數
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
# 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: <<-SHELL
# apt-get update
# apt-get install -y apache2
# SHELL
end
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章