vagrant box add [自定义名称] [box镜像路径]
下载添加Box演示
vagrant box remove box名称
Box移除指令演示
70tj
错误提示
)添加本地Box演示
centos7镜像基本信息
VirtualBox虚拟机
vagrant
插件。看下图: vagrant插件安装演示
vagrant init [box-name]
生成 Vagrantfile
文件(box-name
为box名称、默认为base
)vagrant up
启动 一般在Vagrantfile
文件同级生成.vagrant
的配置文件夹(如果是首次启动通常比较慢,会生成虚拟机镜像,镜像位置以我们上一步配置的路径地址为准) 初始化与启动演示
Vagrantfile
文件常用设置如下:Vagrant.configure("2") do |config|
config.vm.box = "centos/7" # 使用的Box名称
config.vm.hostname = "centos7" # 自定义的名称
# 登录用户名(默认有vagrant这个用户、在未设置之前root可能登录不了)
config.ssh.username = 'vagrant'
config.ssh.password = "vagrant" # 默认的登录密码 (root用户的默认密码也是这个密码)
# 是否使用秘钥、公钥登录(默认为true,如果设为true那么上面的账号密码是无效的,建议设为true)
config.ssh.insert_key = false
# 以下是需要映射的端口 guest:虚拟机端口 host:本机端口
config.vm.network "forwarded_port", guest: 80, host: 80
config.vm.network "forwarded_port", guest: 443, host: 443
config.vm.network "forwarded_port", guest: 3306, host: 3306
# 为虚拟机分配内网IP地址。 SSH可以直接通过192.168.1.10连接
config.vm.network "public_network", ip:"192.168.1.10"
# 需要共享的目录(即我们可在本机修改"D:/WWW"的文件,而在虚拟机环境中运行"/wwwroot"的代码)
config.vm.synced_folder "D:/WWW", "/wwwroot"
vagrant reload
。启动成功后可通过快捷指令连接:vagrant reload
ssh vagrant@192.168.1.10
ssh命令直接登录使用密码登录即可。所有账号默认密码均为:vagrant
演示
authorized_keys
id_rsa.pub
内容复制到虚拟机的authorized_keys
中(如果需要多台免密登录authorized_keys
里的公钥是可以叠加的)# 设置.ssh目录权限
$ chmod 700 -R .ssh
# 设置authorized_keys权限
$ chmod 600 authorized_keys
# 虚拟机编辑ssh配置文件(编辑后需要重启sshd服务,命令:```systemctl reload sshd``` 或 ```service sshd reload```)
vi /etc/ssh/ssh_config
通常的配置:
# 允许使用密码登录
PasswordAuthentication yes
# 允许root认证登录
PermitRootLogin yes
# 允许密钥认证
RSAAuthentication yes
PubkeyAuthentication yes
# 默认公钥存放的位置
AuthorizedKeysFile .ssh/authorized_keys
cat /etc/redhat-release
ssh
命令远程登录。如果不行请通过cat ~/.ssh/authorized_keys
再次确认你的公钥修改已经正确!vagrant ssh
无法免密登录问题Vagrantfile
文件配置项config.ssh.insert_key
是否为true
IdentityFile
指向的文件是否存在,并且文件内容是否为存贮在虚拟机中authorized_keys
文件中的公钥对应的私钥# vagrant 指令
vagrant ssh-config
# 响应内容如下
Host default
HostName 127.0.0.1
User root
Port 2222
UserKnownHostsFile /dev/null
StrictHostKeyChecking no
PasswordAuthentication no
# 检查该键指向的文件
IdentityFile E:/Vagrant/centos7/.vagrant/machines/default/virtualbox/private_key
IdentitiesOnly yes
LogLevel FATAL
.temp
去掉即可)vagrant
启动过程被冲断产生vagrant
关机状态和镜像文件如下:# vagrant 指令
vagrant status
# 响应数据
Current machine states:
default poweroff (virtualbox)
The VM is powered off. To restart the VM, simply run `vagrant up`
正常镜像
# vagrant 指令
vagrant status
# 响应数据
Current machine states:
default inaccessible (virtualbox)
无法访问状态镜像
.vagrant
文件夹之后产生# vagrant 指令
vagrant status
# 响应数据
Current machine states:
default not created (virtualbox)
The environment has not yet been created. Run `vagrant up` to
create the environment. If a machine is not created, only the
default provider will be shown. So if a provider is not listed,
then the machine is not created for that environment.
如果该状态是异常的其实你已经有了镜像只是他无法关联那么解决方法如下:
vagrant up
生成以下文件。立刻通过进程管理器关闭````ruby.exe```(大概)这个名的进程(注:如果不关闭,那么他将重新生成一个新的镜像) .vagrant目录下所需的文件
id
文件的内容。此时通过vagrant status
指令即可查看到正常的状态提示 uuid值
# -*- 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 = "centos"
# 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: 80
config.vm.network "forwarded_port", guest: 3306, host: 3306
config.vm.network "forwarded_port", guest: 9501, host: 9501
# 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.
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.
# 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 "C:/WWW", "/data/wwwroot", owner:"www", group: "www", :mount_options => ["dmode=777","fmode=777"]
# 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
#
# 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
Original url: Access
Created at: 2020-03-27 11:46:48
Category: default
Tags: none
未标明原创文章均为采集,版权归作者所有,转载无需和我联系,请注明原出处,南摩阿彌陀佛,知识,不只知道,要得到
最新评论