Use SSH to connect to the virtualbox Virtual Machine in Nat Mode

Source: Internet
Author: User
Tags ssh port
Requirement

The Linux server is installed in virtualbox, And the Linux server provides Web Services. The physical host is not connected to a vro or vswitch. The following requirements are available:

  • Connect to the Linux server through SSH in the Windows System of the physical host.

  • You can access the Web services provided by Linux.

Preface

In Windows, virtualbox is used to virtualize a Linux Virtual Machine. Generally, the Network Mode of the Linux virtual machine is set to bridge mode. In this way, the Linux virtual machine is equivalent to an independent parallel existence with the PC, the two systems are in the same LAN and can ping each other. However, the premise is that the current PC is connected to a vro or vswitch. If two systems are in the same LAN without routers, switches, and other devices, the two systems cannot be pinged.

Now that the Linux virtual machine cannot use the bridge mode, set it to the NAT mode (Network Address Translation Nat. Nat Mode means that the virtual machine network is completely dependent on the network of the physical host. In this mode, the two systems do not process the same LAN and cannot directly ping the system. However, virtualbox can set port forwarding rules in Nat mode. By setting rules, the PC can connect to virtual machines in virtualbox.

Practice

Open virtualbox, right-click the Linux virtual machine, and select Settings. In the device panel, selectNetwork, Nic 1 connection mode isNetwork Address Translation Nat, ClickAdvancedTo display advanced settings, such:

ClickPort forwardingClick to open the port forwarding rule list. You can add or delete rules.

Some Terms in the Rules are described as follows:

  • HOST: Physical host system.

  • Subsystem: A system virtualized by virtualbox.

  • Host port: the port of the physical host system, which is forwarded to the subsystem port.

  • Subsystem port: the port of the Virtual Machine subsystem.

The following describes how to add an SSH connection rule.

Click the green plus sign on the right to add a rule to the rule list. Change its namessh(This name can be written as needed ). Protocol selectionTCP.Host IP AddressAndSubsystem IP AddressLeave it blank.Host PortEnter2222Can also be changed to another port, as long as it does not conflict with other ports, the physical host will use this port when connecting to the virtual machine through SSH.Subsystem PortEnter22Because SSH connections use port 22 by default, this cannot be entered at will. For example, if the most common protocol is HTTP, port 80 is used by default, and port 21 is used by default for FTP connections. After entering the information, clickOKButton to save the settings.

Open the Linux virtual machine, install and enable the SSH service, and disable the firewall. In Windows, you can use putty or securecrt to connect to a Linux virtual machine.localhostOr127.0.0.1Enter the port in the port forwarding rule set in virtualboxHost PortIn the preceding example2222. If the connection fails, check whether the Linux virtual machine has been installed and enabled for the SSH service, and disable the firewall.

I installed Apache and vsftp on a Linux Virtual Machine and set virtualbox port forwarding rules, as shown in. In Windowslocalhost:8008Or127.0.0.1:8008You can access the Web page throughlocalhost:2121Or127.0.0.1:2121You can connect to FTP.

Command Line

As mentioned above, port forwarding rules are directly set on the graphical interface, and commands are also used.

Open CMD and locate your virtualbox installation directory, for example, "C: \ Program Files \ oracle \ virtualbox ". For example, the name of my Linux Virtual Machine in virtualbox isUbuntuTo add ssh port forwarding rules in the preceding example, enter the following command:

1
VBoxManage modifyvm "Ubuntu" --natpf1 "ssh,tcp,,2222,,22"

The command is parsed as follows:

1
Vboxmanage modifyvm "Linux Virtual Machine name" -- natpf1 "rule name, protocol, Host IP address, host port, subsystem IP address, subsystem port"

Reference
  • Virtualbox SSH connection using NAT


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.