iPhone: add entry to /etc/hosts without jailbreaking
Asked Answered
E

3

27

For my development process I need to access a webserver which is behind a VPN and has no DNS entry. What I was doing on 4.x was to edit /etc/hosts on the iPhone, and add it to the hosts file.

Now I'm on 5.0 beta, and don't want to jailbreak for now just for this purpose.

Is there a way I can add a line to /etc/hosts, just for development purposes (the final, distribution application does not need this hack), without jailbreaking? Can I use other means (declare a fake DNS entry by some unknown means at application launch, for example)?

Emlin answered 19/9, 2011 at 16:7 Comment(0)
U
15

Set up a real DNS entry, either by setting up a local DNS server on your wireless network, or by using a dynamic DNS service, or by adding an A record to a domain you control DNS for.

Unseemly answered 19/9, 2011 at 16:21 Comment(1)
Simple as hell. I'll go for the local DNS server way, 'cause I can't add an A record on my "global" domain for our "mycompany.lan" domain.Emlin
H
25

EDIT: If you're willing to purchase a small license, I recommend using Charles Proxy, a web debugging proxy tool. It will also resolve domains from your local /etc/hosts, and it gives a lot of bonus features (i.e. inspect requests/responses and throttle network speeds). I only stumbled upon this tool from a WWDC video and I'm not affiliated with the product at all. I recommend reading Chris Ching's tutorial for iPhone and Charles Proxy to get you started.


To add to Ramon's answer, a way around it is to setup your local computer as a DNS server and have your iPhone point to your computer as a DNS server. This would also work for Android devices as well

The instructions are for Mac OSX via Homebrew:

  1. brew install dnsmasq
    • dnsmasq is a lightweight dns server that will fallback to the original DNS server when it encounters an unknown domain
  2. Add the line address=/.your.domain.com/10.0.0.5 to the file /usr/local/etc/dnsmasq.conf
    • The IP Address 10.0.0.5 is whatever the IP address assigned to your local computer by your router. You can find this via Network Utility (if you want to be fancy, you can assign a static IP to your local computer in your router)
  3. sudo dnsmasq
    • This starts dnsmasq process, and it will listen on the DNS ports
  4. Assign your local computer and your router as your DNS servers for your computer via System Preferences -> Network -> Advanced -> DNS Tab
    • You'll have two entries, one for your local computer (127.0.0.1) and one for your router. The reason why you include your router's IP is dnsmasq will fulfill unknown entries through the other known DNS servers. Without the router entry, you're whatever devices connected to you dnsmasq won't know how to connect to the internet.
  5. Set your local computer's IP Address as your DNS Server your iPhone, go to Settings -> Wi-Fi -> Info icon for your connected router -> DNS

Some things to consider:

  • If you shut down your machine, your iPhone won't connect to the internet anymore. Make sure to reset your iPhone's DNS server to your router's IP
  • By default dnsmasq will look at your /etc/hosts, so if you had pointed your.domain.com to 127.0.0.1, your iPhone will resolve your.domain.com to 127.0.0.1, which means you won't connect to anything. To change this behaviour edit uncomment the #no-hosts line in the dnsmasq config.

Sources

Humbertohumble answered 17/12, 2013 at 7:15 Comment(3)
I have been searching for a way to do this so that I could get to my local server sites on my iphone and droids without jailbreaking or rooting. Works perfectly. You need to install dnsmasq on the computer that is running your server.Shareeshareholder
Network Utility was deprecated.Acetal
This worked for me. Finally can do local dev work on my phone, thanks!Rancor
U
15

Set up a real DNS entry, either by setting up a local DNS server on your wireless network, or by using a dynamic DNS service, or by adding an A record to a domain you control DNS for.

Unseemly answered 19/9, 2011 at 16:21 Comment(1)
Simple as hell. I'll go for the local DNS server way, 'cause I can't add an A record on my "global" domain for our "mycompany.lan" domain.Emlin
S
4

You can also set up dnsmasq (available from macports/brew), it acts as a DNS forwarder which allows you to set all kinds of alternative records.

You can then set up the DNS on the iphone/ipad to point to the box running DNSmasq, and any host on /etc/hosts on the dnsmasq box will be returned first. If not found, dnsmasq will send the query to the upstream DNS.

Also you can add SRV records to dnsmasq.conf:

srv-host=_sip._udp.devel.foo.com,devel.foo.com,5070 

And many other niceties.

Splat answered 7/5, 2012 at 9:46 Comment(1)
In my home network, I've got a private cloud host running freenas. I spun up a debian VM there and installed a dnsmasq daemon and now can resolve my internal development hosts from any mobile device. Just need to add to dns settings of wifi configs. This has a huge advantage of also being able to use proper SSL certificates so development that requires tls (eg mobile camera) can get proper certificates.Rawlinson

© 2022 - 2024 — McMap. All rights reserved.