Merge pull request #2038 from geekpi/master

trnalsted
This commit is contained in:
geekpi 2014-11-28 22:52:11 +08:00
commit db71d4442c
2 changed files with 67 additions and 69 deletions

View File

@ -1,69 +0,0 @@
Translating----geekpi
Linux FAQs with Answers--How to access a NAT guest from host with VirtualBox
================================================================================
> **Question**: I have a guest VM running on VirtualBox, which uses NAT networking. So the guest VM is getting a private IP address (10.x.x.x) assigned by VirtualBox. If I want to SSH to the guest VM from the host machine, how can I do that?
VirtualBox supports several networking options for guest VMs, one of them being NAT networking. When NAT networking is enabled for a guest VM, VirtualBox automatically performs network address translation between the guest VM and host's network stack, so that you do not have to configure anything on the host machine and local network for the guest VM's networking to work. The implication of such NAT, however, is that the guest VM is not reachable or visible from external networks as well as from the local host itself. This is a problem if you want to access the guest VM from the host machine for some reason (e.g., SSH).
If you want to access a NAT guest from the host on VirtualBox, you can enable port forwarding for VirtualBox NAT, either from the GUI or from the command line. This tutorial demonstrates **how to SSH a NAT guest from the host** by enabling port forwarding for port 22. If you want to access HTTP of a NAT guest instead, replace port 22 with port 80.
### Configure VirtualBox Port Forwarding from the GUI ###
On VirtualBox, choose the guest VM you want to access, and open "Settings" window of the VM. Click on "Network" menu on the left, click on "Advanced" to show additional network adapter options.
![](https://farm8.staticflickr.com/7583/15797904856_2753dc785e_z.jpg)
Click on a button labeled "Port Forwarding."
![](https://farm8.staticflickr.com/7527/15636152708_cf2be7c7e8_z.jpg)
You will see a window where you can configure port forwarding rules. Click on "Add" icon in the upper right corner.
![](https://farm8.staticflickr.com/7489/15636391217_48a9954480_z.jpg)
Add a new port forwarding rule with the following detail.
- **Name**: SSH (any arbitrary unique name)
- **Protocol**: TCP
- **Host IP**: 127.0.0.1
- **Host Port**: 2222 (any unused port higher than 1024)
- **Guest IP**: IP address of the guest VM
- **Guest Port**: 22 (SSH port)
![](https://farm6.staticflickr.com/5603/15202135853_02a07c3212_o.png)
Port forwarding configured for the guest VM will be enabled automatically when you power on the guest VM. For verification, check that port 2222 is opened by VirtualBox after you launch the guest VM:
$ sudo netstat -nap | grep 2222
![](https://farm8.staticflickr.com/7461/15819682411_6bb9707f8a_z.jpg)
Now that port forwarding is in place, you can SSH to the guest VM bs follows.
$ ssh -p 2222 <login>@127.0.0.1
An SSH login request sent to 127.0.0.1:2222 will automatically be translated into 10.0.2.15:22 by VirtualBox, allowing you to SSH to the guest VM.
### Configure VirtualBox Port Forwarding from the Command Line ###
VirtualBox comes with a command-line management interface called VBoxManage. Using this command-line tool, you can also set up port forwarding for your guest VM.
The following command creates a port forwarding rule for guest VM named "centos7" with IP address 10.0.2.15 and SSH port 22, mapped to local host at port 2222. The name of the rule ("SSH" in this example) must be unique.
$ VBoxManage modifyvm "centos7" --natpf1 "SSH,tcp,127.0.0.1,2222,10.0.2.15,22"
Once the rule is created, you can verify that by using the command below.
$ VBoxManage showvminfo "centos7" | grep NIC
![](https://farm8.staticflickr.com/7559/15636458427_7a0959900c_z.jpg)
--------------------------------------------------------------------------------
via: http://ask.xmodulo.com/access-nat-guest-from-host-virtualbox.html
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](http://linux.cn/) 荣誉推出

View File

@ -0,0 +1,67 @@
nux 有问必答 -- 如何从VirtualBox中从主机访问NAT客户机
================================================================================
> **提问**: 我有一台运行在VirtualBox上的使用NAT的虚拟机。因此虚拟机会被VirtualBox分配一个私有IP地址(10.x.x.x)。如果我想要从主机SSH到虚拟机中我该怎么做
VirtualBox对虚拟机支持几种不同的网络方式其中一种是NAT网络。当虚拟机启用NAT后VirtualBox会自动在虚拟机和主机之间进行网络翻译因此你不于必在虚拟机和主机之间配置任何东西。这也意味着NAT中的虚拟机对于外部网络以及主机本身是不可见的。这会在你想要从主机访问虚拟机时会产生问题比如SSH
如果你想从VirtualBox的NAT环境的虚拟机你可以在GUI或者命令行下启用VirtualBox NAT的端口转发。本篇教程将会演示**如何通过启用22端口转发而从主机SSH连接到NAT环境的客户机**。如果你先想要从HTTP访问NAT的客户机用80端口代替22端口即可。
### 通过GUI配置VirtualBox端口转发 ###
在VirtualBox中选择你想要访问的虚拟机打开虚拟机的“设置”。点击左侧的“网络”菜单点击网络适配选项的“高级”。
![](https://farm8.staticflickr.com/7583/15797904856_2753dc785e_z.jpg)
点击“端口转发”按钮
![](https://farm8.staticflickr.com/7527/15636152708_cf2be7c7e8_z.jpg)
你会看到一个配置端口转发规则的窗口。点击右上角的“添加”图标。
![](https://farm8.staticflickr.com/7489/15636391217_48a9954480_z.jpg)
就会看到像下面那样的转发规则。
- **Name**: SSH (可以是任意唯一名)
- **Protocol**: TCP
- **Host IP**: 127.0.0.1
- **Host Port**: 2222 (任何大于1024未使用的端口)
- **Guest IP**: 虚拟机IP
- **Guest Port**: 22 (SSH 端口)
![](https://farm6.staticflickr.com/5603/15202135853_02a07c3212_o.png)
端口转发的规则会自动在你启动虚拟机的时候启用。为了验证。可以在你启用虚拟机后检查端口2222是否被VirtualBox开启了。
$ sudo netstat -nap | grep 2222
![](https://farm8.staticflickr.com/7461/15819682411_6bb9707f8a_z.jpg)
现在端口转发可以使用了你可以用下面的命令SSH到虚拟机。
$ ssh -p 2222 <login>@127.0.0.1
发送到127.0.0.1:2222的登录请求会自动被VirtualBox翻译成10.0.2.15:22这可以让你SSH到虚拟机中。
### 通过命令行配置VirtualBox端口转发 ###
VirtualBox有一个称为VBoxManage的命令行管理工具。使用命令行工具你也可以为你的虚拟机设置端口转发。
下面的命令会为IP地址为10.0.2.15的虚拟机设置一个名字为"centos7"的端口转发规则SSH的端口号为22,映射到本地主机的端口为2222。规则的名字本例中是SSH必须是唯一的。
$ VBoxManage modifyvm "centos7" --natpf1 "SSH,tcp,127.0.0.1,2222,10.0.2.15,22"
规则创建之后,你可以用下面的命令来验证。
$ VBoxManage showvminfo "centos7" | grep NIC
![](https://farm8.staticflickr.com/7559/15636458427_7a0959900c_z.jpg)
--------------------------------------------------------------------------------
via: http://ask.xmodulo.com/access-nat-guest-from-host-virtualbox.html
译者:[geekpi](https://github.com/geekpi)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](http://linux.cn/) 荣誉推出