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