Getting started with Linux: access a NAT client from a host in VirtualBox

Source: Internet
Author: User
Tags ssh port

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.

  1. $ Sudo netstat-nap | grep 2222

Now port forwarding can be used. You can use the following command to SSH to the virtual machine.

  1. $ 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.

  1. $ 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.

  1. $ VBoxManage showvminfo "centos7" | grep NIC

This article permanently updates the link address:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.