在 macOS 上安装 Vagrant
12/12/19 17:46
有时候,我们需要做一些软件安装测试,直接在线上服务器上弄是不太好的。所以需要先在一个模拟环境下测试好。
这里的模拟环境需要跟线上环境一样,例如系统版本,位数等等软件条件做到一样,硬件条件如无法满足也不是必要的,看需求决定。
这里推荐使用 Vagrant 安装虚拟机来模拟线上的系统环境。教程是基于 macOS 来进行的。
代理加速安装
打开终端
export http_proxy=http://127.0.0.1:1087;export https_proxy=http://127.0.0.1:1087;
这个跟ss的配置有关,需要自行检查
安装 Vagrant
需要提前安装 Homebrew
brew cask install virtualbox vagrant vagrant-manager
安装过程中,可能会出现:
==> installer: The install failed (安装器遇到了一个错误,导致安装失败。请联系软件制造商以获得帮助。) Error: Command failed to execute!
需要到 系统偏好设置 -> 安全性与隐私 -> 允许,然后重新安装即可
Vagrant 使用
命令清单:
检查你需要模拟的系统的信息
cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core)
getconf LONG_BIT
64
检查是否有虚拟机
vagrant box list
There are no installed boxes! Use `vagrant box add` to add some.
添加新的虚拟机 centos/7,安装过程中选择 3
vagrant box add centos/7
==> box: Loading metadata for box 'centos/7'
box: URL: https://vagrantcloud.com/centos/7
This box can work with multiple providers! The providers that it
can work with are listed below. Please review the list and choose
the provider you will be working with.
1) hyperv
2) libvirt
3) virtualbox
4) vmware_desktop
Enter your choice: 3
==> box: Adding box 'centos/7' (v1803.01) for provider: virtualbox
box: Downloading: https://vagrantcloud.com/centos/boxes/7/versions/1803.01/providers/virtualbox.box
==> box: Successfully added box 'centos/7' (v1803.01) for 'virtualbox'!
检查是否安装成功
vagrant box list
centos/7 (virtualbox, 1803.01)
初始配置
创建配置目录
cd ~
mkdir Vagrant && cd Vagrant
mkdir centos7 && cd centos7
初始化 centos/7
vagrant init centos/7
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.
ll
total 8
-rw-r--r-- 1 air staff 2.9K 4 12 10:55 Vagrantfile
启动 centos/7
vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'centos/7'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'centos/7' is up to date...
==> default: Setting the name of the VM: centos7_default_1523501887980_60805
==> 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: /Users/air/Vagrant/centos7/ => /vagrant
看到 Machine booted and ready! 就说明启动成功了
查看运行状态
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`.
这个时候我们就可以打开 VirtualBox,看到 centos/7 已经在运行
虚拟机使用
ssh 登陆系统
vagrant ssh
-bash: warning: setlocale: LC_ALL: cannot change locale (zh_CN.UTF-8)
-bash: warning: setlocale: LC_ALL: cannot change locale (zh_CN.UTF-8)
/bin/sh: warning: setlocale: LC_ALL: cannot change locale (zh_CN.UTF-8)
[vagrant@localhost ~]$
进入系统了,但出现了 warning,解决方法如下:
sudo echo "LC_ALL=zh_CN.UTF-8" >> /etc/environment
sudo echo "zh_CN.UTF-8 UTF-8" >> /etc/locale.gen
sudo echo "LANG=zh_CN.UTF-8" > /etc/locale.conf
sudo localedef -v -c -i zh_CN -f UTF-8 zh_CN.UTF-8
ctrl + d 退出ssh,然后重新登陆 vagrant ssh
vagrant ssh
Last login: Thu Apr 12 03:53:37 2018 from 10.0.2.2
[vagrant@localhost ~]$
cat /etc/redhat-release
CentOS Linux release 7.4.1708 (Core)
成功进入系统后,就可以随便折腾了
虚拟机关机
先ctrl + d 退出ssh,使用vagrant halt 关掉虚拟机
vagrant halt
==> default: Attempting graceful shutdown of VM...
检查是否关机成功
vagrant status
Current machine states:
default poweroff (virtualbox)
The VM is powered off. To restart the VM, simply run `vagrant up`
更新 vagrant
brew cask reinstall vagrant
# this might take a long time
vagrant plugin update
至此,vagrant 的教程就完毕了。
这里的模拟环境需要跟线上环境一样,例如系统版本,位数等等软件条件做到一样,硬件条件如无法满足也不是必要的,看需求决定。
这里推荐使用 Vagrant 安装虚拟机来模拟线上的系统环境。教程是基于 macOS 来进行的。
代理加速安装
打开终端
export http_proxy=http://127.0.0.1:1087;export https_proxy=http://127.0.0.1:1087;
这个跟ss的配置有关,需要自行检查
安装 Vagrant
需要提前安装 Homebrew
brew cask install virtualbox vagrant vagrant-manager
安装过程中,可能会出现:
==> installer: The install failed (安装器遇到了一个错误,导致安装失败。请联系软件制造商以获得帮助。) Error: Command failed to execute!
需要到 系统偏好设置 -> 安全性与隐私 -> 允许,然后重新安装即可
Vagrant 使用
命令清单:
命令 | 作用 |
vagrant up | 启动本地环境 |
vagrant halt | 关闭本地环境 |
vagrant suspend | 暂停本地环境 |
vagrant resume | 恢复本地环境 |
vagrant reload | 修改了 Vagrantfile 后,使之生效(相当于先 halt,再 up) |
vagrant ssh | 通过 ssh 登录本地环境所在虚拟机 |
vagrant destroy | 彻底移除本地环境 |
cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core)
getconf LONG_BIT
64
检查是否有虚拟机
vagrant box list
There are no installed boxes! Use `vagrant box add` to add some.
添加新的虚拟机 centos/7,安装过程中选择 3
vagrant box add centos/7
==> box: Loading metadata for box 'centos/7'
box: URL: https://vagrantcloud.com/centos/7
This box can work with multiple providers! The providers that it
can work with are listed below. Please review the list and choose
the provider you will be working with.
1) hyperv
2) libvirt
3) virtualbox
4) vmware_desktop
Enter your choice: 3
==> box: Adding box 'centos/7' (v1803.01) for provider: virtualbox
box: Downloading: https://vagrantcloud.com/centos/boxes/7/versions/1803.01/providers/virtualbox.box
==> box: Successfully added box 'centos/7' (v1803.01) for 'virtualbox'!
检查是否安装成功
vagrant box list
centos/7 (virtualbox, 1803.01)
初始配置
创建配置目录
cd ~
mkdir Vagrant && cd Vagrant
mkdir centos7 && cd centos7
初始化 centos/7
vagrant init centos/7
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.
ll
total 8
-rw-r--r-- 1 air staff 2.9K 4 12 10:55 Vagrantfile
启动 centos/7
vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'centos/7'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'centos/7' is up to date...
==> default: Setting the name of the VM: centos7_default_1523501887980_60805
==> 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: /Users/air/Vagrant/centos7/ => /vagrant
看到 Machine booted and ready! 就说明启动成功了
查看运行状态
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`.
这个时候我们就可以打开 VirtualBox,看到 centos/7 已经在运行
虚拟机使用
ssh 登陆系统
vagrant ssh
-bash: warning: setlocale: LC_ALL: cannot change locale (zh_CN.UTF-8)
-bash: warning: setlocale: LC_ALL: cannot change locale (zh_CN.UTF-8)
/bin/sh: warning: setlocale: LC_ALL: cannot change locale (zh_CN.UTF-8)
[vagrant@localhost ~]$
进入系统了,但出现了 warning,解决方法如下:
sudo echo "LC_ALL=zh_CN.UTF-8" >> /etc/environment
sudo echo "zh_CN.UTF-8 UTF-8" >> /etc/locale.gen
sudo echo "LANG=zh_CN.UTF-8" > /etc/locale.conf
sudo localedef -v -c -i zh_CN -f UTF-8 zh_CN.UTF-8
ctrl + d 退出ssh,然后重新登陆 vagrant ssh
vagrant ssh
Last login: Thu Apr 12 03:53:37 2018 from 10.0.2.2
[vagrant@localhost ~]$
cat /etc/redhat-release
CentOS Linux release 7.4.1708 (Core)
成功进入系统后,就可以随便折腾了
虚拟机关机
先ctrl + d 退出ssh,使用vagrant halt 关掉虚拟机
vagrant halt
==> default: Attempting graceful shutdown of VM...
检查是否关机成功
vagrant status
Current machine states:
default poweroff (virtualbox)
The VM is powered off. To restart the VM, simply run `vagrant up`
更新 vagrant
brew cask reinstall vagrant
# this might take a long time
vagrant plugin update
至此,vagrant 的教程就完毕了。