在 Linux 系统上进行主机名查找的最干净方法是什么,首先检查 /etc/hosts?

slo*_*son 5 domain-name-system linux

可能重复:
用于解析主机名的 Linux 命令行实用程序。/etc/hosts

假设我的 /etc/hosts 中有一个条目

1.2.3.4 example.com
Run Code Online (Sandbox Code Playgroud)

要查找 example.com,我可以使用诸如hostnslookupdig 之类的命令,但它们只执行 DNS 查找(而忽略 /etc/hosts)。我想要一种干净简单的跑步方式

mylookupcmd example.com
Run Code Online (Sandbox Code Playgroud)

首先检查 /etc/hosts ,如果失败,则进行 DNS 查找。

我可以

ping example.com | my-scraper-that-outputs-ip-address
Run Code Online (Sandbox Code Playgroud)

并获取 IP 地址,但这不是“干净”的解决方案。