ios iPhone:在不越狱的情况下向 /etc/hosts 添加条目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7473939/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 21:34:09  来源:igfitidea点击:

iPhone: add entry to /etc/hosts without jailbreaking

iphoneioshosts-file

提问by Cyrille

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.

对于我的开发过程,我需要访问位于 VPN 后面且没有 DNS 条目的网络服务器。我在 4.x 上所做的是在 iPhone 上编辑 /etc/hosts,并将其添加到主机文件中。

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

现在我在 5.0 测试版上,现在不想为了这个目的而越狱。

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)?

有没有一种方法可以在 /etc/hosts 中添加一行,仅用于开发目的(最终的分发应用程序不需要此 hack),而无需越狱?我可以使用其他方式吗(例如,在应用程序启动时通过某种未知方式声明虚假 DNS 条目)?

采纳答案by duskwuff -inactive-

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.

通过在您的无线网络上设置本地 DNS 服务器,或使用动态 DNS 服务,或通过向您控制 DNS 的域添加 A 记录来设置真正的 DNS 条目。

回答by John K. Chow

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 Proxyto get you started.

编辑:如果您愿意购买小型许可证,我建议您使用Charles Proxy,一种网络调试代理工具。它还将从您的本地 /etc/hosts 解析域,并提供许多额外功能(即检查请求/响应和限制网络速度)。我只是从 WWDC 视频中偶然发现了这个工具,我根本不隶属于该产品。我建议您阅读Chris Ching 的 iPhone 和 Charles Proxy 教程以帮助您入门。



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

要添加到Ramon 的答案中,一种解决方法是将您的本地计算机设置为 DNS 服务器,并将您的 iPhone 指向您的计算机作为 DNS 服务器。这也适用于 Android 设备

The instructions are for Mac OSX via Homebrew:

说明适用于通过 Homebrew 的 Mac OSX:

  1. brew install dnsmasq
    • dnsmasqis 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.5to the file /usr/local/etc/dnsmasq.conf
    • The IP Address 10.0.0.5is 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 dnsmasqwill fulfill unknown entries through the other known DNS servers. Without the router entry, you're whatever devices connected to you dnsmasqwon'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
  1. brew install dnsmasq
    • dnsmasq是一个轻量级的 dns 服务器,当遇到未知域时会回退到原始 DNS 服务器
  2. 将该行添加address=/.your.domain.com/10.0.0.5到文件 /usr/local/etc/dnsmasq.conf
    • IP 地址10.0.0.5是路由器分配给本地计算机的任何 IP 地址。您可以通过以下方式找到它Network Utility(如果您想花哨,可以在路由器中为本地计算机分配一个静态 IP)
  3. sudo dnsmasq
    • 这将启动 dnsmasq 进程,它将侦听 DNS 端口
  4. 通过系统偏好设置 -> 网络 -> 高级 -> DNS 选项卡将您的本地计算机和路由器分配为您计算机的 DNS 服务器
    • 您将有两个条目,一个用于您的本地计算机 (127.0.0.1),另一个用于您的路由器。您包含路由器 IP 的原因是dnsmasq将通过其他已知 DNS 服务器完成未知条目。如果没有路由器入口,您连接的任何设备dnsmasq都不知道如何连接到互联网。
  5. 将本地计算机的 IP 地址设置为 iPhone 的 DNS 服务器,转到设置 -> Wi-Fi -> 已连接路由器的信息图标 -> 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 dnsmasqwill look at your /etc/hosts, so if you had pointed your.domain.comto 127.0.0.1, your iPhone will resolve your.domain.comto 127.0.0.1, which means you won't connect to anything. To change this behaviour edit uncomment the #no-hostsline in the dnsmasq config.
  • 如果您关闭机器,您的 iPhone 将无法再连接到互联网。确保将 iPhone 的 DNS 服务器重置为路由器的 IP
  • 默认情况下dnsmasq会看你的/ etc / hosts文件,因此,如果您曾指出your.domain.com127.0.0.1,你的iPhone将解决your.domain.com127.0.0.1,你将无法连接到任何事物的手段。要更改此行为,请编辑取消注释#no-hostsdnsmasq 配置中的行。

Sources

来源

回答by Ramon Poca

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.

您还可以设置 dnsmasq(可从 macports/brew 获得),它充当 DNS 转发器,允许您设置各种替代记录。

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.

然后你可以在iphone/ipad上设置DNS指向运行DNSmasq的盒子,dnsmasq盒子上/etc/hosts上的任何主机都会先返回。如果没有找到,dnsmasq 会将查询发送到上游 DNS。

Also you can add SRV records to dnsmasq.conf:

您也可以将 SRV 记录添加到 dnsmasq.conf:

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

And many other niceties.

以及许多其他细节。