You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Amazon Linux 2解析localhost子域名忽略/etc/hosts的配置调整问询

如何修改Amazon Linux 2的名称解析行为,使其不忽略localhost子域名的/etc/hosts条目?

当前现象

在Amazon Linux 2023容器中执行以下命令时,curl仍将app.localhost解析为127.0.0.1:

docker run -it amazonlinux:2023 /bin/bash  -c "echo '172.4.0.2 app.localhost' >> /etc/hosts ; curl -v app.localhost "

输出结果:

*   Trying 127.0.0.1:80...

而在CentOS或Ubuntu容器中执行相同操作,解析结果符合预期:

docker run -it centos /bin/bash  -c "echo '172.4.0.2 app.localhost' >> /etc/hosts ; curl -v app.localhost "

输出结果:

*   Trying 172.4.0.2...

需求背景

本地开发环境包含两个Docker容器:

  • 运行在Amazon Linux 2上的主应用(与生产环境一致)
  • 提供HTTP服务的Mock容器

将Mock容器命名为app.localhost可实现两个目标:

  1. 前端无需额外配置/etc/hosts即可访问
  2. 前后端使用相同的主机名/环境变量,保持与生产环境一致

已排查内容

  • 确认Amazon Linux 2的/etc/host.conf配置与CentOS/Ubuntu一致
  • 执行getent hosts app.localhost返回正确的172.4.0.2
  • 对比glibc版本:Amazon Linux 2为2.34,CentOS为2.28、Ubuntu为2.35,排除版本差异导致的问题
  • 将/etc/nsswitch.conf修改为与Ubuntu一致的配置:
passwd:         files systemd
group:          files systemd
shadow:         files
gshadow:        files

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

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

疑问

是否存在其他影响localhost子域名解析的配置项?


内容的提问来源于stack exchange,提问作者allan.simon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 22:35:11