Hướng dẫn chỉnh sửa file hosts trên Windows, macOS, và Linux, ưu tiên thao tác bằng dòng lệnh (command), không cần copy file ra/vào.
File hosts dùng để làm gì? Ánh xạ tên miền → địa chỉ IP thủ công Vượt chặn DNS hoặc thử nghiệm web trên IP khác Ví dụ: trỏ example.com về localhost 127.0.0.1 example.com
Windows
Vị trí: file hosts
C:\Windows\System32\drivers\etc\hosts
Mở bằng dòng lệnh (cmd hoặc PowerShell Admin):
notepad C:\Windows\System32\drivers\etc\hosts
Phải chạy Command Prompt hoặc PowerShell bằng quyền Administrator
MacOS
Vị trí file host:
/etc/hosts
Chỉnh bằng Terminal:sudo nano /etc/hosts
Sau khi chỉnh xong, để flush DNS (nếu cần):sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder
Ubuntu (hoặc Debian)
Vị trí file host:
/etc/hosts
Chỉnh bằng Terminal:sudo nano /etc/hosts
Flush DNS nếu dùng systemd:sudo systemd-resolve --flush-caches
Ví dụ nội dung hosts:
127.0.0.1 localhost
127.0.0.1 example.test
192.168.1.10 myserver.local
Lưu ý: Luôn có dòng trống cuối file Địa chỉ IP và domain cách nhau bằng tab hoặc space Sau khi sửa hosts, đừng quên clear cache DNS
