![仅适用于 Vagrantfile 的公共静态 IP](https://linux22.com/image/79168/%E4%BB%85%E9%80%82%E7%94%A8%E4%BA%8E%20Vagrantfile%20%E7%9A%84%E5%85%AC%E5%85%B1%E9%9D%99%E6%80%81%20IP.png)
我修改了针叶林流浪者的 Vagrantfile基于VAGRANT DOCS/公共网络如下:
# -*- mode: ruby -*-
# vi: set ft=ruby :
require "./source.rb"
ROOT_PATH = File.dirname(__FILE__)
VAGRANTFILE_API_VERSION = "2"
def configure_extra(config)
end
def configure(config)
config.vm.box = "trusty64"
config.vm.box_url = "https://cloud-images.ubuntu.com/vagrant/trusty/current/trusty-server-cloudimg-amd64-vagrant-disk1.box"
# 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
# Host name
config.vm.hostname = "taiga"
# Create a forwarded port mapping which allows access to a specific port
# within the machine from a port on the host machine.
config.vm.network :forwarded_port, host: 8000, guest: 8000
config.vm.network :forwarded_port, host: 9001, guest: 9001
# 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", autoconfig: false
config.vm.provision "fix-no-tty", type: "shell" do |s|
s.privileged = false
s.inline = "sudo sed -i '/tty/!s/mesg n/tty -s \\&\\& mesg n/' /root/.profile"
end
# manual ip
config.vm.provision "shell",
run: "always",
inline: "ifconfig eth1 xxx.yyy.zzz.153 netmask 255.255.255.0 up"
# manual ipv6
config.vm.provision "shell",
run: "always",
inline: "ifconfig eth1 inet6 add fc00::17/7"
# default router
config.vm.provision "shell",
run: "always",
inline: "route add default gw xxx.yyy.zzz.132"
# default router ipv6
config.vm.provision "shell",
run: "always",
inline: "route -A inet6 add default gw fc00::1 eth1"
# delete default gw on eth0
config.vm.provision "shell",
run: "always",
inline: "eval `route -n | awk '{ if ($8 ==\"eth0\" && $2 != \"0.0.0.0\") print \"route del default gw \" $2; }'`"
# If true, then any SSH connections made will enable agent forwarding.
# Default value: false
config.ssh.forward_agent = true
# 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", "/home/vagrant/data"
# Provider-specific configuration so you can fine-tune various
# backing providers for Vagrant. These expose provider-specific options.
config.vm.provider "virtualbox" do |vb|
# Boot with headless mode
vb.gui = false
host = RbConfig::CONFIG['host_os']
# Giving a quarter of system memory to VM and access to all available cpu cores
if host =~ /darwin/
cpus = `sysctl -n hw.ncpu`.to_i
# sysctl returns Bytes, converting to MB...
mem = `sysctl -n hw.memsize`.to_i / 1024 / 1024 / 4
elsif host =~ /linux/
cpus = `nproc`.to_i
# meminfo returns KB, converting to MB...
mem = `grep 'MemTotal' /proc/meminfo | sed -e 's/MemTotal://' -e 's/ kB//'`.to_i / 1024 / 4
else
# hardcoding values for windows...
cpus = 2
mem = 2048
end
vb.customize ["modifyvm", :id, "--memory", mem]
vb.customize ["modifyvm", :id, "--cpus", cpus]
end
# Provisioning
config.vm.provision "shell" do |shell|
vagrant_shell_scripts_configure(
shell,
File.join(ROOT_PATH, "scripts"),
"provision.sh",
{}
)
end
end
# Look for a Vagrantfile.local to load
local_vagrantfile = "#{__FILE__}.local"
if File.exists?(local_vagrantfile)
eval File.read(local_vagrantfile)
end
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
configure config
configure_extra config
end
之后vagrant up
,我被要求“网络应该桥接到哪个接口?”。选择后1(对于 eth0),VirtualBox 虚拟机启动得很好,我可以通过 ssh 连接。该虚拟机的网络接口设置为:
eth0 Link encap:Ethernet HWaddr 08:00:27:70:2a:9d
inet addr:10.0.2.15 Bcast:10.0.2.255 Mask:255.255.255.0
inet6 addr: fe80::a00:27ff:fe70:2a9d/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:1462 errors:0 dropped:0 overruns:0 frame:0
TX packets:968 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:151879 (151.8 KB) TX bytes:122611 (122.6 KB)
eth1 Link encap:Ethernet HWaddr 08:00:27:2e:e8:7f
inet addr:xxx.yyy.zzz.153 Bcast:xxx.yyy.zzz.255 Mask:255.255.255.0
inet6 addr: fe80::a00:27ff:fe2e:e87f/64 Scope:Link
inet6 addr: fc00::17/7 Scope:Global
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:1335 errors:0 dropped:0 overruns:0 frame:0
TX packets:683 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:1078620 (1.0 MB) TX bytes:85690 (85.6 KB)
小更新
启动时,有几条消息
default: Warning: Connection timeout. Retrying...
default: Warning: Remote connection disconnect. Retrying...
default: Warning: Remote connection disconnect. Retrying...
default: Warning: Remote connection disconnect. Retrying...
...
不过,机器终于启动了。我可以从外部互联网访问针叶林。但,这两个接口有必要都有吗? 我可以不只为 eth0 提供静态公共 IP 吗?
答案1
该帖子位于https://github.com/mitchellh/vagrant/pull/1745#issuecomment-33159374很有用。下面这行就成功了!
config.vm.network "public_network", :bridge => "eth0", ip: "xxx.yyy.zzz.153", :netmask => "255.255.255.128", auto_config: false