Windows下使用Vagrant
Vagrant是用來快速構建虛擬機器的工具,只需要短短數行命令,就可以幫我們構建一個基於Virtualbox/VMware的虛擬機器。在Linux系統下使用非常方便。本文介紹其在Windows下的使用方法。
1,準備工作
1.1,安裝Virtualbox和Vagrant,這裡不多說;
1.2,新建一個資料夾,例如”C:\vm1″,並進入到此目錄;
1.3,在Powershell中進入此目錄 ,方法如下(以下方法任選其一):
方法1(推薦):按一下Shift+F10後鬆開,然後按S
方法2:在目錄空白處滑鼠右鍵,在右鍵選單中選擇”在此處開啟 Powershell 視窗(s)”
方法3:按一下Windows+X後鬆開,然後按I,此時進入的Powershell不在”C:\vm1″目錄,我們可以手動執行”cd C:\vm1″命令進入之
2,在Powershell中初始化虛擬機器
在https://app.vagrantup.com找到你想要的虛擬機器,
比如https://app.vagrantup.com/ubuntu/boxes/xenial64
頁面上有個簡單的介紹, 只需要下面幾行命令
以下命令會在當前目錄下(本文是C:\vm1)生成一個Vagrantfile的檔案,記錄該虛擬機器的大致配置資訊
$ vagrant init ubuntu/xenial64#Ubuntu 16.04 or $ vagrant init debian/stretch64#Debian 9 or $ vagrant init centos/7#Centos 7
3,虛擬機器設定: 指定虛擬機器IP
這個非常重要,因為使用Vagrantfile安裝的虛擬機器預設guest與host網路互不相通
使用Notepad++等工具開啟這個Vagrantfile檔案,取消註釋如下行(即將虛擬機器的IP指定為192.168.33.10,如果系統中已經有192.168.33.10的虛擬機器在執行,請把這裡的IP改成其它)
# config.vm.network "private_network", ip: "192.168.33.10"
4,虛擬機器設定: 指定虛擬機器記憶體大小
根據情況取消註釋如下內容
# 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
5,開啟和執行虛擬機器
$ vagrant up#從vagrant網站下載對應的虛擬機器,並執行之 $ vagrant ssh#進入到虛擬機器的shell環境, 預設使用者名稱為vagrant,有sudo許可權
其它注意事項
1,進入虛擬機器以後, 如果發現多個虛擬機器不能互相ssh進來, 使用以下命令依次排查問題
$ telnet 192.168.33.10 22 $ egrep '(PasswordAuthentication|PermitRootLogin)' /etc/ssh/sshd_config #PermitRootLogin yes #PasswordAuthentication yes PasswordAuthentication yes#CentOS 7裡面可能預設是no, 需要改成yes以後再systemctl restart sshd
2,如果需要埠轉發功能,即物理機的公網IP某轉口轉向虛擬機器的22(執行vagrant的時候會有提示是哪個埠)。也可以在Vagrantfile檔案中新增一行如下內容, 使得外部可以SSH進來
config.ssh.forward_agent = true
3,一些其它的有用命令
PS C:\vm1> vagrant status Current machine states: defaultrunning (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`.
PS C:\vm1> vagrant help Usage: vagrant [options] <command> [<args>] -v, --versionPrint the version and exit. -h, --helpPrint this help. Common commands: boxmanages boxes: installation, removal, etc. destroystops and deletes all traces of the vagrant machine global-statusoutputs status Vagrant environments for this user haltstops the vagrant machine helpshows the help for a subcommand initinitializes a new Vagrant environment by creating a Vagrantfile loginlog in to HashiCorp's Vagrant Cloud packagepackages a running vagrant environment into a box pluginmanages plugins: install, uninstall, update, etc. portdisplays information about guest port mappings powershellconnects to machine via powershell remoting provisionprovisions the vagrant machine pushdeploys code in this environment to a configured destination rdpconnects to machine via RDP reloadrestarts vagrant machine, loads new Vagrantfile configuration resumeresume a suspended vagrant machine snapshotmanages snapshots: saving, restoring, etc. sshconnects to machine via SSH ssh-configoutputs OpenSSH valid configuration to connect to the machine statusoutputs status of the vagrant machine suspendsuspends the machine upstarts and provisions the vagrant environment validatevalidates the Vagrantfile versionprints current and latest Vagrant version 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`.
4,如果需要更嚴格的guest與host訪問許可權, 可以參考https://www.vagrantup.com/docs/networking/forwarded_ports.html