Does hosts file exist on the iPhone? How to change it? [closed]
Asked Answered
M

8

113

I am developing an application that query to the server. In my Mac, I use the hosts file to change the dns to point to a local server within my local area network.

Now I need to test it with my iPhone, the problem is that my iPhone does not recognize that server due to the missing configuration for dns.

On my Mac or Windows, I simply add: 192.168.0.20 http://www.google.com to the hosts file.

Now how do I let my iPhone know the URL: http://www.google.com ?

Minuet answered 8/1, 2010 at 15:27 Comment(7)
Jailbreak your iPhone, download iFile, navigate to /etc/ and open the file called hosts in the text viewer. Now you can edit it.Rafaelarafaelia
Local Development on iPhone with OSX and Charles Web Proxy deployfx.com/2011/08/…Xenon
OSX iOS Simulator and update your /etc/hosts file.Subservient
Alternatively: install Bonjour (sudo apt-get install avahi-daemon avahi-utils) and access via hostname.local on iphone/ipadArachnid
I was surprised not to see an answer like this one, which worked for me: https://mcmap.net/q/86830/-how-do-you-access-a-website-running-on-localhost-from-iphone-browserObau
When do we get to vote on moderator decisions?Sometime
this is a legit question, the number of upvotes speaks for itself.Heavyladen
O
39

This doesn't directly answer your question, but it does solve your problem...

What make of router do you have? Your router firmware may allow you to set DNS records for your local network. This is what I do with the Tomato firmware

Overrefinement answered 8/1, 2010 at 15:41 Comment(3)
thanks, this is what I did to solve the issue ;)Minuet
I used this approach too, except I'm using pfsense.Jalopy
how will it work, i too have same issue, i am on cisco wrt routerSinless
O
85

Another option here is to have your iPhone connect via a proxy. Here's an example of how to do it with Fiddler (it's very easy):

http://conceptdev.blogspot.com/2009/01/monitoring-iphone-web-traffic-with.html

In that case any dns lookups your iPhone does will use the hosts file of the machine Fiddler is running on. Note, though, that you must use a name that will be resolved via DNS. example.local, for instance, will not work. example.xyz or example.dev will.

Outwardly answered 17/6, 2011 at 12:49 Comment(12)
This is the simplest solution. I should have thought of this!!Nicolas
Awesome. This worked a treat for me!Virginia
Same thing works great in osx, setup a HTTP proxy in network settings i.imgur.com/h5zEm.pngLeeannaleeanne
Local Development on iPhone with OSX and Charles Web Proxy deployfx.com/2011/08/…Xenon
This is a great tool to have in the arsenal for any mobile app development - not just for DNS lookupsCapsular
On the Linux I use mitmproxy for the same thing and it doesn't need any configuration (except setting proxy on the iphone side )Rationale
This should be the correct answer! This actually solves the problem, and is an elegant solution! I love it.Alethiaaletta
Unfortunately Fiddler is Windows-only, so you'll need a virtualized system running Windows if you're in a Mac-only shop.Tactful
Great solution ! On Linux, you also have tinyproxy which is very simple to install and configure.Premier
@JonathonHill: There's now a Mono build in beta. Worked on my Linux Mint 17 install.Electroacoustics
Do I have to create/set the file whenever I switch to a different router in this solution?Discourtesy
Any possibility to access sites ending with .local like example.local ? My setup has restriction and I have to use .local.Ashlar
O
39

This doesn't directly answer your question, but it does solve your problem...

What make of router do you have? Your router firmware may allow you to set DNS records for your local network. This is what I do with the Tomato firmware

Overrefinement answered 8/1, 2010 at 15:41 Comment(3)
thanks, this is what I did to solve the issue ;)Minuet
I used this approach too, except I'm using pfsense.Jalopy
how will it work, i too have same issue, i am on cisco wrt routerSinless
S
31

Not programming related, but I'll answer anyway. It's in /etc/hosts.

You can change it with a simple text editor such as nano.

(Obviously you would need a jailbroken iphone for this)

Showker answered 8/1, 2010 at 15:29 Comment(7)
You can't use nano on a vanilla iphone to directly get to /etc/hosts. I would assume is about an app that would be distributed, in which case you can't mess with files outside your sandbox.Benedikta
@Jaanus: of course I meant with a jailbroken iphone. And he never said anything about an app being redistributed; he simply mentioned he needed to test it on his iphone which had a dns problem.Showker
I don't understand the downvotes. The answer is technically correct and well written.Showker
I'll restore some karma for the edit; if the down-votes were used because of the implicit 'for distributed app' then while I can understand them, I think the downvoters should've left a comment to explain that.Marnie
I presume he only needs to change the hosts file so he can test his app. Once tested, it shouldnt need the hosts file changed. Perfectly acceptable to recommend jailbreaking here!Clapperclaw
Can this answer be updated? I did not find this directory on iOS 8...Kordofanian
@DaMaxContent: The directory is still valid for every version of iOS, as this is part of the POSIX-API afaik. However, the /etc/-directory is often a symbolic link. Try navigating to /private/etc/hosts, if you cannot access /etc/hosts directlyEmpiric
A
14

I just edited my iPhone's 'hosts' file successfully (on Jailbroken iOS 4.0).

  • Installed OpenSSH onto iPhone via Cydia
  • Using a SFTP client like FileZilla on my computer, I connected to my iPhone
    • Address: [use your phone's IP address or hostname, eg. simophone.local]
    • Username: root
    • Password: alpine
  • Located the /etc/hosts file
  • Made a backup on my computer (in case I want to revert my changes later)
  • Edited the hosts file in a decent text editor (such as Notepad++). See here for an explanation of the hosts file.
  • Uploaded the changes, overwriting the hosts file on the iPhone

The phone does cache some webpages and DNS queries, so a reboot or clearing the cache may help. Hope that helps someone.

Simon.

Ageold answered 20/9, 2010 at 3:8 Comment(3)
@Simon I followed the same steps, but it didn't work. Then i tried to change encoding settings, and got it! You have to convert your host file to ANSI in Notepad++ before you save it. For whom failed to get the host file working; click "Convert to ANSI" under "Encoding" menu.Debenture
You can also use the iFile app from Cydia to edit the hosts file on your device without having to use a computer. Ad@mPontiac
.. And change the default password from alpine to something else!Ss
C
7

It might exist, but you cannot change it on a non-jailbreaked iPhone.

Assuming that your development webserver is on a Mac, why don't you simply use its Bonjour name (e.g. MyMac.local.) instead of myrealwebserverontheinternet.com?

Calx answered 8/1, 2010 at 15:37 Comment(1)
all the web services exist in a server within my LAN network. I have a domain name and I would like to map my domain name to this server as well. and immagine that someone gives you an address for all of his webservices, he ask you to query these services using this address, he can show you how to add hosts file to mac, windows, but not iphone :-sMinuet
M
7

In case anybody else falls onto this page, you can also solve this by using the Ip address in the URL request instead of the domain:

NSURL *myURL = [NSURL URLWithString:@"http://10.0.0.2/mypage.php"];

Then you specify the Host manually:

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:myURL];
[request setAllHTTPHeaderFields:[NSDictionary dictionaryWithObjectAndKeys:@"myserver",@"Host"]];

As far as the server is concerned, it will behave the exact same way as if you had used http://myserver/mypage.php, except that the iPhone will not have to do a DNS lookup.

100% Public API.

Millisent answered 12/12, 2011 at 19:14 Comment(3)
However this will only work for the first request, if the mypage.php does any other request to myserver then the app won't do the conversion.Bastard
This approach is good if you don't intend to use cookie.Conidium
Nah. This didn't work either. We are using SSL Pinning, so domain mismatch messed it up: '017-07-13 10:21:42.244723-0400 ApperName[1015:449748] libMobileGestalt MobileGestaltSupport.m:153: pid 1015 (ApperName) does not have sandbox access for frZQaeyWLUvLjeuEK43hmg and IS NOT appropriately entitled 2017-07-13 10:21:42.244886-0400 UnionBank[1015:449748] libMobileGestalt MobileGestalt.c:550: no access to InverseDeviceID (see <rdar://problem/11744455>)'Silicle
B
6

Don't change the DNS on the phone. Instead, connect with wifi to the local network and you are all set.

At my office, we have internal servers with internal DNS that are not exposed to the Internet. I just connect with iPhone to the office wifi and can then access them fine.

YMMV, but instead of configuring the phone DNS, it feels to me that just setting up local internal DNS and wifi is a cleaner and easier solution.

Benedikta answered 8/1, 2010 at 19:36 Comment(0)
S
1

No, an iPhone application can only change stuff within its own little sandbox. (And even there there are things that you can't change on the fly.)

Your best bet is probably to use the servers IP address rather than hostname. Slightly harder, but not that hard if you just need to resolve a single address, would be to put a DNS server on your Mac and configure your iPhone to use that.

Sadducee answered 8/1, 2010 at 15:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.