# 當我們想要體驗體驗不同類型的Linux的使用技巧,節省個人開銷,可以在Windows系統上創建其他系統的虛擬機,可以創建虛擬機常用的應用有: VMware Station VirtualBox 此篇文章使用VirtualBox,結合Vagrant來實現Linux虛擬機的創建。使用這兩種應用結合的 ...
# 當我們想要體驗體驗不同類型的Linux的使用技巧,節省個人開銷,可以在Windows系統上創建其他系統的虛擬機,可以創建虛擬機常用的應用有:
- VMware Station
- VirtualBox
此篇文章使用VirtualBox,結合Vagrant來實現Linux虛擬機的創建。使用這兩種應用結合的方法,相對於使用VMware Station創建虛擬機來可以節省時間,加快效率,減少了等待系統安裝的漫長等待。
涉及的應用:Vagrant 下載路徑:Install | Vagrant | HashiCorp Developer
VirtualBox 下載路徑:Downloads – Oracle VM VirtualBox
一. Vagrant+VirtualBox安裝
下載Vagrant和VirtualBox完成後,安裝兩個應用。由於VirtualBox和Vagrant的安裝過程均是圖形化界面安裝,直接下一步,安裝路徑可以自行選擇(記住自己選的路徑),安裝順序沒有前後順序,安裝完成後可以檢查以下:
(1)在Windows系統下,使用快捷鍵“Win + R”打開運行視窗,在其中輸入“cmd”,在命令行視窗中輸入vagrant ,如下:
C:\Users\Administrator>vagrant Usage: vagrant [options] <command> [<args>] -h, --help Print this help. Common commands: autocomplete manages autocomplete installation on host 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 serve start Vagrant server 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`. --[no-]color Enable or disable color output --machine-readable Enable machine readable output -v, --version Display Vagrant version --debug Enable debug output --timestamp Enable timestamps on log output --debug-timestamp Enable debug output with timestamps --no-tty Enable non-interactive output C:\Users\Administrator>
出現如上的內容,表示Vagrant安裝成功!
(2)VirtualBox安裝完成後,會是一個普通的應用程式,直接點擊對應圖標打開,就是如下的界面(應用剛安裝,無以下的虛擬系統列表):
到此,VirtualBox安裝完成。
二. Linux虛擬機安裝
現在,使用現有的環境,創建一個版本為 Centos 7 的虛擬機,安裝開始。
(1)在Windows系統下,使用快捷鍵“Win + R”打開運行視窗,在其中輸入“cmd”,打開命令行視窗,輸入如下內容:
在windows的F盤下創建一個虛擬機總目錄:F:\vrbox
C:\Users\Administrator>F: F:\>mkdir vrbox
F:\>dir
驅動器 F 中的捲沒有標簽。
F:\ 的目錄
2023/08/01 22:21 <DIR> vrbox
0 個文件 0 位元組
1 個目錄 92,429,574,144 可用位元組
在F:\vrbox的路徑下,創建Centos 7 虛擬機的文件目錄:F:\vrbox\vcentos:
F:\vrbox>mkdir vcentos F:\vrbox>cd vcentos F:\vrbox\vcentos>
(2) 在F:\vrbox\vcentos目錄下打開命令行視窗,輸入如下命令:
下載創建虛擬機:
F:\vrbox\vcentos>vagrant init centos/7
執行結果,如下圖;
(3) 啟動創建的虛擬機
啟動虛擬機:vagrant up
(此處需要註意:當初次執行vagrant up命令時,由於需要從vagrant的 Discover Vagrant Boxes - Vagrant Cloud (vagrantup.com) 網站中下載Centos虛擬機使用的系統鏡像,需要稍等一會,這個等待時間相對於之間下載系統鏡像(.iso)文件來說可以忽略,下載完成後會自動完成安裝)
F:\vrbox\vcentos>vagrant up Bringing machine 'default' up with 'virtualbox' provider... ==> default: Checking if box 'centos/7' version '2004.01' is up to date... ==> 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: Adapter 2: hostonly ==> 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: 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: Configuring and enabling network interfaces... ==> default: Rsyncing folder: /cygdrive/f/vrbox/vcentos/ => /vagrant ==> default: Machine already provisioned. Run `vagrant provision` or use the `--provision` ==> default: flag to force provisioning. Provisioners marked to run always will still run.
(4)遠程連接虛擬機。
安裝完成後,執行命令,連接虛擬機。
F:\vrbox\vcentos>vagrant ssh Last login: Mon Jul 24 14:16:36 2023 from 10.0.2.2
使用以上命令可以直接連接虛擬機,不需要輸入密碼。虛擬機的預設用戶及密碼在“啟動虛擬機”出現的命令提示中顯示出來了:
==> 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
預設用戶:vagrant 密碼: vagrant
若需要使用root用戶,可以在命令行執行如下命令: [vagrant@localhost ~]$ id uid=1000(vagrant) gid=1000(vagrant) groups=1000(vagrant) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 [vagrant@localhost ~]$ sudo -i [root@localhost ~]# id uid=0(root) gid=0(root) groups=0(root) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 [root@localhost ~]# 退出root用戶,輸入如下命令: [root@localhost ~]# exit logout [vagrant@localhost ~]$ id uid=1000(vagrant) gid=1000(vagrant) groups=1000(vagrant) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 [vagrant@localhost ~]$
打開VirtualBox應用的虛擬機列表中,也可以看到此設備:
到此,一臺為Centos 7的虛擬機就已經安裝完成了。此虛擬機沒有圖形化界面,只有命令行界面。若需要圖形化界面,可以使用相關命令進行添加。
相關命令: ##關閉虛擬機,需使用root許可權 [vagrant@localhost ~]$ sudo -i [root@localhost ~]# poweroff Connection to 127.0.0.1 closed by remote host. Connection to 127.0.0.1 closed.