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可实现两个目标:
- 前端无需额外配置
/etc/hosts即可访问 - 前后端使用相同的主机名/环境变量,保持与生产环境一致
已排查内容
- 确认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
相关产品推荐
相关产品推荐

