Skip to content

Instantly share code, notes, and snippets.

@gotnix
Last active August 24, 2016 07:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gotnix/9a1dbc0a076d4e15eaec2784081e4b82 to your computer and use it in GitHub Desktop.
Save gotnix/9a1dbc0a076d4e15eaec2784081e4b82 to your computer and use it in GitHub Desktop.
解决 VirtualBox NAT 网络(Network Address Translation Service)不工作

首先我通过「全局设定」新建了一个 NAT 网络,配置如下:

[terry@imtz.tm:~] [二 8月 23 - 23:32:41]
456 => $ VBoxManage list natnetworks
NetworkName:    NatNetwork
IP:             10.0.2.1
Network:        10.0.2.0/24
IPv6 Enabled:   No
IPv6 Prefix:    fd17:625c:f037:2::/64
DHCP Enabled:   Yes
Enabled:        Yes
Port-forwarding (ipv4)
        ssh04:tcp:[127.0.0.1]:2204:[10.0.2.4]:22
loopback mappings (ipv4)
        127.0.0.1=2

但是 VirtualBox 的日志里没有异常,2204 这个端口也没有监听,倒是有一个 Virtualbox 相关的僵尸进程,看名字就猜到这个问题十有八九就是它导致的:

terry     5034  0.0  0.0      0     0 ?        Z    22:37   0:00 [VBoxNetNAT] <defunct>

然后查到父进程是 VBoxSVC,然后就尝试 kill -HUP 了一下尝试回收僵尸进程,就把父进程给 kill 没了,手动跑了一下,发现有报错如下:

453 => $ /usr/lib/virtualbox/VBoxSVC --auto-shutdown
****************************************************
Oracle VM VirtualBox XPCOM Server Version 5.1.4_SUSE
(C) 2004-2016 Oracle Corporation
All rights reserved.

Starting event loop....
[press Ctrl-C to quit]
Informational: VirtualBox object created (rc=NS_OK).
VBoxNetNAT: Error -10 in SUPR3HardenedMain!
VBoxNetNAT: Effective UID is not root (euid=1000 egid=100 uid=1000 gid=100)

VBoxNetNAT: Tip! It may help to reinstall VirtualBox.

通过 Google 找到 What does “Effective UID is not root” mean for VirtualBox?,得到以下命令:

for bin in VirtualBox VirtualBoxVM VBoxNetAdpCtl VBoxNetDHCP VBoxNetNAT VBoxHeadless; do
    chmod u+s /usr/lib/virtualbox/${bin}
done

这几个文件有的已经加过 uid 了,我就拿现成的命令一起跑了,结果如下,ss 的输出已经看到要转发的端口已经在监听了:

[root@imtz.tm:/usr/lib/virtualbox] [Tue Aug 23 - 23:45:21]
197 => # ps aux | head -1 ; ps aux | grep VBoxNet
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
terry     3812  0.0  0.3 391316 14880 ?        Sl   23:44   0:00 /usr/lib/virtualbox/VBoxNetDHCP --ip-address 10.0.2.3 --lower-ip 10.0.2.4 --mac-address 08:00:27:67:99:FD --need-main on --netmask 255.255.255.0 --network NatNetwork --trunk-type whatever --upper-ip 10.0.2.254
terry     3813  0.0  0.5 530560 22044 ?        Sl   23:44   0:00 /usr/lib/virtualbox/VBoxNetNAT --ip-address 10.0.2.1 --netmask 255.255.255.0 --network NatNetwork --trunk-type whatever
root      3927  0.0  0.0   9356   948 pts/0    S+   23:45   0:00 grep --color=auto VBoxNet

[root@imtz.tm:/usr/lib/virtualbox] [Tue Aug 23 - 23:45:28]
198 => # ss -antlop '( sport = :2204 )'
State      Recv-Q Send-Q  Local Address:Port    Peer Address:Port
LISTEN     0      5           127.0.0.1:2204               *:*       users:(("VBoxNetNAT",pid=3813,fd=47))

最后,我还遇到一个坑,openSUSE 的 rpm 包没有加 kernel-symskernel-source 这 2 个包的依赖,要手动装一下。另外,执行 VirtualBox 图形化程序的用户加一下 vboxusers 这个用户组。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment