Vagrant安装centos7

版本说明

Virtual Box版本:6.1.0
Vagrant版本:2.2.6
电脑系统:windows10 64位
虚拟机系统:centos7

寻找虚拟机镜像

在使用vagrant管理Virtual Box进行创建虚拟机的时候,镜像文件就是各种各样的box。这些box可以在vagrant的官网找到。网址如下所示:
https://app.vagrantup.com/boxes/search
找到的centos7的网址是:
https://app.vagrantup.com/centos/boxes/7
打开centos7的网址,可以知道这个box的名称叫做:centos/7。不需要我们去寻找下载链接,只需要知道这个名字就足够了。

下载box

这里假装你已经安装了vagrant和Virtual box。打开命令行,运行如下命令:

D:\Vagrant>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' (v1905.1) for provider: virtualbox
    box: Downloading: https://vagrantcloud.com/centos/boxes/7/versions/1905.1/providers/virtualbox.box
    box: Download redirected to host: cloud.centos.org
    box:
==> box: Successfully added box 'centos/7' (v1905.1) for 'virtualbox'!

D:\Vagrant>vagrant box list
centos/7 (virtualbox, 1905.1)

vagrant box add命令表示给vagrant新增一个box,centos/7表示box的名称

由于在线安装的速度比较慢,可以直接在Vagrant Cloud中下载,地址: http://cloud.centos.org/centos/7/vagrant/x86_64/images

# 本地磁盘安装
vagrant box add -name 'centos/7' [box放置的位置]

查看box中是否已经添加

D:\Vagrant>vagrant box list
centos/7 (virtualbox, 1905.1)

初始化虚拟机

找一个文件夹,用于保存Vagrantfile文件,这个文件表示了Vagrant对虚拟机的一些配置文件。我这里在F:\vagrant-workspace下创建一个文件夹。

cd F:\vagrant-workspace
mkdir centos-1
cd centos-1

使用如下命令进行初始话

F:\vagrant-workspace>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.

vagrant init命令就是初始化命令,centos/7是指box的名称

启动虚拟机

初始话之后,就可以开始启动虚拟机,运行如下命令:

F:\vagrant-workspace\centos7-1>vagrant up
No usable default provider could be found for your system.

Vagrant relies on interactions with 3rd party systems, known as
"providers", to provide Vagrant with resources to run development
environments. Examples are VirtualBox, VMware, Hyper-V.

The easiest solution to this message is to install VirtualBox, which
is available for free on all major platforms.

If you believe you already have a provider available, make sure it
is properly installed and configured. You can see more details about
why a particular provider isn't working by forcing usage with
`vagrant up --provider=PROVIDER`, which should give you a more specific
error message for that particular provider.

出现以上问题,是因为Vagrant2.2.6 和VirtualBox6.1.0的版本不兼容,解决方案请查看:https://blog.csdn.net/u014704612/article/details/103817864

处理以上问题后,重新运行

F:\vagrant-workspace\centos7-1>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' version '1905.1' is up to date...
==> default: Setting the name of the VM: centos7-1_default_1578020398186_89854
==> 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: /cygdrive/f/vagrant-workspace/centos7-1/ => /vagrant

SSH连接虚拟机

F:\vagrant-workspace\centos7-1>vagrant ssh
Last login: Fri Jan  3 06:12:50 2020
Last login: Fri Jan  3 06:12:50 2020

默认账号是:vagrant/vagrant , root账号的密码为:vagrant

注:window机器不支持 ssh 127.0.0.1 222 的命令,必须使用第三方客户端来进行连接,例如xmoba、putty、Xshell等.

ssh: 127.0.0.1
端口: 2222
用户名/密码: vagrant/vagrant(root/vagrant)

用Xshell连接时,如果弹出需要私钥,则查看私钥文件目录,并选择文件

F:\vagrant-workspace\centos7-2\.vagrant>vagrant ssh-config
Host default
  HostName 127.0.0.1
  User vagrant
  Port 2200
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile F:/vagrant-workspace/centos7-2/.vagrant/machines/default/virtualbox/private_key
  IdentitiesOnly yes
  LogLevel FATAL



# IdentityFile F:/vagrant-workspace/centos7-2/.vagrant/machines/default/virtualbox #私钥目录

查看所有的账号

cat /etc/passwd | grep -v /sbin/nologin | cut -d : -f 1

参考:https://blog.csdn.net/wang465745776/article/details/80720523

发布了15 篇原创文章 · 获赞 1 · 访问量 2704

猜你喜欢

转载自blog.csdn.net/u014704612/article/details/103820642
今日推荐