0

I have installed a headless Virtual Box on my server A, this virtual box contains another ubuntu server B. I can ssh from my laptop to server A, and from server A to server B, but how can I ssh directly from my laptop to server B ?

My goal is to access the web server which is on server B from my laptop.

Below are my current network settings.

  • ifconfig on server A ifconfig on server A

  • Headless VirtualBox settings (VB is on server A) VirtualBox : preferences VirtualBox : server B settings : Adaptater 1 VirtualBox : server B settings : Adaptater 2

  • ifconfig on server B ifconfig on server B

2
  • 4
    This depends on how you've set up networking on your virtual machine within virtualbox. Using bridged networking can give you an IP address on the same network as the host server.
    – Arronical
    Feb 16, 2017 at 10:48
  • I have updated my post with the current network settings on each server and in Virtual Box.
    – DevonDahon
    Feb 17, 2017 at 0:08

3 Answers 3

3
+25

As I can tell from your screenshots of the headless system's network configuration, you have a host-only adapter and a NAT adapter attached to Server B. Neither of these will allow outside machines to access Server B directly unless you set up port forwarding from Server A to Server B.

As Per Chapter 6 of the Virtualbox Manual:

Host-only networking is another networking mode that was added with version 2.2 of VirtualBox. It can be thought of as a hybrid between the bridged and internal networking modes: as with bridged networking, the virtual machines can talk to each other and the host as if they were connected through a physical Ethernet switch. Similarly, as with internal networking however, a physical networking interface need not be present, and the virtual machines cannot talk to the world outside the host since they are not connected to a physical networking interface.

And for NAT:

A virtual machine with NAT enabled acts much like a real computer that connects to the Internet through a router. The "router", in this case, is the VirtualBox networking engine, which maps traffic from and to the virtual machine transparently. In VirtualBox this router is placed between each virtual machine and the host. This separation maximizes security since by default virtual machines cannot talk to each other.

The disadvantage of NAT mode is that, much like a private network behind a router, the virtual machine is invisible and unreachable from the outside internet; you cannot run a server this way unless you set up port forwarding (described below).

So, as per Chapter 6 of the manual, your best bet for allowing the headless machine to easily face the outside network is to give the machine a Bridged Adapter:

With bridged networking, VirtualBox uses a device driver on your host system that filters data from your physical network adapter. This driver is therefore called a "net filter" driver. This allows VirtualBox to intercept data from the physical network and inject data into it, effectively creating a new network interface in software. When a guest is using such a new software interface, it looks to the host system as though the guest were physically connected to the interface using a network cable: the host can send data to the guest through that interface and receive data from it. This means that you can set up routing or bridging between the guest and the rest of your network.

For this to work, VirtualBox needs a device driver on your host system. The way bridged networking works has been completely rewritten with VirtualBox 2.0 and 2.1, depending on the host operating system. From the user perspective, the main difference is that complex configuration is no longer necessary on any of the supported host operating systems.[31]

By giving the headless server a bridged adapter, it will be able to connect to the wider network that Server A is actually connected to, getting its own IP Address from DHCP or from a static IP if necessary.

However, if you are running Server A as a Virtual Private Server (VPS) on some hosting service, that may not be a feasible option. In such a case, I would recommend leaving Server B with a NAT adapter that has been set up with Port Forwarding, forwarding traffic for port 80 and 443 for HTTP/HTTPS so that you can use the web server.

To configure Port Forwarding you can use the graphical Port Forwarding editor which can be found in the Network Settings dialog for Network Adaptors configured to use NAT. Here you can map host ports to guest ports to allow network traffic to be routed to a specific port in the guest.

Alternatively command line tool VBoxManage could be used; for details, please refer to Section 8.8, “VBoxManage modifyvm”.

For further information on the port-forwarding aspect (if needed) or for more information on networking in Virtualbox in general, I strongly recommend you read the relevant parts of Chapter 6 of the Virtualbox Manual.

Edit: I misread your intent to ssh into Server B directly as actually needing to access web pages, rather than SSH. The information above still stands, however. Simply open port 22 (or whatever your desired port for SSH is), as a port forward for the NAT/Host on Server A's Virtualbox Configuration, or set up a bridged adapter.

0

I would change Server A's IP address to something like 192.168.100.2. The third and fourth octets of the current IP address would put it in a different subnet based on the subnet mask of 255.255.255.0 for Server B. Alternatively, you could change Server B's IP address to be in the same subnet mask as Server A. Server B will need a unique IP address (meaning the fourth octet should not be 171).

For Server B, go to VirtualBox's Devices -> Network -> Network Settings -> Adapter 1, then go to "Attached to:" and set it to "Internal Network." Expand "Advanced" then set "Promiscuous mode" to "Allow all." Click "Ok."

Server B will not have Internet access with this solution. But you should be able to connect to it directly from your laptop.

For miscellaneous troubleshooting: I would verify the ports are open from your laptop to server B. If you are using Windows, depending on your version of Windows, you could use a PowerShell script or command to test ports being open to Server B. You could try tracert from your Windows laptop to server B. If your laptop is not Windows, you could use traceroute to see if there is an intermediate firewall blocking connectivity.

0

Use port forwarding.

On Server A go to the NAT adapter's settings (adapter 2 in your case). Click on the port forwarding button there. Now enter 22 (or some other non standard port if you still need to access Server A via ssh) for the host port and 22 for the guest port. I don't think you need to specify the ip's but you can if you want to be precise. However, if you don't specify them (I think) VBox will try to apply those port forwarding rules to every interface on Server A.

With those options set, you should be able to ssh to Server A's ip address at whichever port you specified and you will be forwarded through to Server B.

This answer explains it quite nicely.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .