如何使用 VirtualBox 正确进行端口转发?
我在连接 VirtualBox 中的来宾操作系统时遇到问题。 我的环境如下:
- 主机:MacOSX (Lion)
- 访客:CentOS (5.7, 2.6.18-274.3.1.el5)
- VirtualBox: 4.1.4 r74291 -- 网络适配器类型:NAT
在来宾操作系统中,我可以看到 httpd 正在正常运行,如下所示:
$ ps aux | grep httpd
root 2571 0.0 3.5 258440 8824 ? Ss 08:20 0:00 /usr/sbin/httpd
apache 2573 0.0 2.2 258440 5780 ? S 08:20 0:00 /usr/sbin/httpd
apache 2574 0.0 2.0 258440 5184 ? S 08:20 0:00 /usr/sbin/httpd
apache 2575 0.0 2.0 258440 5180 ? S 08:20 0:00 /usr/sbin/httpd
apache 2576 0.0 2.0 258440 5180 ? S 08:20 0:00 /usr/sbin/httpd
apache 2577 0.0 2.0 258440 5180 ? S 08:20 0:00 /usr/sbin/httpd
apache 2578 0.0 2.0 258440 5180 ? S 08:20 0:00 /usr/sbin/httpd
apache 2579 0.0 2.0 258440 5180 ? S 08:20 0:00 /usr/sbin/httpd
apache 2580 0.0 2.0 258440 5180 ? S 08:20 0:00 /usr/sbin/httpd
$ curl localhost
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
...
在主机操作系统中,我进行了如下配置来进行端口转发:
$ VBoxManage showvminfo CentOS_3 | grep "NIC 1"
NIC 1: MAC: 080027B1FA87, Attachment: NAT, Cable connected: on, Trace: off (file: none), Type: Am79C973, Reported speed: 0 Mbps, Boot priority: 0, Promisc Policy: deny
NIC 1 Settings: MTU: 0, Socket (send: 64, receive: 64), TCP Window (send:64, receive: 64)
NIC 1 Rule(0): name = http, protocol = tcp, host ip = , host port = 8003, guest ip = , guest port = 80
NIC 1 Rule(1): name = ssh, protocol = tcp, host ip = , host port = 2203, guest ip = , guest port = 22
随着此信息的进行,我正在尝试执行以下操作端口转发从 8003 到 80,从 2203 到 22。 事实上,我可以使用“ssh -p 2203 localhost”与来宾操作系统建立 ssh 连接。 但是,当我尝试查看 http://localhost:8003 时,浏览器显示 “无法加载网页,因为服务器没有发送数据。”
当我在主机操作系统中尝试“telnet localhost 8003”时 看来端口正在监听,但是有 来宾操作系统的访问日志中未显示任何日志。
我应该怎么做才能解决这个问题?提前致谢!
(补充:10月14日15:55)
$ netstat -nl | grep tcp | grep 127.0.0.1 # Host OS
tcp4 0 0 127.0.0.1.8003 127.0.0.1.64698 ESTABLISHED
tcp4 0 0 127.0.0.1.64698 127.0.0.1.8003 ESTABLISHED
tcp4 0 0 127.0.0.1.26164 127.0.0.1.53917 ESTABLISHED
tcp4 0 0 127.0.0.1.53917 127.0.0.1.26164 ESTABLISHED
tcp4 0 0 127.0.0.1.2203 127.0.0.1.53183 ESTABLISHED
tcp4 0 0 127.0.0.1.53183 127.0.0.1.2203 ESTABLISHED
$ netstat -nl | grep tcp # Guest OS
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:952 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN
tcp 0 0 :::80 :::* LISTEN
tcp 0 0 :::22 :::* LISTEN
tcp 0 0 :::443 :::* LISTEN
I have a trouble connecting to a guest OS in VirtualBox.
My environtment is as follows:
- Host: MacOSX (Lion)
- Guest: CentOS (5.7, 2.6.18-274.3.1.el5)
- VirtualBox: 4.1.4 r74291
-- Network Adapter Type: NAT
In the guest OS, I can see that the httpd is running properly like:
$ ps aux | grep httpd
root 2571 0.0 3.5 258440 8824 ? Ss 08:20 0:00 /usr/sbin/httpd
apache 2573 0.0 2.2 258440 5780 ? S 08:20 0:00 /usr/sbin/httpd
apache 2574 0.0 2.0 258440 5184 ? S 08:20 0:00 /usr/sbin/httpd
apache 2575 0.0 2.0 258440 5180 ? S 08:20 0:00 /usr/sbin/httpd
apache 2576 0.0 2.0 258440 5180 ? S 08:20 0:00 /usr/sbin/httpd
apache 2577 0.0 2.0 258440 5180 ? S 08:20 0:00 /usr/sbin/httpd
apache 2578 0.0 2.0 258440 5180 ? S 08:20 0:00 /usr/sbin/httpd
apache 2579 0.0 2.0 258440 5180 ? S 08:20 0:00 /usr/sbin/httpd
apache 2580 0.0 2.0 258440 5180 ? S 08:20 0:00 /usr/sbin/httpd
$ curl localhost
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
...
In the host OS, I did a configuration to do port forwarding as follows:
$ VBoxManage showvminfo CentOS_3 | grep "NIC 1"
NIC 1: MAC: 080027B1FA87, Attachment: NAT, Cable connected: on, Trace: off (file: none), Type: Am79C973, Reported speed: 0 Mbps, Boot priority: 0, Promisc Policy: deny
NIC 1 Settings: MTU: 0, Socket (send: 64, receive: 64), TCP Window (send:64, receive: 64)
NIC 1 Rule(0): name = http, protocol = tcp, host ip = , host port = 8003, guest ip = , guest port = 80
NIC 1 Rule(1): name = ssh, protocol = tcp, host ip = , host port = 2203, guest ip = , guest port = 22
As this information go, I'm trying to do port forwarding from 8003 to 80 and from 2203 to 22.
In fact, I can make the ssh connection to the guest OS with "ssh -p 2203 localhost".
However, when I try to see http://localhost:8003, the browser says
"Unable to load the webpage because the server sent no data."
When I try "telnet localhost 8003" in the host OS
it seems the port is listening, but there
appears no log in the access log of the guest OS.
What should I do to solve this problem? Thanks in advance!
(addition: Oct. 14 15:55)
$ netstat -nl | grep tcp | grep 127.0.0.1 # Host OS
tcp4 0 0 127.0.0.1.8003 127.0.0.1.64698 ESTABLISHED
tcp4 0 0 127.0.0.1.64698 127.0.0.1.8003 ESTABLISHED
tcp4 0 0 127.0.0.1.26164 127.0.0.1.53917 ESTABLISHED
tcp4 0 0 127.0.0.1.53917 127.0.0.1.26164 ESTABLISHED
tcp4 0 0 127.0.0.1.2203 127.0.0.1.53183 ESTABLISHED
tcp4 0 0 127.0.0.1.53183 127.0.0.1.2203 ESTABLISHED
$ netstat -nl | grep tcp # Guest OS
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:952 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN
tcp 0 0 :::80 :::* LISTEN
tcp 0 0 :::22 :::* LISTEN
tcp 0 0 :::443 :::* LISTEN
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看来就像mkj所写的那样,您安装了防火墙。
尝试运行(以root身份):
yum install system-config-firewall-tui
运行 system-config-firewall-tui,选择自定义并确保选中“WWW (HTTP)”。
It seems that like mkj wrote, you have a firewall installed.
try running (as root):
yum install system-config-firewall-tui
run system-config-firewall-tui, select customize and make sure that "WWW (HTTP)" is checked.
虽然是老问题,但这可能对某人有帮助,因为我今天遇到了这个问题。
正如 mkj 所提到的,问题出在来宾操作系统(在我的例子中是 Centos 6)防火墙上。它允许 ssh(端口 22)连接,但阻止其他连接。我在从主机访问来宾操作系统(端口 5432)中运行的 postgresql 服务器时遇到问题。因此,在来宾操作系统上执行以下 iptables 命令是有效的:
在您的情况下,对于在端口 80 运行的 httpd 服务器,您可能必须执行以下操作:
在将 ubuntu/debian 作为来宾操作系统运行时,我从未遇到过此问题,因此可能未配置防火墙他们。
Although old question, but this may be helpful to someone, as I faced this problem today.
As mentioned by mkj, the issue is with Guest OS ( Centos 6 in my case) firewall. It allows ssh (port 22) connection, but blocks others. I had issue accessing postgresql server running in Guest OS (at port 5432) from Host. So executing following iptables command on Guest OS worked:
In your case, for httpd server running at port 80, you may have to do below:
I never faced this problem when running ubuntu/debian as Guest OS, so probably firewall is not configured in them.