Difference between /etc/hosts and /etc/resolv.conf

8,159

Solution 1

resolv.conf specifies the nameservers for resolver lookups, where it will actually use the DNS protocol for resolving the hostnames.
Typically the hosts file is used for administrative purposes, such as backend and internal functions, which is substantially more isolated in scope, as only the local server will reference it.

/etc/nsswitch.conf specifies the lookup order with the hosts entry.

If this does not answer your question, please clarify further.

Look at the following manpages:

HOSTS(5)  
RESOLVER(5)

Solution 2

resolv.conf specifies nameservers in order of search preference.

hosts overrides all nameservers by mapping urls/shortnames to IPs.

Solution 3

  • /etc/resolv.conf: Lists nameservers that are used by your host for DNS resolution. If you are using DHCP, this file is automatically populated with DNS record issued by DHCP server.
  • /etc/hosts/: It is just a static lookup method for resolution.
  • /etc/nsswitch.conf: It defined order of resolution. Who should it consult first for resolution, a DNS or a host file? For example, if the file has following configuration hosts: files dns then /etc/hosts file will be checked first for resolution, if domain is still un-resolvable, DNS will then be consulted.
Share:
8,159

Related videos on Youtube

shelzmike
Author by

shelzmike

Updated on September 17, 2022

Comments

  • shelzmike
    shelzmike almost 2 years

    I am fairly new to coding, but have built a few small things. One thing I figured out on my last project was how to run 2 simple commands normally run from a console, but from within a form application instead. Simply, the form had 2 buttons and clicking one caused ipconfig to run and the other ipconfig /all. It then posted the ip information coming from the command into another form I created as a message box. That is important because I am trying to do something similar and nothing is working now.

    I have a form that has a spot for user name and a spot for password. On submit, I want it to essentially run the following:

    NET USE F: \\ALPHA\CLIENTAPPS /user:domain\%username% %password% /persistent:no
    NET USE O: \\ALPHA\USERS /user:domain\%username% %password% /persistent:no
    NET USE S: \\ALPHA\COMPANY /user:domain\%username% %password% /persistent:no
    

    Where %username% and %password% are captured from the form and domain will be our actual domain.

    Using similar methods to the aforementioned ipconfig program that is working, this is what I came up with. However, when I click the Submit button, nothing happens, no errors, nor does it actually create the network share:

    private void btnSubmit_Click(object sender, EventArgs e)
    {
        string un = txtUsername.Text;
        string pw = txtPassword.Text;
    
        System.Diagnostics.ProcessStartInfo PR = new System.Diagnostics.ProcessStartInfo("cmd", @" /c net use W: \\\\ALPHA\\CLIENTAPPS /user:acsdish\\" + un + " " + pw + "/persistent:no");
        PR.RedirectStandardOutput = true;
        PR.UseShellExecute = false;
        PR.CreateNoWindow = true;
        System.Diagnostics.Process StartPR = new System.Diagnostics.Process();
        StartPR.StartInfo = PR;
        StartPR.Start();
    }
    

    What am I missing here, or is there a better way? Thanks.

    Mike

  • Zoredache
    Zoredache over 14 years
  • shelzmike
    shelzmike almost 12 years
    Your information is helpful, so thanks for that. Actually, in between me posting and actually checking back in here, I came across the idea somewhere else that using the API would be a better option. I did find this [link] codeproject.com/Articles/6847/… which seems to be good. I was able to use the core and modify to fit my needs. However, This seems to be using the older WNetAddConnection though. I will check out 2 to see if that is more practical, or rather if I can figure it out easily. Thanks again.
  • shelzmike
    shelzmike almost 12 years
    Great info! It is always the little things that offer the most benefit. So, basically, if I use the @ I do not have to escape the \ characters (or anything really)? Thanks again!
  • shelzmike
    shelzmike almost 12 years
    I have more problems going on with trying to get the WNetAddConnection2() to work; however, it is not relevant to the specific question I asked initially so I will search for the answer and if I cannot find it, I will post another one that is related specifically to using that function. Thanks again!