大网数据广告活动
网站运营与故障问答

Why does subdomain.localhost work on Ubuntu but not on Debian? (without hosts entry)

· 52CE 我爱测

I have set up LAMP stacks on both Ubuntu 20.04 and Debian 10 running Apache 2.4. The VirtualHost Files are identical as follows : <VirtualHost localhost:80> ServerAdmin webmaster@localhost ServerName subdomain.localhost ServerAli…

问题原文

原帖链接 · McRaef · CC BY-SA 4.0

I have set up LAMP stacks on both Ubuntu 20.04 and Debian 10 running Apache 2.4.

The VirtualHost Files are identical as follows :

<VirtualHost localhost:80>

    ServerAdmin webmaster@localhost
    ServerName subdomain.localhost
    ServerAlias www.subdomain.localhost
    DocumentRoot /var/www/localhost/subdomain

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>

I have dissabled the default site and enabled the localhost site on both machines.

On Ubuntu i can just visit subdomain.localhost (and www.subdomain.locahost) via my browser. On Debian, however, I have to add an entry to the /etc/hosts file for every subdomain I'd like to create/use.

  1. Why is that? Whats the difference between Debain and Ubuntu that causes that?

  2. Am I able to configure Debian in a way so that I do not need to add an entry for every subdomain for localhost?

已采纳解答

原帖链接 · Stephen Ostermiller · CC BY-SA 4.0

Ubuntu is running the service systemd-resolved that maps localhost subdomains to your local IP address.

If you query the DNS using dig you will see that it gets the local IP address (127.0.0.1) from a SERVER running on a localhost IP address (127.0.0.53):

$ dig www.subdomain.localhost
...
www.subdomain.localhost. 0  IN  A   127.0.0.1
...
;; SERVER: 127.0.0.53#53(127.0.0.53)

The configuration file that sets up the DNS servers to use (resolv.conf) specifies to use this local DNS server:

$ cat /etc/resolv.conf 
...
# 127.0.0.53 is the systemd-resolved stub resolver.
# run "systemd-resolve --status" to see details about the actual nameservers.
nameserver 127.0.0.53

You can verify that the systemd-resolved service is running with

$ sudo service systemd-resolved status
● systemd-resolved.service - Network Name Resolution
     Loaded: loaded (/lib/systemd/system/systemd-resolved.service; enabled; vendor preset: enabled)
     Active: active (running) ...

This service is available on Debian: https://manpages.debian.org/stretch/systemd/systemd-resolved.service.8.en.html but it is not enabled by default. You should be able to start the service on Debian by following the instructions from this Server Fault question: Is that possible for debian users to connect to network through systemd-networkd?

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Webmasters Stack Exchange
适用范围
以原帖系统、软件版本与发布日期为准(2021-03-26)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。