Comments (11)
Vagrant设置账号密码登录
在虚拟机 vagrantfile 的目录位置 打开控制台,输入vagrant ssh-config
❯ vagrant ssh-config
Host centos7
HostName 127.0.0.1
User vagrant
Port 2222
UserKnownHostsFile /dev/null
StrictHostKeyChecking no
PasswordAuthentication no
IdentityFile /Users/amz/develop/Vagrant/centos/.vagrant/machines/centos7/virtualbox/private_key
IdentitiesOnly yes
LogLevel FATAL
查看 HostName ,Port,IdentityFile 这三个位置。
有可能不能选择密码登录,默认是用密钥登录,浏览,文件,找到 刚刚的IdentityFile ,再输入密码vagrant 就可以使用vagrant 用户ssh登录。
设置root账号登录
vagrant登陆后,切换到root账号
$ sudo -i
设置root的密码
passwd
编辑 /etc/ssh/sshd_config 文件,(注意,vagrant用户下这个文件是只读的,可能什么也看不见)
修改 PermitRootLogin 改为yes ,并把前面的# 去掉
修改 PasswordAuthentication 改为yes ,并把前面的# 去掉
保存退出,重启sshd服务
$ systemctl restart sshd
from notes.
单主机且使用帐号密码登录配置
Vagrant.configure("2") do |config|
config.vm.define :ruby do |ruby|
ruby.vm.provider "virtualbox" do |v|
v.customize ["modifyvm", :id, "--name", "ruby", "--memory", "1024"]
end
config.ssh.insert_key = false
config.ssh.username = "vagrant"
config.ssh.password = "vagrant"
ruby.vm.box = "ubuntu/trusty64"
ruby.vm.hostname = "ruby"
ruby.vm.network :public_network, ip: "192.168.9.226"
end
end
from notes.
最新box安装方法
https://atlas.hashicorp.com/boxes/search
或者
Ubuntu box http://uec-images.ubuntu.com/vagrant/
CentOs box http://nrel.github.io/vagrant-boxes/ 下载
1. 首先选择你的目标box,这里以ubuntu/trusty64
为例
2. 初始化box
vagrant init ubuntu/trusty64
将会在目录下生成Vagrantfile
文件及.vagrant
目录,其中在文件中有这么一段内容,用来指定box的名称
config.vm.box = "ubuntu/trusty64"
3. 启动box
vagrant up --provider virtualbox
启动box,会去远程下载该box 其真是的box地址为https://atlas.hashicorp.com/ubuntu/boxes/trusty64/versions/20150521.0.0/providers/virtualbox.box
以下是添加CentOs 6.5 box的例子
vagrant init chef/centos-6.5
vagrant up --provider virtualbox
具体的输出信息
G:\os\centos> vagrant init chef/centos-6.5
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.
G:\os\centos>vagrant up --provider virtualbox
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Box 'chef/centos-6.5' could not be found. Attempting to find and in
stall...
default: Box Provider: virtualbox
default: Box Version: >= 0
==> default: Loading metadata for box 'chef/centos-6.5'
default: URL: https://atlas.hashicorp.com/chef/centos-6.5
==> default: Adding box 'chef/centos-6.5' (v1.0.0) for provider: virtualbox
default: Downloading: https://atlas.hashicorp.com/chef/boxes/centos-6.5/vers
ions/1.0.0/providers/virtualbox.box
default: Progress: 0% (Rate: 12624/s, Estimated time remaining: 3:06:13))
https://atlas.hashicorp.com/chef/boxes/centos-6.5/versions/1.0.0/providers/virtualbox.box
手动添加本地box
vagrant box add "ubuntu/trusty64" ../boxs/trusty-server-cloudimg-amd64-vagrant-disk1.box
屏幕输出:
G:\os\ubuntu>vagrant box add "ubuntu/trusty64" ../boxs/trusty-server-cloudimg-amd64-vagrant-disk1.box
==> box: Adding box 'ubuntu/trusty64' (v0) for provider:
box: Downloading: file://G:/os/boxs/trusty-server-cloudimg-amd64-vagrant-disk1.box
box: Progress: 100% (Rate: 39.4M/s, Estimated time remaining: --:--:--)
==> box: Successfully added box 'ubuntu/trusty64' (v0) for 'virtualbox'!
查看已添加的box列表
vagrant box list
初始化box
vagrant init ubuntu/trusty64
屏幕输出:
G:\os\ubuntu>vagrant init ubuntu/trusty64
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.
启动box
vagrant up
屏幕输出:
G:\os\ubuntu>vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'ubuntu/trusty64'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: ubuntu_default_1432530675194_86633
==> default: Clearing any previously set forwarded ports...
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
default: Adapter 1: nat
==> default: Forwarding ports...
default: 22 => 2222 (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
The guest machine entered an invalid state while waiting for it
to boot. Valid states are 'starting, running'. The machine is in the
'poweroff' state. Please verify everything is configured
properly and try again.
If the provider you're using has a GUI that comes with it,
it is often helpful to open that and watch the machine, since the
GUI often has more helpful error messages than Vagrant can retrieve.
For example, if you're using VirtualBox, run `vagrant up` while the
VirtualBox GUI is open.
from notes.
Vagrant.configure("2") do |config|
config.vm.define :u1 do |u1|
u1.vm.provider "virtualbox" do |v|
v.customize ["modifyvm", :id, "--name", "u1", "--memory", "1024"]
end
u1.vm.box = "ubuntu/trusty64"
u1.vm.hostname = "u1"
u1.vm.network :public_network, ip: "192.168.0.225"
end
config.vm.define :u2 do |u2|
u2.vm.provider "virtualbox" do |v|
v.customize ["modifyvm", :id, "--name", "u2", "--memory", "1024"]
end
u2.vm.box = "ubuntu/trusty64"
u2.vm.hostname = "u2"
u2.vm.network :public_network, ip: "192.168.0.226"
end
end
from notes.
Vagrant.configure("2") do |config|
config.vm.box = "CentOS/7"
config.vm.hostname = "c"
config.vm.network :public_network, ip: "192.168.99.138"
config.vm.provider "virtualbox" do |v|
v.memory = "2048"
v.name = "c"
end
end
若报"rsync" could not be found on your PATH. Make sure that rsync is properly installed on your system and available on the PATH.
则需要修改.vagrant.d\boxes
目录下的Vagrantfile
文件
config.vm.synced_folder ".", "/vagrant", type: "virtualbox"
from notes.
windows下centos7镜像共享目录报错解决方法
- rsync方式(默认方式)
修改C:\Users\Administrator\.vagrant.d\boxes\centos-VAGRANTSLASH-7\0\virtualbox\Vagrantfile
文件中的config.vm.synced_folder
为rsync
如下所示
config.vm.synced_folder ".", "/vagrant", type: "rsync"
下载 https://itefix.net/dl/cwRsync_5.5.0_x86_Free.zip 文件,解压后将rsync.exe
添加到Path
环境变量中
vagrant reload
- virtualbox方式共享目录
修改C:\Users\Administrator\.vagrant.d\boxes\centos-VAGRANTSLASH-7\0\virtualbox\Vagrantfile
文件中的config.vm.synced_folder
为virtualbox
如下所示
config.vm.synced_folder ".", "/vagrant", type: "virtualbox"
启动时若报如下错误
mount -t vboxsf -o uid=1000,gid=1000 vagrant /vagrant
The error output from the command was:
mount: unknown filesystem type 'vboxsf'
做如下操作即可
vagrant plugin install vagrant-vbguest
vagrant reload
vagrant vbguest --status
进入centos
,在系统内执行如下命令并重启centos
系统
yum update
yum install kernel-headers kernel-devel
重启后进入centos
系统内执行如下命令
sudo /opt/VBoxGuestAdditions-*/init/vboxadd setup
参考资料
hashicorp/vagrant#6769 (comment)
http://www.rehack.cn/techshare/devtools/3398.html
https://itefix.net/content/cwrsync-free-edition
from notes.
使用metadata.json方式添加本地box
使用该方式添加的box将带有版本信息
其metadata.json
文件内容如下
{
"name": "centos/7",
"description": "This box contains CentOS 7 64-bit.",
"versions":
[
{
"version": "1705.01",
"providers": [
{
"name": "virtualbox",
"url": "centos7.box"
}
]
}
]
}
注意:
- 使用该方式时建议在磁盘分区根目录下进行操作,不可有中文目录名称,否则报错。
metadata.json
文件最好与本地box在同一目录下
执行如下命令添加
vagrant box add metadata.json
Vagrantfile
最佳实践
使用时最好在Vagrantfile
文件中做如下配置
config.vm.box_version = '1705.01'
与metadata.json
文件中的version
保持一致
config.vm.box_check_update = false
禁用box升级检查
如下所示
Vagrant.configure("2") do |config|
config.vm.box = "centos/7"
config.vm.box_version = '1705.01'
config.vm.box_check_update = false
config.vm.network "public_network", ip: "192.168.31.216"
end
参考资料
https://www.vagrantup.com/docs/boxes/format.html
https://pigjian.com/article/homestead-problems
https://www.vagrantup.com/docs/multi-machine/
from notes.
Windows 10下正常运行Vagrant的版本如下所示
Vagrant 1.8.7
VirtualBox 5.1.10
切勿升级,切勿升级,切勿升级
from notes.
CentOS 7单机自定义Vagrantfile
Vagrant.configure("2") do |config|
config.vm.define "centos7" do |node|
node.vm.box = "centos/7"
node.vm.box_version = "1705.01"
node.vm.box_check_update = false
node.vm.provision "shell", inline: "echo This box contains CentOS 7 64-bit."
node.vm.hostname = "centos"
node.vm.network "public_network", ip: "192.168.31.216"
node.vm.provider "virtualbox" do |v|
v.name = "centos"
v.memory = 2048
v.cpus = 2
end
end
end
使用Vagrantfile批量创建多节点虚拟机
Vagrant.configure("2") do |config|
(1..3).each do |i|
config.vm.define "node#{i}" do |node|
node.vm.box = "centos/7"
node.vm.box_version = "1705.01"
node.vm.box_check_update = false
node.vm.provision "shell", inline: "echo This box contains CentOS 7 64-bit."
node.vm.hostname = "node#{i}"
node.vm.network "public_network", ip: "192.168.31.#{i}"
node.vm.provider "virtualbox" do |v|
v.name = "node#{i}"
v.memory = 2048
v.cpus = 2
end
end
end
end
参考资料
http://kiwenlau.com/2016/07/03/vagrant-vm-cluster/
from notes.
离线方式添加 Box
编写 metadata.json
{
"name": "centos/7",
"versions": [{
"version": "1710.01",
"providers": [{
"name": "virtualbox",
"url": "file:///virtualbox.box"
}]
}]
}
添加 Box
vagrant box add metadata.json
from notes.
Vagrant 管理
# 全局管理
vagrant global-status
vagrant global-status --prune
vagrant destroy <vm_id>
vagrant halt <vm_id1> <vm_id2>
vagrant reload <vm_id1> <vm_id2>..
vagrant up <vm_id1> <vm_id2>..
Vagrant 插件
- vagrant-hostmanager
实现多台虚拟机之间直接通过名称访问,原理为更改 host文件
# 安装并验证插件
vagrant plugin install vagrant-hostmanager
vagrant plugin list
# 在 Vagrantfile 中修改
config.hostmanager.enabled = true
config.hostmanager.manage_guest = true
config.hostmanager.manage_host = true
# 执行命令,更新虚拟机上的hosts,同时更新主机上的 hosts
vagrant hostmanager
- vagrant-vbguest
处理 VisualBox 中无法设置共享目录问题
vagrant plugin install vagrant-vbguest
vagrant vbguest --status
vagrant vbguest --do install node1
# 配置 vagrantfile
config.vbguest.auto_update=false
- vagrant-bindfs
非使用 visualBox 自带的共享目录,自定义使用文件系统 nfs,性能更高
vagrant plugin install vagrant-bindfs
# 。。。
node1. vm. synced_folder "./app","/mnt/app-data", type:"nfs
node1. bindfs. bind_folder "/mnt/app-data","/app", force_user:"root", force_group:"root",o:"nonempty"
# 代理设置插件
# 在Vagrantfile中的config部分添加代理配置, 全部网络都走主机代理
vagrant plugin install vagrant-proxyconf
vim Vagrantfile
Vagrant.configure("2") do |config|
if Vagrant.has_plugin?("vagrant-proxyconf")
config.proxy.http = "<http://192.168.0.2:3128/>"
config.proxy.https = "<http://192.168.0.2:3128/>"
config.proxy.no_proxy = "localhost,127.0.0.1,.example.com"
end
# ... other stuff
end
# 复用虚拟机环境插件安装
vagrant plugin list
vagrant plugin install vagrant-scp
vagrant scp
# 处理虚拟机安装进行目录文件映射权限问题
vagrant plugin install vagrant-vbguest
vagrant plugin list
网络配置
根据需要设置虚拟机网络 IP 地址固定,实现虚拟机中的软件根据名称访问,设置虚拟机可以访问外部的网络。
- 端口转发
将宿主机的端口与虚拟机的端口绑定,从而让外部通过端口可以访问虚拟机
若 guest_ip 和 host_ip 两项配置为空,则局域网下的所有设备都可以访问该虚拟机
Vagrant.configure(2") do |config|
config.vm.network
"forwarded_port"(必选) //端口转发标示
,guest(必选): # 虚拟端口
,host(必选): # 宿主机端口,值必须大于1024
,gust_ip(可选): # 虚拟机端口绑定虚拟机ip地址
,host_ip(可选): # 虚拟机端口绑定宿主机端口ip
,protocol(可选): # 指定通信协议,可以使用tcp/udp,默认tcp
,auto_correct(可选): # ture/fasle,开机是否自动检测端口冲突
end
# 实际配置
# 配置2个端口映射,把物理机的8080映射到虚拟机80,物理机的2100映射到虚拟机的22
# host_ip 在主机 IP 较为固定情况下配置使用
config.vm.network :"forwarded_port", guest: 80, host: 8060,host_ip: "10.2.11.203"
config.vm.network :"forwarded_port", guest: 22, host: 2100, host_ip: "10.2.11.203"
- 私有网络
虚拟机之间处在同一网段的地址可相互访问,主机可以访问虚拟机,无法通过虚拟机进行团队合作,不与宿主机的 IP 在同一个网段,防止冲突
配置 vagrant 里面的虚拟机的私有网段的时候,切记不能和企业(公司)内部的 DHCP 分配的 IP 地址在同一网段,否则会发生冲突
# 配置 Static IP
config.vm.network "private_network", ip: "192.168.50.10"
config.vm.network "private_network", ip: "192.168.55.20"
# 配置通过 DHCP 进行获取 IP,之后执行 `vagrant reload`
config.vm.network "private_network", type: "dhcp"
# 实际使用
Vagrant.configure("2") do |config|
config.vm.network "private_network", ip: "192.168.50.10",
auto_config: false
end
- 公有网络
与宿主机一样的网络配置, vagrant1.3+ 支持设置固定 IP,虚拟机 IP 与主机 IP 处在同一个网段时,实现局域网之间的互通,需要有路由器分配 IP.一般来说开发和测试使用较为封闭的网络模型是比较好的方式,通常不建议 vm 配置有 public_network 的网卡关联
配置虚拟机自动获取公司内部DHCP服务器分配的IP地址,在局域网任何一台电脑上,都可以ssh到虚拟机,或访问虚拟机上提供的服务
config.vm.network "public_network", ip: "192.168.1.120"
# 配置动态 IP
# 配置共有网络,使用主机上可以访问外网的接口(ipconfig)
# 配置默认网关
config.vm.network "public_network",bridge: "ens33"
config.vm.provision "shell",run: "always",inline: "route add default gw 10.2.11.1"
# 配置静态 IP
# auto_config:关闭自动配置
# ifconfig enp0s8 10.2.11.196 netmask 255.255.255.0 up: 配置静态ip(这里的ip不能和公司内部的地址冲突)
# route add default gw 10.2.11.1 指定网关(添加默认路由)
# bridge: 绑定接口(物理机哪个接口可以上网)
config.vm.network "public_network", auto_config: false ,bridge: "ens33"
config.vm.provision "shell",run: "always",inline: "ifconfig eth1 10.2.11.196 netmask 255.255.0.0 up"
config.vm.provision "shell",run: "alway",inline: "route add default gw 10.2.11.1"
config.vm.network "public_network", auto_config: false
config.vm.provision "shell",run: "always",inline: "ip addr add 172.17.10.51/21 dev eth1"
config.vm.provision "shell",run: "alway",inline: "ip route add 172.17.8.0/21 via 172.17.0.49"
共享文件
配置宿主机中的数据与虚拟机的数据映射
# src: 是物理机的目录,相对路径,(相对于项目目录(/vagrant/ubuntu))
# /srv/website: 虚拟机的目录,绝对路径,如果没有,会自动创建
config.vm.synced_folder "src/", "/srv/website"
end
config.vm.synced_folder ".", "/vagrant", disabled: true
config.vm.synced_folder "src/","/srv/website",owner: "root",group: "root"
from notes.
Related Issues (20)
- ADB命令大全
- Centos7防火墙配置
- spring boot logback config
- jvm gc log
- 分布式锁
- IDEA Iedis Plugin逆向
- Java线上服务排查
- 短网址Short URL实现原理
- 使用winsw将exe文件注册为windows系统服务
- Kafka集群高可用
- nginx流量镜像ngx_http_mirror_module
- apache-httpclient-get-with-body
- Spring MVC异步请求Controller
- java自动化部署插件
- SpringCloud
- Rancher安装k8s集群 HOT 2
- Python常见问题
- mac设置终端代理
- 接私活利器
- APM
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from notes.