window上搭建centos7环境
环境如下:
win11 64 位
VirtualBox-6.1.38-153438-Win.exe
Centos7.box
vagrant_2.3.0_windows_i686.msi
1.下载准备
- 下载 VM VirtualBox
https://www.virtualbox.org/
- 下载安装vagrant
https://www.vagrantup.com/
- 下载centos7.box镜像
https://app.vagrantup.com/centos/boxes/7
2.安装
- 安装目录如下:
D:\Program Files\Oracle\VirtualBox
D:\Program Files\Vagrant
- 验证vagrant 安装成功
C:\Users\zw>vagrant -v
Vagrant 2.3.0
3.配置centos
centos7 存放路径:
D:\dev\virtualbox_images\centos7.box
3.1创建目录
D:\dev\vagrant\centos7_env
3.2 初始化
在该目录下打开cmd,执行vagrant init centos/7
此时会在当前目录下生成Vagrantfile,同时指定使用的镜像为centos/7
D:\dev\vagrant\centos7_env>vagrant init centos/7
3.3 添加镜像
执行把镜像添加到vagrant管理的镜像中:
vagrant box add centos/7 D:\dev\virtualbox_images\centos7.box
D:\dev\vagrant\centos7_env>vagrant box list
There are no installed boxes! Use `vagrant box add` to add some.
D:\dev\vagrant\centos7_env>vagrant box add centos/7 D:\dev\virtualbox_images\centos7.box
==> box: Box file was not detected as metadata. Adding it directly...
==> box: Adding box 'centos/7' (v0) for provider:
box: Unpacking necessary files from: file:///D:/dev/virtualbox_images/centos7.box
box:
==> box: Successfully added box 'centos/7' (v0) for 'virtualbox'!
D:\dev\vagrant\centos7_env>vagrant box list
centos/7 (virtualbox, 0)
3.4 配置vagrantfile
主要添加如下
config.vm.network "public_network"
config.vm.provider "virtualbox" do |vb|
vb.memory = "3000"
vb.name= "tom-centos7"
vb.cpus= 2
end
4.启动
vagrant up
D:\dev\vagrant\centos7_env>vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'centos/7'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: tom-centos7
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
default: Adapter 1: nat
default: Adapter 2: bridged
==> default: Forwarding ports...
default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Running 'pre-boot' VM customizations...
==> 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: Configuring and enabling network interfaces...
==> default: Rsyncing folder: /cygdrive/d/dev/vagrant/centos7_env/ => /vagrant
5.配置链接
上面日志中的地址就是可以链接的地址
127.0.0.1:2222
01 使用centos7的默认账号连接
在centos文件夹下执行vagrant ssh-config
关注:Hostname Port IdentityFile
IP:127.0.0.1
port:2222
用户名:vagrant
密码:vagrant
文件:Identityfile指向的文件private-key
02 使用root账户登录
vagrant ssh 进入到虚拟机中
sudo -i
vi /etc/ssh/sshd_config
修改PasswordAuthentication yes
passwd修改密码,比如abc123
systemctl restart sshd
使用账号root,密码abc123进行登录
6 其他
6.1vagrant 常用命令
以后大家操作虚拟机,还是要在centos文件夹打开cmd窗口操作
vagrant halt 优雅关闭
vagrant up 正常启动
vagrant常用命令
(1)vagrant ssh
进入刚才创建的centos7中
(2)vagrant status
查看centos7的状态
(3)vagrant halt
停止/关闭centos7
(4)vagrant destroy
删除centos7
(5)vagrant status
查看当前vagrant创建的虚拟机
(6)Vagrantfile中也可以写脚本命令,使得centos7更加丰富
但是要注意,修改了Vagrantfile,要想使正常运行的centos7生效,必须使用vagrant reload
6.2 box的打包分发
01 退出虚拟机
vagrant halt
02 打包
vagrant package --output first-docker-centos7.box
03 得到first-docker-centos7.box
04 将first-docker-centos7.box添加到其他的vagrant环境中
vagrant box add first-docker-centos7 first-docker-centos7.box
05 得到Vagrantfile
vagrant init first-docker-centos7
06 根据Vagrantfile启动虚拟机
vagrant up [此时可以得到和之前一模一样的环境,但是网络要重新配置]
版权声明:本文为m0_56344192原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。