使用Vagrant和Virtual Box創建一臺CentOS7虛擬機

版本說明

Virtual Box版本:5.2.10
Vagrant版本:2.0.4
電腦系統:windows10 64位
虛擬機系統:centos7
連接工具:Xshell

尋找虛擬機鏡像

在使用vagrant管理Virtual Box進行創建虛擬機的時候,鏡像文件就是各種各樣的box。這些box可以在vagrant的官網找到。網址如下所示:
https://app.vagrantup.com/boxes/search
找到的centos7的網址是:
https://app.vagrantup.com/centos/boxes/7
打開centos7的網址,可以知道這個box的名稱叫做:centos/7。不需要我們去尋找下載鏈接,只需要知道這個名字就足夠了。

下載box

這裏假裝你已經安裝了vagrant和Virtual box。打開命令行,運行如下命令:

vagrant box add centos/7

vagrant box add命令表示給vagrant新增一個box
centos/7表示box的名稱,是的,我們不需要指定下載鏈接,只需要給定一個名字,vagrant就會到官網的Vagrant Cloud中尋找並下載。

提示:下載速度很慢,這裏提供了一個下載鏈接:
https://pan.baidu.com/s/1DIis9g8JoyXMJvkkWkiL8A
這種將本地的box添加到Vagrant的命令如下:

vagrant box add -name 'centos/7' [box放置的位置]

初始化虛擬機

找一個文件夾,用於保存Vagrantfile文件,這個文件表示了Vagrant對虛擬機的一些配置文件。我這裏在G:\vagrant-workspace下創建一個文件夾。

cd G:\vagrant-workspace
mkdir test
cd test

使用如下命令進行初始話

vagrant init centos/7

vagrant init命令就是初始話命令
centos/7是指box的名稱
運行之後,有如下提示:

A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.

啓動虛擬機

初始話之後,就可以開始啓動虛擬機,運行如下命令:

vagrant up

運行之後,輸入如下:

Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'centos/7'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: test_default_1529231219933_10952
==> default: Fixed port collision for 22 => 2222. Now on port 2200.
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 22 (guest) => 2200 (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:2200
    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/g/test/ => /vagrant

這時候虛擬機已經啓動了。

連接虛擬機

使用Xshell工具連接虛擬機,這裏假設你已經安裝了虛擬機。可以使用ssh命令連接虛擬機。但是此時虛擬機是連接不上的,所以需要進入虛擬機去修改一些配置。

  1. 先給root修改一下密碼,默認root是沒有密碼的。

    sudo passwd root

    修改完了之後,切換到root用戶

    su root
  2. 通過Virtual Box進入虛擬機,默認賬號和密碼都是:vagrant
    這裏寫圖片描述

  3. 進入文件夾/etc/ssh,修改配置文件sshd_config

    cd /etc/ssh
    vi sshd_config

    將配置中的
    這裏寫圖片描述
    修改爲
    這裏寫圖片描述

  4. 重啓sshd.service服務

    systemctl restart sshd.service
  5. 本地使用Xshell連接虛擬機,運行命令如下:

    ssh 127.0.0.1 2200

    在彈出的窗口,輸入用戶名和密碼就進入了虛擬機。ssh表示連接的命令,127.0.0.1 2200可以從vagrant up的時候的輸出命令中找到。

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