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

如何仅通过/etc/hosts解析主机名的IPv4/IPv6地址?

仅基于/etc/hosts解析主机名到IPv4/IPv6的方法

如果你需要严格限制仅从本地/etc/hosts文件解析主机名,禁止回退到DNS,有以下几种可行方案:

1. 全局系统配置修改(影响所有程序)

修改/etc/nsswitch.conf文件,找到hosts:对应的行,移除dns选项,只保留files:

hosts: files

这样所有调用标准名称解析接口(如getaddrinfo)的程序,都会仅查询/etc/hosts,无法解析时直接失败,不会发起DNS请求。

注意:此配置是全局生效的,会影响系统内所有依赖名称解析的程序,修改前需确认是否符合需求。

2. 程序内自定义解析(单个程序可控)

直接在代码中读取并解析/etc/hosts文件,完全自定义解析逻辑,避免依赖系统解析接口。这种方式最灵活,完全可控,不会受系统配置影响。

以C语言为例,简单的解析实现片段:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>

#define HOSTS_FILE "/etc/hosts"
#define MAX_LINE_LEN 1024

void resolve_host_from_local(const char *target_host) {
    FILE *hosts_fp = fopen(HOSTS_FILE, "r");
    if (!hosts_fp) {
        perror("Failed to open /etc/hosts");
        return;
    }

    char line_buf[MAX_LINE_LEN];
    while (fgets(line_buf, MAX_LINE_LEN, hosts_fp)) {
        // 跳过注释行和空行
        char *line_content = strtok(line_buf, "#");
        if (!line_content || strlen(line_content) == 0) continue;

        // 提取IP地址
        char *ip_str = strtok(line_content, " \t");
        if (!ip_str) continue;

        // 判断IP类型(IPv4/IPv6)
        struct in_addr v4_addr;
        struct in6_addr v6_addr;
        int is_ipv4 = (inet_pton(AF_INET, ip_str, &v4_addr) == 1);
        int is_ipv6 = (inet_pton(AF_INET6, ip_str, &v6_addr) == 1);
        if (!is_ipv4 && !is_ipv6) continue;

        // 遍历该行所有主机名/别名,匹配目标
        char *current_host;
        while ((current_host = strtok(NULL, " \t\n"))) {
            if (strcmp(current_host, target_host) == 0) {
                printf("Matched %s: %s\n", is_ipv4 ? "IPv4" : "IPv6", ip_str);
            }
        }
    }

    fclose(hosts_fp);
}

int main(int argc, char *argv[]) {
    if (argc != 2) {
        fprintf(stderr, "Usage: %s <hostname>\n", argv[0]);
        return EXIT_FAILURE;
    }
    resolve_host_from_local(argv[1]);
    return EXIT_SUCCESS;
}

3. 局部临时配置(仅影响单个程序)

通过环境变量指定临时的nsswitch配置,让目标程序的解析仅依赖/etc/hosts,不影响系统其他程序:

  1. 创建临时配置文件(比如/tmp/local_nss.conf),内容如下:
hosts: files
  1. 运行目标程序时,设置环境变量NSS_CONF指向该临时文件:
NSS_CONF=/tmp/local_nss.conf ./your_application

这样该程序调用getaddrinfo时,只会查询/etc/hosts,不会触发DNS请求。


内容的提问来源于stack exchange,提问作者Tootsie

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 23:12:16