Are the changes of file /etc/resolv.conf immediate?

前端 未结 1 1121
生来不讨喜
生来不讨喜 2021-01-16 04:52

I am changing the file /etc/resolv.conf. But the DNS is being resolved to the old IP again, not the one I updated to I wish to avoid restarting the server. Can someone pleas

相关标签:
1条回答
  • 2021-01-16 05:51

    resolv.conf

    Because the resolv.conf is ordinary file, correct, but not-so-useful answer to your question is simple yes, the file is changed immediately when any process writes it (exactly the same as any other ordinary file in the system).

    System resolver

    Nevertheless, your actual question is probably that you want to know if a process, which is resolving domain names does immediately use new data in resolv.conf file.

    Any process can read and interpret this file as it wishes, but in absolute majority of situations (let's specialized DNS utilities aside) it simply calls system resolver by using, for example, getaddrinfo() or gethostname() functions.

    So you need to find out how does the system resolver handle these files (it is actually not only resolv.conf but nsswitch.conf too, what affects whole process). One can start looking in respective manpages (resolv.conf and nsswitch.conf). There is a statement, that the respective file is read by the resolver routines the first time they are invoked by a process. Therefore, changes would not be propagated to already running processes.

    But this claim implicitly assumes, program simply calls getaddrinfo() and does not care about anything else. It would be true for most simple utilities etc., but there are ways to change this behavior if considered appropriate.

    There is res_init() function, which is normally called implicitly on first call to getaddrinfo() or alike, but application is free to call it explicitly too. One can read The res_init() function reads the configuration files (see resolv.conf(5)) to get the default domain name, search order and name server address(es). So application has a tool to make its copy of system resolver's state to reread these files. But it has to be explicit request.

    So, in the end, only possible answer is it depends. For the small utility, one can expect the resolver files are read only once, and you have to restart this utility to have updated data. Larger, long-running applications (web browser for example) can have its own way of watching resolv.conf and they can call res_init() when deemed appropriate. In any case, this is application-dependent, it has nothing to do with network manager or whichever other system filling resolv.conf with new data.

    0 讨论(0)
提交回复
热议问题