我可以访问互联网和另一台 Virtualbox Vagrant 机器(它们互相 ping 对方),但我无法访问我的 LAN 和网关的同一范围 IP。我尝试了使用私有网络和网桥的许多设置,但我不知道如何才能在同一 LAN 上正常工作。
IP:192.168.15.1网关-IP:192.168.15.103主机
验证:
[root@centos-vm vagrant]# ifconfig | grep 192.
inet 192.168.15.100 netmask 255.255.255.0 broadcast 192.168.15.255
[root@centos-vm vagrant]# ping 192.168.15.102
PING 192.168.15.102 (192.168.15.102) 56(84) bytes of data.
64 bytes from 192.168.15.102: icmp_seq=1 ttl=64 time=0.250 ms
\\\
--- 192.168.15.102 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2033ms
rtt min/avg/max/mdev = 0.250/0.269/0.300/0.022 ms
[root@centos-vm vagrant]# ping 192.168.15.1
PING 192.168.15.1 (192.168.15.1) 56(84) bytes of data.
^C
--- 192.168.15.1 ping statistics ---
2 packets transmitted, 0 received, 100% packet loss, time 1000ms
[root@centos-vm vagrant]# ping 192.168.15.103
PING 192.168.15.103 (192.168.15.103) 56(84) bytes of data.
--- 192.168.15.103 ping statistics ---
3 packets transmitted, 0 received, 100% packet loss, time 2000ms
root@debian-vm:/home/vagrant# ping 192.168.15.100
PING 192.168.15.100 (192.168.15.100) 56(84) bytes of data.
64 bytes from 192.168.15.100: icmp_seq=1 ttl=64 time=0.382 ms
Vagranfile:
Vagrant.configure("2") do |config|
config.vm.provider "virtualbox" do |vb|
vb.gui = false
vb.memory = "4096"
vb.cpus = "4"
end
config.vm.define "vm1" do |vm1|
vm1.vm.box = "centos/7"
vm1.vm.hostname = "centos-vm"
vm1.vm.network "public_network", ip: "192.168.15.100"
vm1.vm.provision "shell", run: "always", inline: <<-SHELL
route add default gw 192.168.15.1
#removed
sudo yum update
SHELL
end
config.vm.define "vm2" do |vm2|
vm2.vm.box = "debian/buster64"
vm2.vm.hostname = "debian-vm"
vm2.vm.network "public_network", ip: "192.168.15.102"
vm2.vm.provision "shell", run: "always", inline: <<-SHELL
route add default gw 192.168.15.1
##removed
sudo apt update
SHELL
end
end
以前也尝试过这种方法,但是效果并不好,只是得到同样的结果。
Vagrant.configure("2") do |config|
config.vm.provider "virtualbox" do |vb|
vb.gui = false
vb.memory = "4096"
vb.cpus = "4"
end
config.vm.define "vm1" do |vm1|
vm1.vm.box = "centos/7"
vm1.vm.hostname = "centos-vm"
vm1.vm.network "private_network", auto_config: "false", virtualbox__intnet: "true", ip: "192.168.15.142"
vm1.vm.provision "shell", inline: <<-SHELL
sudo yum update
SHELL
end
config.vm.define "vm2" do |vm2|
vm2.vm.box = "debian/buster64"
vm2.vm.hostname = "debian-vm"
vm2.vm.network "private_network"
vm2.vm.network "private_network", auto_config: "false", virtualbox__intnet: "true", ip: "192.168.15.142"
vm2.vm.provision "shell", inline: <<-SHELL
sudo apt-get update
SHELL
end
end
Vboxmanage 列出虚拟机:
C:\Program Files\Oracle\VirtualBox
λ vboxmanage showvminfo pipeline_vm1_1569857197240_90603
Name: pipeline_vm1_1569857197240_90603
Groups: /
Guest OS: Red Hat (64-bit)
UUID: 4a54f8b2-13b2-4160-80da-79648c538363
Config file: C:\Users\Marlon\VirtualBox VMs\pipeline_vm1_1569857197240_90603\pipeline_vm1_1569857197240_90603.vbox
Snapshot folder: C:\Users\Marlon\VirtualBox VMs\pipeline_vm1_1569857197240_90603\Snapshots
Log folder: C:\Users\Marlon\VirtualBox VMs\pipeline_vm1_1569857197240_90603\Logs
Hardware UUID: 4a54f8b2-13b2-4160-80da-79648c538363
Memory size 4096MB
Page Fusion: disabled
VRAM size: 16MB
CPU exec cap: 100%
HPET: disabled
CPUProfile: host
Chipset: piix3
Firmware: BIOS
Number of CPUs: 4
PAE: enabled
Long Mode: enabled
Triple Fault Reset: disabled
APIC: enabled
X2APIC: enabled
Nested VT-x/AMD-V: disabled
CPUID Portability Level: 0
CPUID overrides: None
Boot menu mode: message and menu
Boot Device 1: Floppy
Boot Device 2: DVD
Boot Device 3: HardDisk
Boot Device 4: Not Assigned
ACPI: enabled
IOAPIC: enabled
BIOS APIC mode: APIC
Time offset: 0ms
RTC: UTC
Hardw. virt.ext: enabled
Nested Paging: enabled
Large Pages: enabled
VT-x VPID: enabled
VT-x unr. exec.: enabled
Paravirt. Provider: Default
Effective Paravirt. Prov.: KVM
State: running (since 2019-09-30T15:26:49.514000000)
Monitor count: 1
3D Acceleration: disabled
2D Video Acceleration: disabled
Teleporter Enabled: disabled
Teleporter Port: 0
Teleporter Address:
Teleporter Password:
Tracing Enabled: disabled
Allow Tracing to Access VM: disabled
Tracing Configuration:
Autostart Enabled: disabled
Autostart Delay: 0
Default Frontend:
Storage Controller Name (0): IDE
Storage Controller Type (0): PIIX4
Storage Controller Instance Number (0): 0
Storage Controller Max Port Count (0): 2
Storage Controller Port Count (0): 2
Storage Controller Bootable (0): on
IDE (0, 0): C:\Users\Marlon\VirtualBox VMs\pipeline_vm1_1569857197240_90603\centos-7-1-1.x86_64.vmdk (UUID: b753eb58-3af1-4af6-b894-36e7546038d3)
NIC 1: MAC: 5254008AFEE6, Attachment: NAT, Cable connected: on, Trace: off (file: none), Type: 82540EM, Reported speed: 0 Mbps, Boot priority: 0, Promisc Policy: deny, Bandwidth group: none
NIC 1 Settings: MTU: 0, Socket (send: 64, receive: 64), TCP Window (send:64, receive: 64)
NIC 1 Rule(0): name = ssh, protocol = tcp, host ip = 127.0.0.1, host port = 2222, guest ip = , guest port = 22
NIC 2: MAC: 0800274B37C2, Attachment: Bridged Interface 'Realtek PCIe GBE Family Controller #2', Cable connected: on, Trace: off (file: none), Type: 82540EM, Reported speed: 0 Mbps, Boot priority: 0, Promisc Policy: deny, Bandwidth group: none
NIC 3: disabled
NIC 4: disabled
NIC 5: disabled
NIC 6: disabled
NIC 7: disabled
NIC 8: disabled
Pointing Device: PS/2 Mouse
Keyboard Device: PS/2 Keyboard
UART 1: disabled
UART 2: disabled
UART 3: disabled
UART 4: disabled
LPT 1: disabled
LPT 2: disabled
Audio: enabled (Driver: DSOUND, Controller: AC97, Codec: STAC9700)
Audio playback: disabled
Audio capture: disabled
Clipboard Mode: disabled
Drag and drop Mode: disabled
Session name: headless
Video mode: 720x400x0 at 0,0 enabled
VRDE: disabled
OHCI USB: disabled
EHCI USB: disabled
xHCI USB: disabled
USB Device Filters:
<none>
Available remote USB devices:
<none>
Currently Attached USB Devices:
<none>
Bandwidth groups: <none>
Shared folders:<none>
VRDE Connection: not active
Clients so far: 0
Capturing: not active
Capture audio: not active
Capture screens:
Capture file: C:\Users\Marlon\VirtualBox VMs\centos-7-1-1.x86_64_1569857189085_60778\centos-7-1-1.x86_64_1569857189085_60778.webm
Capture dimensions: 1024x768
Capture rate: 512kbps
Capture FPS: 25kbps
Capture options:
Guest:
Configured memory balloon size: 0MB
OS type: RedHat_64
Additions run level: 0
Guest Facilities:
No active facilities.
答案1
做了一些修复并更改了我的 Vagranfile,在同一个 LAN 上 ping 网关和主机时一切正常!!更改了 VirtualBox - Host Netkork Manager 以反映我的路由器 IP 范围和 gw 的相同范围和配置...
Vagrant.configure("2") do |config|
config.vm.provider "virtualbox" do |vb|
vb.gui = false
vb.memory = "4096"
vb.cpus = "4"
end
config.vm.define "centos7" do |centos7|
centos7.vm.box = "centos/7"
centos7.vm.hostname = "centos-vm"
centos7.vm.network "public_network", ip: "192.168.15.100"
centos7.vm.provision "shell", run: "always", inline: <<-SHELL
sudo yum update
sudo yum -y install wget ntpdate net-tools nano firewalld jenkins apache
sudo route add default gw 192.168.15.1
# Install jenkins
sudo yum -y install epel-release
sudo yum install java-1.8.0-openjdk
java -version
sudo rpm --import https://jenkins-ci.org/redhat/jenkins-ci.org.key
sudo wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins-ci.org/redhat-stable/jenkins.repo
sudo rpm --import https://jenkins-ci.org/redhat/jenkins-ci.org.key
sudo yum -y install jenkins
sudo systemctl start jenkins
sudo systemctl enable --now jenkins
sudo systemctl status jenkins
# Install docker
curl -fsSL https://get.docker.com/ | sh
sudo systemctl start docker
sudo systemctl status docker
sudo systemctl enable docker
sudo usermod -aG docker $USER
sudo ps -ef | grep dockerd
SHELL
end
config.vm.define "debian10" do |debian10|
debian10.vm.box = "debian/buster64"
debian10.vm.hostname = "debian-vm"
debian10.vm.network "public_network", ip: "192.168.15.102"
debian10.vm.provision "shell", run: "always", inline: <<-SHELL
sudo apt update
sudo apt install wget ntpdate net-tools nano -y
sudo apt install apt-transport-https ca-certificates curl gnupg2 software-properties-common -y
sudo route add default gw 192.168.15.1
curl -fsSL https://download.docker.com/linux/debian/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/debian $(lsb_release -cs) stable"
sudo apt update
apt-cache policy docker-ce
sudo apt install docker-ce -y
sudo usermod -aG docker $USER
sudo systemctl status docker
sudo systemctl enable docker
sudo ps -ef | grep dockerd
SHELL
end
end