Getting started with Linux: access a NAT client from a host in VirtualBox
Q: I have a virtual machine running on VirtualBox that uses NAT. Therefore, VirtualBox assigns a private IP address (10. x) to the virtual machine ). What should I do if I want to SSH from the host to the virtual machine?
VirtualBox supports several different network methods for virtual machines, one of which is NAT network. When the Virtual Machine enables NAT, VirtualBox automatically performs network translation between the virtual machine and the host, so you do not have to configure anything between the virtual machine and the host. This also means that the virtual machine in NAT is invisible to the external network and the host itself. This will cause problems (such as SSH) when you want to access the virtual machine from the host ).
If you want to use a virtual machine in the VirtualBox NAT environment, you can enable VirtualBox NAT port forwarding in GUI or command line. This tutorial will demonstrate how to connect to a client in the NAT environment through SSH on the host by enabling port 22 forwarding. If you first want to access the NAT client from HTTP, use port 80 instead of port 22.
Configure VirtualBox port forwarding through GUI
In VirtualBox, select the virtual machine you want to access and enable "Settings" of the virtual machine ". Click the "network" menu on the left and click "advanced" of the network adaptation option ".
Click "port forwarding ".
You will see a window for configuring port forwarding rules. Click "add" in the upper-right corner.
The forwarding rules shown below are displayed.
- Name: SSH (can be any unique Name)
- Protocol: TCP
- Host IP: 127.0.0.1
- Host Port: 2222 (any Port not used greater than 1024)
- Guest IP: Virtual Machine IP Address
- Guest Port: 22 (SSH Port)
Port forwarding rules are automatically enabled when you start the virtual machine. For verification. You can check whether port 2222 is enabled by VirtualBox after you enable the VM.
- $ Sudo netstat-nap | grep 2222
Now port forwarding can be used. You can use the following command to SSH to the virtual machine.
- $ Ssh-p 2222 <login> @ 127.0.0.1
The login request sent to 127.0.0.1: 2222 is automatically translated into 10.0.2.15: 22 by VirtualBox, which allows you to SSH to the virtual machine.
Configure VirtualBox port forwarding through the command line
VirtualBox has a command line management tool called VBoxManage. With the command line tool, you can also set port forwarding for your virtual machine.
The following command sets a port forwarding rule for the virtual machine with the IP address 10.0.2.15 named "CentOS7". The SSH port number is 22 and the port mapped to the local host is 2222. The rule name (SSH in this example) must be unique.
- $ VBoxManage modifyvm "centos7" -- natpf1 "SSH, tcp, 127.0.0.1, 2222, 10.0.2.15, 22"
After creating a rule, you can use the following command to verify it.
- $ VBoxManage showvminfo "centos7" | grep NIC
This article permanently updates the link address: