无法在我的 Linux 服务器上运行 hosts 文件

Ste*_*ven 3 virtualhost hosts-file

我在 VirtualBox 上运行了 Ubuntu 11.10,并且已经设置了一个虚拟命名服务器。
我可以从 Windows 7 访问该站点,但无法从 Ubuntu 访问该站点。

我的 HOSTS 文件位于/etc/文件夹中,如下所示:

127.0.0.1       localhost
127.0.1.1       ubuntu-VirtualBox
192.168.0.97     mysite.com
Run Code Online (Sandbox Code Playgroud)

但是每当我尝试从服务器访问 mysite.com 时,我都会被重定向到一个网站,说该域名正在出售。

主机文件不起作用的原因可能是什么?

更新

这是我的 /etc/nsswitch.conf

# /etc/nsswitch.conf
#
# Example configuration of GNU Name Service Switch functionality.
# If you have the `glibc-doc-reference' and `info' packages installed, try:
# `info libc "Name Service Switch"' for information about this file.

passwd:         compat
group:          compat
shadow:         compat

#hosts:          files mdns4_minimal [NOTFOUND=return] dns mdns4
hosts:          files dns
networks:       files

protocols:      db files
services:       db files
ethers:         db files
rpc:            db files

netgroup:       nis
Run Code Online (Sandbox Code Playgroud)

Fra*_*sen 9

首先,检查 nsswitch.conf 是否正确:

$ grep hosts /etc/nsswitch.conf
hosts:          files dns
Run Code Online (Sandbox Code Playgroud)

确保它显示“files dns”,否则在询问 DNS 之前它不会在您的主机文件中查找。

如果在 nsswitch 中它是正确的,我之前已经看到一个不可见的字符以某种方式进入了我的主机文件,使其看起来完全正常,但不可见的字符使其无法工作。
所以尝试删除该行,然后重新创建它 - 手动,不要粘贴。

您可以尝试的下一件事是通过 ping 测试它查找的内容:

$ ping mysite.com
PING mysite.com (192.168.0.97) 56(84) bytes of data.
Run Code Online (Sandbox Code Playgroud)

如果您在那里看到正确的 IP 地址,它确实可以正确解析,并且您的浏览器是造成问题的原因。不要使用 host、dig 或 nslookup,它们会忽略 /etc/hosts!使用 ping 来解析 IP 地址,因为大多数应用程序都会看到它。