0-安裝Vagrant和使用
Vagrant 是一款軟件,可以自動化虛擬機的安裝和配置流程。用來管理虛擬機,如 VirtualBox、VMware、AWS等,主要好處是可以提供一個可配置、可移植和復用的軟件環境,可以使用shell、chef、puppet等工具部署。所以vagrant不能單獨使用,如果你用它來管理自己的開發環境的話,必須在自己的電腦里安裝了虛擬機軟件,我使用的是 virtualbox。
Vagrant提供一個命令行工具 vagrant,通過這個命令行工具可以直接啟動一個虛擬機,當然你需要提前定義一個Vagrantfile文件,這有點類似 Dockerfile 之于 docker 了。
Vagrant 官網地址。
Vagrant 安裝
下載地址
下載安裝即可。
驗證是否安裝成功:
vincent@dell-Inspiron-7559 ~/virtual-os vagrant --help ? 1000 14:28:05
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 Clouddestroy stops and deletes all traces of the vagrant machineglobal-status outputs status Vagrant environments for this userhalt stops the vagrant machinehelp shows the help for a subcommandinit initializes a new Vagrant environment by creating a Vagrantfilelogin package packages a running vagrant environment into a boxplugin manages plugins: install, uninstall, update, etc.port displays information about guest port mappingspowershell connects to machine via powershell remotingprovision provisions the vagrant machinepush deploys code in this environment to a configured destinationrdp connects to machine via RDPreload restarts vagrant machine, loads new Vagrantfile configurationresume resume a suspended vagrant machinesnapshot manages snapshots: saving, restoring, etc.ssh connects to machine via SSHssh-config outputs OpenSSH valid configuration to connect to the machinestatus outputs status of the vagrant machinesuspend suspends the machineup starts and provisions the vagrant environmentupload upload to machine via communicatorvalidate validates the Vagrantfileversion prints current and latest Vagrant versionwinrm executes commands on a machine via WinRMwinrm-config outputs WinRM configuration to connect to the machineFor 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`.
安裝虛擬操作系統Centos7
1.創建centos7
文件夾,并進入此文件夾執行以下命令:
vagrant init centos/7
這一步相當于創建
centos/7
操作系統的描述信息,這些信息在Vagrantfile
文件內。
2.創建虛擬機,執行下面命令
vagrant up
此命令為下載 box base
,可以理解為 docker
的image
鏡像。
此過程需要很久,因為墻的原因。
下載完成后會自動安裝,只需要等待就好了。
同時可以在 VirtualBox 管理器中可以看到 已經正在安裝的虛擬機
3.ssh
登錄,使用下面命令
vagrant ssh
執行命令后將會進入centos/7
的系統命令行。
刪除虛擬系統
-
查看當前系統狀態
vagrant status
Current machine states:default running (virtualbox)The VM is running. To stop this VM, you can run `vagrant halt` to shut it down forcefully, or you can run `vagrant suspend` to simply suspend the virtual machine. In either case, to restart it again, simply run `vagrant up`.
-
如果是運行狀態先停止
vagrant halt
然后再次查看狀態是否關機
vagrant status
default poweroff (virtualbox)The VM is powered off. To restart the VM, simply run `vagrant up`
-
刪除系統
vagrant destroy
回答
y
即可確認刪除
查看 VirtualBox 管理中是否已經刪除
Vagrantfile 下載
vagrantfile
類似 docker 的 Dockerfile
,其實也是有很多人制作了現成的文件供我們使用。
大家可以到 Vagrant Cloud 網站搜索下載。
拷貝文件
- 安裝插件
vagrant plugin install vagrant-scp
- 查看虛擬機名稱
vagrant global-status
id name provider state directory
------------------------------------------------------------------------
bca430a default virtualbox running /home/vincent/virtual-os/centos7
- 從本地拷貝到虛擬機
vagrant scp /home/vincent/backend/go-dev/proxy-v default:~
開啟ssh登錄
首先進入虛擬機。
開啟密碼驗證
sudo nano /etc/ssh/sshd_config
找到passwordAuthentication
項,修改成yes
重啟ssh服務:
sudo service sshd restart
密碼設置:
sudo passwd vagrant
然后使用ssh即可連接。