星期四, 19 6 月, 2025
No Result
View All Result
育心文具行

育心文具行

  • 首頁
  • 心得x體驗
  • 筆記x備忘
  • 閒談x雜記
  • 我的工具箱
  • 關於我
育心文具行
  • 首頁
  • 心得x體驗
  • 筆記x備忘
  • 閒談x雜記
  • 我的工具箱
  • 關於我
No Result
View All Result
育心文具行
Home 筆記x備忘

MacOS 添加本地伺服器的 DNS

2025-03-26
in 筆記x備忘
Reading Time:3 mins read
A A
0

目前本地開發使用 VM 的應用程式(VMWare 或 Parallels),也會需要將 local 的 domain 指向正確的 Private IP。原本都是用 iHost 這套 GUI APP ,這 APP 實作修改 /etc/hosts 的方式達到顯示 local domain 的功能,但有兩個問題:

  1. /etc/hosts 的權限是 root 導致每次開啟的時候都需要操作更換權限。
  2. 沒有支援 wildcard 的功能

於是我想換一個方法處理。可惜的是目前還沒找到 GUI 的 APP,這邊實作在 macOS 上使用 Homebrew 安裝 dnsmasq ,配置讓所有 *.localhost.test 指向 10.211.55.12 :


1. 安裝 dnsmasq

如果尚未安裝 Homebrew,可以先到 Homebrew 官網 參考安裝方法。安裝好 Homebrew 後,打開終端機執行:

brew install dnsmasq

2. 配置 dnsmasq

編輯 dnsmasq 的配置文件 /usr/local/etc/dnsmasq.conf,加入對 *.localhost.test 的解析規則(注意這路徑未必正確):

nano /usr/local/etc/dnsmasq.conf

在文件中新增以下這一行:

address=/.localhost.test/10.211.55.12

這條規則表示所有以 localhost.test 為後綴的域名(例如 abc.localhost.test)都會被解析到 10.211.55.12。


3. 啟動 dnsmasq 服務

配置完成後,啟動 dnsmasq 並設置為開機自啟:

brew services start dnsmasq

如果之後有修改配置,需要重啟服務:

brew services restart dnsmasq

4. 配置 macOS 使用本地 dnsmasq

為了使 macOS 對 localhost.test 域名的 DNS 查詢走本地的 dnsmasq,需要在 /etc/resolver 目錄下建立解析器文件。

1. 先建立 /etc/resolver 目錄(如果尚不存在):

sudo mkdir -p /etc/resolver

2. 創建一個名為 localhost.test 的文件:

sudo nano /etc/resolver/localhost.test

在文件中加入:

nameserver 127.0.0.1

這樣,所有針對 *.localhost.test 的 DNS 請求都會由本地的 dnsmasq(監聽 127.0.0.1)來處理。


5. 測試配置

可以使用 dig 或 nslookup 測試解析是否正確。例如,執行:

dig subdomain.localhost.test

查詢結果應該會顯示 IP 為 10.211.55.12。

以上步驟是用 AI 寫的,需要特別注意:

/usr/local/etc/dnsmasq.conf 這個路徑未必是正確的,安裝完成後提示:

To start dnsmasq now and restart at startup:
  sudo brew services start dnsmasq
Or, if you don't want/need a background service you can just run:
  /opt/homebrew/opt/dnsmasq/sbin/dnsmasq --keep-in-foreground -C /opt/homebrew/etc/dnsmasq.conf -7 /opt/homebrew/etc/dnsmasq.d,\*.conf
==> Summary
🍺  /opt/homebrew/Cellar/dnsmasq/2.91: 11 files, 665.1KB
==> Running `brew cleanup dnsmasq`...

所以設定的路徑是 /opt/homebrew/etc/dnsmasq.conf

這樣之後 VM 統一設定網域 localhost.test 或子網域就能運作啦!

Tags: dnsmasqHomebrew dnsmasqlocalhost.testmac 開發環境macOS DNS 設定Wildcard DNS本機開發域名映射網域名稱解析設定 *.localhost 子域開發者工具 DNS
Share22Tweet14
Previous Post

Mac 上使用 pyenv 管理 python 環境

Next Post

Visual Studio Code 的 PHP CS Fixer 設定

Related Posts

No Content Available
Load More
Next Post

Visual Studio Code 的 PHP CS Fixer 設定

使用 Tailwind CLI 快速建立靜態 CSS 檔的基本步驟

使用 deepwiki-open 針對程式碼產生 WIKI

全站搜尋

No Result
View All Result

關於我

育心文具行

Jerry Lin

程式設計師

育心文具行是一家文具店,老闆的小兒子是一個設計師,職業是設計程式,興趣是設計人,倒是還沒有實作的機會。
--
所有的相遇都不是巧合。
在廣告公司待了三年有兩年多在做內容產品,離職後繼續在內容產業走了四年多,最後選則離開台灣的公司體制在家 SOHO ,碰上了 2020 年疫情才知道原來自己這是走在時代的潮流尖端。 感謝許多老闆和貴人相助,至今目前尚能存活,在台北街頭努力著。

近期文章

  • 於 MacOS 中,整合自然語言,自動化添加行事曆事件
  • 使用 deepwiki-open 針對程式碼產生 WIKI
  • 使用 Tailwind CLI 快速建立靜態 CSS 檔的基本步驟
  • Visual Studio Code 的 PHP CS Fixer 設定
  • MacOS 添加本地伺服器的 DNS

分類

  • 心得x體驗 (45)
  • 筆記x備忘 (77)
  • 閒談x雜記 (13)

贊助買咖啡

Facebook Instagram RSS

標籤

ai android cloudflare flutter git ios javascript linode linux MAC macos mis nginx php plugin QBQ!問題背後的問題 server work sublime sublime text vps vue.js vue3 What if What If? 如果這樣,會怎樣? wordpress 前端 原則 原子習慣 團隊,從傳球開始 團隊,從傳球開始:五百年都難以超越的 UCLA 傳奇教練伍登培養優越人才和團隊的領導心法 壓力測試 外掛 如果這樣,會怎樣?:胡思亂想的搞怪趣問 正經認真的科學妙答 心得 情緒 活動心得 灰階思考 約翰‧漢尼斯 股癌 自慢10 自慢10:18項修練 記錄 謝孟恭 讀書心得 這一生,你想留下什麼?

文章分類

  • 心得x體驗 (45)
  • 筆記x備忘 (77)
  • 閒談x雜記 (13)

近期文章

  • 於 MacOS 中,整合自然語言,自動化添加行事曆事件
  • 使用 deepwiki-open 針對程式碼產生 WIKI
  • 使用 Tailwind CLI 快速建立靜態 CSS 檔的基本步驟
  • Visual Studio Code 的 PHP CS Fixer 設定
  • MacOS 添加本地伺服器的 DNS

近期留言

  • 「Bolin Lai」於〈WordPress 之中藍新金流開啟後, Elementor 外掛有時會打不開的問題處理〉發佈留言
  • 「Jerry Lin」於〈WordPress 之中藍新金流開啟後, Elementor 外掛有時會打不開的問題處理〉發佈留言
  • 「JCL」於〈WordPress 之中藍新金流開啟後, Elementor 外掛有時會打不開的問題處理〉發佈留言

文章月曆

2025 年 6 月
日 一 二 三 四 五 六
1234567
891011121314
15161718192021
22232425262728
2930  
« 5 月    

© 2020 hipster.crazyjerry.studio - a blog about experience, notes and nonsense, by Jerry Lin.

No Result
View All Result
  • 首頁
  • 心得x體驗
  • 筆記x備忘
  • 閒談x雜記
  • 我的工具箱
  • 關於我

© 2020 hipster.crazyjerry.studio - a blog about experience, notes and nonsense, by Jerry Lin.