Mac OSX Lion DNS lookup order [closed] - macos

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
After upgrading to Mac OSX Lion I figured out that /etc/hosts is not looked up in first place for name resolution anymore. This leads to some side effects like:
Entries in /etc/hosts are resolved painfully slow
You can't not override existing domains, e.g. 127.0.0.1 www.google.com
If you get search domain entries from DHCP, let say .lan, and some funny guy configured localhost.lan to something else then 127.0.0.1 in the local DNS you can not reach your localhost anymore.
Is this behavior intended? Does it make any sense? And most important, how can I come back to the old behavior.

I think he matter is Lion handles .local TLD differently because it's reserved for some Multicast DNS features (used by Bonjour). The only way i found to solve this issue is using a different TLD for development hosts (ie: .dev). It works fine for me, hope it's gonna be helpful to others!

With regards to overriding domains in the hosts file, I have found that in some circumstances, Lion queries the IPv6 address for a domain if it senses that a domain is unreachable over the IPv4 network.
I discovered this when I noticed some ads that I had never seen before on Snow Leopard because I had redirected the ad domains to 127.0.0.1. I fired up wireshark and noticed AAAA (IPv6 DNS records) queries following the IPv4 A queries (IPv4). The ad servers indeed have IPv6 addesses and were able to serve me their content.
The solution to this is have a
::1 mydomain.com
entry for every
127.0.0.1 mydomain.com
entry in your hosts file.
Interestingly, if you happen to have a local webserver running on 127.0.0.1:80 and your browser receives a response from the webserver (error or otherwise), no AAAA query is issued, as it seems to be satisfied that a TCP connection was at least possible.
On a related note, if you make heavy use of the hosts file (for adblocking, local web development, etc), you may want to look into running your own local DNS resolver. There is a considerable disk/CPU hit from having to read /etc/hosts on every request, so it is in your best interest to keep that file very light.
One advantage of running something like dnsmasq locally (besides the significant performance boost) is that you can redirect whole top-level domains back to your local machine. This allows you to have the whole *.dev namespace for development (for instance), without having to individually enter every domain you want resolved locally into /etc/hosts

The problem was that I symlinked the /etc/hosts file. If /etc/hosts is a plain file everything is ok.

Update(2): OSX 10.10.5 brings the return of mDNSResponder.
Update: OSX 10.10 Yosemite has replaced mDNSResponder with "discoveryd". I've not upgraded so I am not sure of the discoveryd behavior w/r/t DNS lookups and /etc/hosts.
The system DNS resolver on Lion is the mDNSResponder process.
You may be thinking "but mDNSResponder is the multicast dns responder." You're right; that's what it originally was for, and it still fulfills this function. However, on newer MacOS versions it also does standard host lookups.
In Lion, it does not appear to automatically re-read /etc/hosts when it changes, at least not always. Killing mDNSResponder (and allowing it to be automatically restarted) seems to fix the problem.
sudo killall mDNSResponder
should do the trick.
below is my original answer for posterity. I suppose it might still be an issue in some cases.
Make sure your /etc/hosts file is a unix style text file, with linefeeds as the ending rather than cr's.
Editing with TextWrangler or a unix text editor should preserve the file.
If your file is already messed up, try this to fix
tr '\015' '\012' < /etc/hosts > /tmp/hosts.$$
mv /etc/hosts /etc/hosts.bad
mv /tmp/hosts.$$ /etc/hosts
# fix up permissions while we are at it
chown root:wheel /etc/hosts
chmod 644 /etc/hosts
credit for this fix to:
http://techpatio.com/2011/guides-how-to/fixed-mac-osx-lion-etc-hosts-bugs-dns

ive had this issue for a while, as im working a team of devs it became necessary to actually use .local rather then .dev or .localhost, i found this article to be very useful.
iTand.me - Lion local domains and etc hosts..
In summary;
But if you have to use .local, the most elegant solution I've found is the dscl utility. Using it is very straightforward. To add a host called mydev.local and point it to the localhost, just do this:
sudo dscl localhost -create /Local/Default/Hosts/mydev.local IPAddress 127.0.0.1
To see all the currently defined hosts and their IPs
sudo dscl localhost -list /Local/Default/Hosts IPAddress
And to remove a host:
sudo dscl localhost -delete /Local/Default/Hosts/mydev.local
Overall, pretty straightforward and works well. I still would prefer to be able to edit /etc/hosts instead, but this is a better alternative to having to rename all our .local servers.

Prior to moving from Snow Leopard to Lion, I had several app-specific entries in /etc/hosts, like this:
127.0.0.1 foo.bar.local
After the update, loading my local apps was VERY slow. I noticed that the delay happened before the request showed up in the log file, and that once it did, the app itself was as fast as usual.
Now I have two lines per app, like this:
127.0.0.1 foo.bar.local
::1 foo.bar.local
... and everything is fast again.
Apparently this adds IPv6 addresses? I don't quite get it, really, but it works.

My situation was similar, but the delays, of exactly 5 seconds, only happened for URLs ending with '.local'. When viewing sites that ended in '.dev', there was no delay.
Some of the other developers in my office had this problem, while a few did not. I was hoping for a simple fix and I did not want to rename the site to '.local' due to other dependencies.
I ran the following command in Terminal and diffed my output with a few other users in the office.
scutil --dns
This section was the only difference:
resolver #2
domain : 00000000.members.btmm.icloud.com
options : pdns
timeout : 5
order : 150000
My Mac was linked to my iCloud account and I had Back To My Mac enabled. Once I disabled Back To My Mac, the additional resolver went away and the 5 second delay disappeared.

Wow, what a nightmare. I have read absolutely everything on this subject and everything that has been suggested so far was tantilizingly close to what I was experiencing, but none of the solutions worked for me.
And I figured out why.
Unlike others, I was not using /etc/hosts to set up local domains. My /etc/hosts file was stock, containing only the entries needed for the loopback interface and the broadcast host. Moreover, it was a correctly-encoded unix file, as I'm the sort of person who would only edit that from the command-line using emacs. And, thank goodness, I did not have to resort to running my own DNS server like DNSmasq to get around the problem.
(To be clear, the symptom that brought me here to this issue was that emacs took about 10 seconds to start, but only when I was on wifi. If I turned off wifi, emacs would start up instantly as expected.)
My solution: my laptop has a name, "terminator". (Yes, its shiny aluminum exterior made me think of the Arnold Schwarzenegger character.) I just needed to add entries to /etc/hosts for the name of the machine itself:
127.0.0.1 terminator
::1 terminator
I found the name of my host by running a simple command in the terminal:
hostname
...which came back with the output: "terminator". After changing /etc/hosts to contain those two entries, emacs can now quickly resolve my laptop's name.
I hope this helps someone.

I've had speed issues using OSX Lion as a web development box ... Using a combination of suggestions I resorted to disabling ipv6 networking and routing ipv6 to localhost6 ... things sped up quite a bit ...
sudo networksetup -setv6off Ethernet
/etc/hosts ...
127.0.0.1 localhost
127.0.0.1 dev.aliasdomain.com
...
::1 localhost6

I think there's been some bug fixes. I've seen a lot of problems mentioned, and none of these seem to apply currently (for example, putting multiple aliases on a single line now works fine for me).
At any rate it seems that with Lion, Apple made some drastic changes to mDNSResponder which handles all the DNS lookups, and (with Lion at least) also handles /etc/hosts cacheing. For me forward lookups also now work. But reverse lookups (e.g. looking up 1.2.3.4 instead of google.com) don't work.
After a lot of pain, it looks like mDNSResponder converts this lookup to 4.3.2.1.in-addr.arpa and does a name lookup. This may well be how DNS prefers to operate, but it doesn't work at all with /etc/hosts.
Unless of course you add an alias of 4.3.2.1.in-addr.arpa for each host, where 4.3.2.1 is the ip address in the opposite order from which you are used to seeing it. This fixes everything for me. Here's an example /etc/hosts entry:
1.2.3.4 foo foo.example.com alias.example.com 4.3.2.1.in-addr.arpa

Related

Access a localhost running in Windows from inside WSL2? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed last year.
The community reviewed whether to reopen this question 9 months ago and left it closed:
Original close reason(s) were not resolved
Improve this question
I am running a local AEM server in my Windows machine. The server is running on localhost:4502. I am using Ubuntu distro running in WSL2 for my development. I want to access the localhost:4502 running in the Windows machine in my WSL2 Ubuntu.
Is there any way to do that or is it not possible ?
Short answer for most recent Windows versions
mDNS has been a feature of WSL2 for a while now. Concatenating your WSL2 hostname (or the equivalent command/function in your programming/language environment) with ".local" should get you access.
For example, from Bash, try:
ping "$(hostname).local"
For instance, if your hostname is "MyComputer", then the mDNS should be MyComputer.local.
If ICMP is blocked (as it seems to be on new Windows 11 installs), or if you want to test the connection to the actual port, then use netcat. It's available by default in the WSL Ubuntu installation, but may need to be installed in other distributions like openSUSE:
nc -zv "$(hostname).local" <portnumber>
Why localhost doesn't work
WSL2 is running with a virtual network (vNIC) that is created by the Windows Virtual Machine Platform (a subset of Hyper-V). Inside WSL2, localhost is the address of the vNIC.
What you need
WSL2 also sets up a virtual router on the Windows host to allow connectivity to both the outside world as well as the Windows host. You can see this via:
ip route
This is the address you need to use for the Windows host.
You could, of course, parse it from the route (or, as in an earlier answer, from /etc/resolv.conf), but WSL sets up a convenience mDNS (the .local domain) using the Windows "computer name", which is also used as the hostname of the WSL instance.
So concatenating $(hostname) (or it's equivalent in your programming/language environment) with ".local" should get you access.
Other considerations:
mDNS is reliant on the Windows host to resolve the name. If you have changed your /etc/resolv.conf under WSL, then this will likely not work.
Remember to open any necessary firewall ports. WSL2 is considered a separate network from that of the Windows host. Windows will consider network connections from WSL2 to be coming from an external source. (Credit to #RamilGilfanov for a comment pointing this out)
The first time a connection is made from WSL2 to a particular port, Windows Defender (if that is your firewall) will typically display a dialog asking if you want to grant access. However, in my experience, this dialog often gets buried under the main window due to timing of mouse-clicks, keyboard, etc., so it's easy to miss.
Remember to have your Windows service accept connections from remote hosts.
Many servers are configured by default to bind to localhost/127.0.0.1. Because WSL2 appears to Windows as a remote network, you'll typically need to update your configuration to bind to 0.0.0.0 or a specific address.
Note that, since the address for WSL2 changes after each reboot, it can be difficult to update your configuration each time. If at all possible, use 0.0.0.0 unless there are security concerns. Since WSL is designed for development rather than production, this shouldn't be an issue.
I was also looking for some solution to do this but currently, there is no such option available. Check out this GitHub issue:
https://github.com/microsoft/WSL/issues/4619
One solution can be this:
If you have the IP of windows(host) then it will do the job but the only problem is that IP will change every time. But, WSL2 stores your windows(host) IP in /etc/resolv.conf file. So we can modify our etc/hosts to map winhost to the IP dynamically.
Add the following lines at the end of ~/.bashrc file. This will grep the IP and modify the etc/hosts when you boot the WSL.
export winhost=$(cat /etc/resolv.conf | grep nameserver | awk '{ print $2 }')
if [ ! -n "$(grep -P "[[:space:]]winhost" /etc/hosts)" ]; then
printf "%s\t%s\n" "$winhost" "winhost" | sudo tee -a "/etc/hosts"
fi
then run the following command to reload the changes.
$ source ~/.bashrc
now you can use winhost instead of localhost in WSL2(client) to access the servers running windows(host). In your case, it will be winhost:4502 but this will work for any other use cases as well like accessing Postgres/MySQL server running on windows, etc.
NOTE: Always remember to configure your firewall on windows to allow those ports so that WSL2 can access otherwise your connection may get blocked by firewall.
You need add ipv6 rule in hosts file.
Like:
127.0.0.1 example.com
::1 example.com localhost
I had a similar problem and found the solution at this link:
https://github.com/microsoft/WSL/issues/5728
This is simple, you just need to do 2 things. set ip address to localhost in hosts file and allow windows firewall to allow incoming connections.
Here are steps by step instructions:
Get IP address, open cmd type ipconfig /all, note down ip address 172.22.16.1
Open notepad as administrator, File->open , browse to C:\Windows\System32\drivers\etc\hosts , add line 172.22.16.1 localhost, save and close.
Last option is to allow Firewall rules to accept public connections, for this open control panel > System and Security > Windows Defender Firewall > Advanced Settings > Inbound Rules > 'New Rule`
Rule Type : choose Port , click next
Protocol and Ports : type *Specific local port: 4502 , click next
Action : Allow the connection , click next
Profile : check Public , click next
Name : type AEM server , click finish
close other windows, again open cmd , type wsl --shutdown for shutdown ubuntu subsystem.
open ubuntu again.
testing connection type curl localhost:4502 in command prompt. if it returns something means you are good to go.
The easy way:
in ubuntu command line type: ip route
default via 172.23.96.1 dev eth0
172.23.96.0/20 dev eth0 proto kernel scope link src 172.23.97.122
In your app, config the host to: 172.23.97.122
Now you can access using Windows Browser
This really belongs on another forum, but I suspect you could do this.
Step 1. Run your AEM server on your actual IP address (or all addresses) instead of binding to just localhost.
Step 2. Your Ubuntu WSL client instance won't share localhost with the host machine, but it will share the local IP address your machine actually has (e.g. 10.0.0.2). Just have your client connect to that IP address instead of localhost.
I don't know anything about AEM, but you might need to secure it if you are exposing it to a larger network. Block port 4502 on the upstream firewall or use a Windows Firewall rule to only allow incoming on port 4502 from your own IP address. This certainly appears possible.
Windows 10 considers localhost as ::1
Ubuntu considers localhost as 127.0.0.1
So solution is to create a mapping
create a file called .wslconfig in C:\Users<your_username>.wslconfig
add the following to it.
[wsl2]
localhostForwarding=true
PS: I dont know if it mess up anything else but it helped me run django servers which were broken after upgrading to wsl2

How to add a hostname to the LAN so that a Ruby web server, such as WEBrick can serve off that hostname?

We can use Ruby and WEBrick to start a local web server, just by
ruby -run -e httpd -- -p 8080 .
I have seen some iPhone / iPad app being able to add a hostname of http://awesome.local:1234 on the LAN, so a local Mac, and other computers can connect to it. How can we do this using Ruby, adding the hostname and add it together, if possible, with WEBrick?
It is not possible to do anything with Ruby to do this, because this is not Ruby's problem. Computers deal with translation of host names to IP addresses in two different ways: hosts file and a DNS query.
hosts file is a way to tell a specific computer what addresses it knows about. You edit the file, put in what name maps to what IP address, and when that computer makes a request to one of those names, it knows who to contact. Since you need to have the record in the client's computer, there is nothing you can do on the server to affect it, which is why no amount of fiddling with Ruby or WEBrick will help. Also, iPhones don't have a hosts file you can edit.
When a computer does not know a name, it asks someone who does. This someone is a DNS server, a sort of smart Yellow Pages for computers. Your computer has a DNS server it is configured to work with; if it does not know, it will ask other DNS servers for the information. Notably, the router of your LAN almost certainly has DNS functionality.
Thus, the normal way to do what you want to is to tell your router which address it should tell everyone when asked about awesome.local. How to do this is both off-topic for Stack Overflow (as it is not a programming problem; you could ask on SuperUser), and impossible to answer without knowing the exact make and model of your router.
EDIT: Given that your example involves an iOS device, there is another answer: : Apple devices use Bonjour to yell their own configuration data at each other. It's not the app doing it, it is iOS. You can install Bonjour on most other operating systems. There is also a Ruby gem that implements Bonjour protocol, dnssd. I have not worked with it, so I can't tell you whether or not it will solve your problem.

Performance boost with DNS caching?

IN SHORT:
How would one create a local DNS cache on a linux system (ubuntu) so that common queries can run faster, and is it then possible to purge it?
The cache should be populated upon first-queries, not created by hand.
BACKGROUND:
There's a web server up in the cloud which makes connections to itself since the database is currently on the same (virtual)machine. To make it easier for future expansion where the database will be on another server, I've simply pointed the webserver at an address like database.example.com and set the DNS record to 127.0.0.1. The plan is that I can then simply change the DNS record once everything's migrated over. This might seem overkill with just web and database, but there will be other types of servers too (redis, node.js, etc.)
The problem is that when I use the hostname version, it is going very slow (5-10 seconds for session_start). When I use the IP address (i.e. 127.0.0.1), it is very fast (a couple milliseconds).
It seems clear to me that the problem is in DNS, and I believe local caching is a fine solution since it will allow me to manage it all in one place, rather than having to step through the different parts of the system and change configuration.
Install dnsmasq
apt-get install dnsmasq
Lock it down to only localhost add the following to /etc/dnsmasq.conf
listen-address=127.0.0.1
start your service and verify that it is running
service dnsmasq start
dig www.google.com #127.0.0.1
Edit /etc/resolv.conf add the following as your first line
nameserver 127.0.0.1
And remove options rotate if present.
Note you may have some scripts automatically rewriting / changing /etc/resolv.conf it which case you'll have to change those as well (ie dhclient or /etc/network/interfaces)

How to access vhosts via IPad?

I'm using XAMPP, on Mavericks, to simulate a localhost. I set jobeet.local, for example, as my vhosts. I tried to access it on my computer, it worked as well.
Now, I want to test my website on an IPad. So, I did the following steps:
Run XAMPP server on port 80, as usual
Connect the IPad to my wifi network.
Setup Http Proxy on Ipad, as picture showed below but change the port to 80
Now, I tested to access 127.0.0.1 via IPad. It worked! However, jobeet.local doesn't work.
I have searched and tried many ways to achieve this issue but they don't work.
Could you help me figure it out?
Thanks in advance.
You might have to update the hosts file.
The hosts file is a text file that maps hostnames to IP addresses.
Upon typing a url address on the browser, the system is checking if there is a relevant entry on the hosts file and gets the corresponding IP address, else it resolves the IP via the active connection’s DNS servers.
The hosts file can be edited to block certain hostnames (like ad-serving/malicious hosts), or used for web development purposes, i.e. to redirect domains to local addresses.
Editing the hosts file
Editing the hosts file in Mac OS X – Leopard, is a pretty easy task, especially if you are familiar with the terminal.
Step 1 – Open the Terminal.app
Either by start typing Terminal on the Spotlight, or by going into Applications -> Utilities -> Terminal.
Step 2 – Open the hosts file
Open the hosts by typing on the Terminal that you have just opened:
$ sudo nano /private/etc/hosts
Type your user password when prompted.
Step 3 – Edit the hosts file
The hosts file contains some comments (lines starting with the # symbol), as well as some default hostname mappings (e.g. 127.0.0.1 – localhost).
Simply append your new mappings underneath the default ones. Or edit one of the default values if you know what you are doing!
You can navigate the file using the arrow keys.
Step 4 – Save the hosts file
When done editing the hosts file, press control-o to save the file.
Press enter on the filename prompt, and control-x to exit the editor.
Step 5 – Flush the DNS cache
On Leopard you can issue a simple Terminal command to flush the DNS cache, and have your host file changes to take immediate effect:
$ dscacheutil -flushcache
You can now test your new mapping on the browser!
Hope this works for you!
I had to reinstall my setup today and made a step by step at that occasion:
I use a combination of Squidman and Mamp Pro (I assume it's similar to XAMPP)—hope this help anyone in their quest; happy to read feedback or get advice to make this better...
Squidman http://squidman.net/squidman/index.html
Mamp Pro http://www.mamp.info/en/mamp-pro/
on Squidman
- Preferences > General > Http port:
something different from the one MAMP is using (if map uses 80, then put 8080)
Preferences > Clients > Provide proxy service for:
insert the IP address or the subnet we will be catering for
Start Squidman
on MAMP:
setup the hostname, as well as the the website attached
insert the (local) IP address of the local machine
insert the port of Apache on the local machine
start Mamp; check that website is running correctly on local machine
on iPad/iPhone/mobile device
select the same wireless network as the laptop
in Wifi network > Preferences/Information: set a Manual HTTP Proxy
server IP is the (local) IP address of the Apache server, also running Squidman; port is the port used for Squidman
(Extra)
stuff to test: move dev server onto virtual machine (VirtualBox), to use with Node, custom PHP build, etc.
automate the setting: write pref for Squidman, Mamp/hostname, restart the Apache server, send configuration of proxy to mobile via iMessage or email.
I recommend using http://xip.io/. For example (taken from the website):
10.0.0.1.xip.io resolves to 10.0.0.1
www.10.0.0.1.xip.io resolves to 10.0.0.1
mysite.10.0.0.1.xip.io resolves to 10.0.0.1
foo.bar.10.0.0.1.xip.io resolves to 10.0.0.1
It does the job and you don't have to set anything up. I'm only pushing it because I am overly excited that I have wasted time trying over-engineered solutions.
On a mac you can use Squid on Windows that's Fiddler, however I'm having troubles with Fiddler and iPhone at the moment...

How do I get bind to use the DHCP dns for lookup?

I've got XAMPP setup on my laptop (OSX 10.6) for dev, and I wanted to use VirtualDocumentRoot so that I could do *.localhost and it would automap to the folder under my sites directory. I've got this all set up fine, and it works great, but when I got to work today, I found an issue with the way our LAN handles DNS.
Long story short, instead of checking the LAN DNS server for local domains, it goes out to the root. Is there a way to get bind to check the DHCP supplied DNS server for addresses it's not responsible for? Or alternatively, is there a way to get my os to use the DHCP DNS server first, and then fall back to the local with minimal performance hit?
Thanks!
I'm using Linux Arch, but as MacOSX is based on some *nix system - may this ideas helps you:
Take a look at the file /etc/resolv.conf. In my setup this file is automatically generated by NetworkManager.
This document writes about ways to update /etc/resolv.conf when dhcpcd, NetworkManager or dhclient is used: https://wiki.archlinux.org/index.php/Dnsmasq#DHCP_Setup
In this way you do just prepend the local dns before the dhcp's dns (or static if you're switching to a static configuration). Make sure you remove all forwarders from your dns-server.
If macos does not use them, may this workaround gives you a hint, even if it's very limited:
Add a global name-server (like google's one 8.8.8.8) to your dns-server's list of forwarders.

Resources