前言
windows环境下使用vagrant+VirtualBox搭建centos7虚拟机。
准备工作
VirtualBox 6.1.22
Vagrant 2.2.16
CentOS-7-x86_64-Vagrant-2004_01.VirtualBox.box
为了方便安装,把镜像重命名为CentOS7.box
镜像下载地址:http://mirrors.ustc.edu.cn/centos-cloud/centos/7/vagrant/x86_64/images/
开始安装
创建文件夹 E:\virtual_machains\vagrant
- 加载镜像,命名为vagrant
vagrant box add --name vagrant E:\virtual_machains\box\CentOS7.box
- 初始化,会在vagrant目录下生成一个Vagrantfile文件
vagrant init vagrant
-
修改配置文件 vagrantFile,这里配置了共享目录,需要现在本地创建一个data目录
# -*- mode: ruby -*- # vi: set ft=ruby : # All Vagrant configuration is done below. The "2" in Vagrant.configure # configures the configuration version (we support older styles for # backwards compatibility). Please don't change it unless you know what # you're doing. Vagrant.configure("2") do |config| # The most common configuration options are documented and commented below. # For a complete reference, please see the online documentation at # https://docs.vagrantup.com. # Every Vagrant development environment requires a box. You can search for # boxes at https://vagrantcloud.com/search. config.vm.box = "vagrant" # Disable automatic box update checking. If you disable this, then # boxes will only be checked for updates when the user runs # `vagrant box outdated`. This is not recommended. # config.vm.box_check_update = false # Create a forwarded port mapping which allows access to a specific port # within the machine from a port on the host machine. In the example below, # accessing "localhost:8080" will access port 80 on the guest machine. # NOTE: This will enable public access to the opened port # config.vm.network "forwarded_port", guest: 80, host: 8080 # Create a forwarded port mapping which allows access to a specific port # within the machine from a port on the host machine and only allow access # via 127.0.0.1 to disable public access # config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1" # Create a private network, which allows host-only access to the machine # using a specific IP. # 指定IP,设置为私有模式,只能本地链接 # config.vm.network "private_network", ip: "192.168.33.10" # Create a public network, which generally matched to bridged network. # Bridged networks make the machine appear as another physical device on # your network. # 设置网络为公共模式,ip由路由器分配 config.vm.network "public_network" # Share an additional folder to the guest VM. The first argument is # the path on the host to the actual folder. The second argument is # the path on the guest to mount the folder. And the optional third # argument is a set of non-required options. # config.vm.synced_folder "../data", "/vagrant_data" # 配置共享目录,前一个是本地文件夹,后一个是centos系统下的文件夹,这里的data目录 # 路径是 E:\virtual_machains\vagrant\data config.vm.synced_folder "./data", "/home/vagrant" # Provider-specific configuration so you can fine-tune various # backing providers for Vagrant. These expose provider-specific options. # Example for VirtualBox: # # 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 # 配置虚拟机的内存和cpu参数 config.vm.provider "virtualbox" do |vb| vb.memory = "2048" vb.gui = false vb.name= "vagrant-1" vb.cpus= 2 end # View the documentation for the provider you are using for more # information on available options. # Enable provisioning with a shell script. Additional provisioners such as # Ansible, Chef, Docker, Puppet and Salt are also available. Please see the # documentation for more information about their specific syntax and use. # config.vm.provision "shell", inline: <<-SHELL # apt-get update # apt-get install -y apache2 # SHELL end
-
启动服务器
vagrant up
启动后会有一个默认用户vagrant,密码也是vagrant。root用户的密码也是vagrant
这里遇到了一个共享目录无法挂载的问题,但是不影响服务器启动,后面再解决这个问题。
virtualBox可以看到服务器启动。
-
修改登录方式为使用密码
现在是使用私有key登录,修改为使用密码登录
vagrant ssh 进去之后,su root
-
修改 /etc/ssh/sshd_config 文件
修改PasswordAuthentication 为 yes
执行命令 service sshd restart 重启服务,或者直接重启服务器。 -
安装常用命令
安装网络工具 yum -y install net-tools 安装telnet yum -y install telnet-server yum -y install telnet 安装vim yum -y install vim*
-
关闭服务器
vagrant halt
共享目录无法挂载问题
看了网上的很多解决办法,这里做了如下操作:
-
安装vagrant的插件vagrant-vbguest,安装很慢,要等一会
vagrant plugin install vagrant-vbguest
-
更新服务器,登陆服务器,使用root用户执行
yum update
-
重启服务器
进入/home/vagrant创建一个文件,在本地E:\virtual_machains\vagrant\data下可以看到。在本地data目录创建文件,服务器上也可以看到。
yum update
-
重启服务器
进入/home/vagrant创建一个文件,在本地E:\virtual_machains\vagrant\data下可以看到。在本地data目录创建文件,服务器上也可以看到。