
How to Edit the Hosts File as Administrator (2026)
Why the hosts file requires admin rights on Windows, Mac and Linux. Fix access denied, permission errors, and UAC issues on every platform.
Manage hosts files without the terminal
ToggleHosts helps you manage environments visually on Windows, macOS, and Linux, with automatic DNS flush and backups.
One-time payment
Every operating system protects the hosts file behind administrator privileges. If you have ever seen "Access denied," "Permission denied," or a UAC prompt you dismissed, this guide explains why admin rights are required, how to properly elevate on each platform, and how to fix the most common permission errors.
How to edit the hosts file as administrator
To edit the hosts file as administrator, elevate your editor before opening the file:
- Windows: right-click Notepad (or Windows Terminal) and choose Run as administrator, then open
C:\Windows\System32\drivers\etc\hosts. - Mac: run
sudo nano /etc/hostsand enter your admin password. - Linux: run
sudo nano /etc/hosts(orsudo -e /etc/hosts). - Make your changes, then save.
- If you still get "Access/Permission denied," you opened the editor without elevation, close it and reopen as administrator.
Per-platform elevation and error fixes are below.
See the main walkthrough: How to edit the host file on all platforms
Why admin rights are required
The hosts file is not a user document, it is a system configuration file that controls how your computer resolves every domain name. If any program could modify it silently:
- Malware could redirect your bank's domain to a phishing server
- Adware could inject ad-serving domains
- Attackers could intercept HTTPS by redirecting certificate validation domains
Operating systems therefore restrict write access:
| Platform | File location | Owner | Write access |
|---|---|---|---|
| Windows | C:\Windows\System32\drivers\etc\hosts | SYSTEM | Administrators (elevated) |
| macOS | /etc/hosts | root | root (via sudo) |
| Linux | /etc/hosts | root | root (via sudo) |
Reading the file never requires admin rights. Writing always does.
Windows: Run as administrator
On Windows, membership in the Administrators group is necessary but not sufficient. User Account Control (UAC) requires explicit elevation for each editing session.
Correct method
- Press Windows key, type Notepad
- Right-click Notepad → Run as administrator
- Click Yes on UAC
- Open
C:\Windows\System32\drivers\etc\hosts - Edit and Ctrl+S
What goes wrong
Opening Notepad normally, then navigating to the file
You can open and edit the file, but saving fails with:
Cannot create the C:\Windows\System32\drivers\etc\hosts file.
Make sure that the path and file name are correct.Or simply: Access is denied.
Fix: Close Notepad completely. Reopen with Run as administrator.
Opening the file from File Explorer double-click
Same problem, File Explorer does not elevate Notepad automatically.
Fix: Always launch the editor as administrator first, then open the file from within the elevated editor.
PowerShell as administrator
# Open Terminal/PowerShell as administrator first, then:
notepad C:\Windows\System32\drivers\etc\hostsOr edit inline:
Add-Content -Path C:\Windows\System32\drivers\etc\hosts -Value "127.0.0.1 mysite.test"See Windows 11 guide for more methods.
Windows security features that block saves
Even with admin elevation, these can interfere:
Controlled Folder Access
- Settings → Privacy & security → Windows Security → Ransomware protection
- Add Notepad/VS Code/Terminal to allowed apps
Third-party antivirus
- Norton, McAfee, Kaspersky often "protect" the hosts file
- Look for "hosts file shield" or "DNS protection" in antivirus settings
Group Policy (corporate machines)
- IT may deploy policies that lock the hosts file
- Contact your administrator, do not attempt to bypass corporate security
macOS: sudo and root privileges
On Mac, /etc/hosts is owned by root:wheel with permissions -rw-r--r--. Only root can write.
Correct method
sudo nano /etc/hostsEnter your account password (not root's, macOS admin accounts use sudo).
Save: Ctrl+O, Enter, Ctrl+X
What goes wrong
Editing without sudo
nano /etc/hosts
# Error: [ Error writing /etc/hosts: Permission denied ]Using a GUI text editor without elevation
TextEdit opened normally cannot save to /etc/hosts.
Fix for TextEdit (not recommended, use Terminal or ToggleHosts):
sudo open -a TextEdit /etc/hostsForgetting your account is not admin
Standard (non-admin) Mac accounts cannot use sudo. Check: System Settings → Users & Groups, your account should say "Admin."
Linux: sudo and root
Linux follows the same model as macOS, /etc/hosts owned by root.
sudo nano /etc/hosts
# or
sudo vim /etc/hostsWhat goes wrong
User not in sudo group
user is not in the sudoers file. This incident will be reported.Fix: Ask your system administrator to add you to the sudo group, or use su - to switch to root (if you have root password).
Editing as wrong user in multi-user systems
Shared servers may restrict sudo via /etc/sudoers. Check with your admin.
Immutable flag set
lsattr /etc/hosts
# ----i---------e----- /etc/hosts
sudo chattr -i /etc/hosts # remove immutable flag
# edit file
sudo chattr +i /etc/hosts # restore if neededSee Linux guide and Ubuntu guide.
What NOT to do
Do not change file ownership
Bad (Windows):
Taking ownership of hosts from SYSTEM to your user account weakens security and may break Windows Update behavior.
Bad (Mac/Linux):
sudo chown $USER /etc/hosts # DON'T DO THISAlways use elevation instead of ownership changes.
Do not chmod 666
Making the file world-writable allows any process to modify it, exactly what the OS tries to prevent.
sudo chmod 666 /etc/hosts # NEVERCorrect permissions: -rw-r--r-- (644), owned by root.
Do not disable UAC permanently
Some tutorials suggest disabling UAC to "fix" access denied. This exposes your entire system to silent elevation by malware. Always use Run as administrator per session.
GUI tools and admin rights
Dedicated hosts file editors handle elevation for you:
- ToggleHosts, prompts for admin password once, manages elevation automatically on Mac
- SwitchHosts, requires admin on each apply on Windows
- Gas Mask, macOS, handles sudo internally
Even GUI tools cannot bypass the underlying requirement, they just automate the elevation prompt.
Managed and corporate environments
If you cannot edit the hosts file despite following all steps:
- Check Group Policy (Windows):
gpedit.msc→ look for DNS or hosts restrictions - Check MDM profiles (Mac): System Settings → Privacy & Security → Profiles
- Check endpoint security logs for blocked writes
- Ask IT, they may provide a approved process or deny hosts edits by policy
Developers on locked-down corporate laptops sometimes use:
- Remote development on a personal VM or cloud instance
/etc/hostsequivalent in Docker--add-host- DNS server they control (dnsmasq on a dev VM)
Verification checklist
After editing with proper admin rights:
Windows:
ipconfig /flushdns
ping mysite.testmacOS:
sudo dscacheutil -flushcache && sudo killall -HUP mDNSResponder
ping mysite.testLinux/Ubuntu:
sudo resolvectl flush-caches
getent hosts mysite.testIf resolution is correct, your admin elevation worked.
Quick reference by error message
| Error | Platform | Fix |
|---|---|---|
| Access is denied | Windows | Run editor as administrator |
| Permission denied | Mac/Linux | Use sudo before editor command |
| Operation not permitted | macOS | Enable Full Disk Access for Terminal if using certain tools |
| Read-only file system | Linux | Remount or check if live USB is read-only |
| Cannot save | Windows | UAC not approved, or Controlled Folder Access |
| sudo: command not found | Linux | Install sudo or use su - |
| immutable flag | Linux | sudo chattr -i /etc/hosts |
Remote editing and SSH
On remote Linux servers accessed via SSH, you edit the server's hosts file, not your local machine's:
ssh user@staging-server
sudo nano /etc/hostsThis affects resolution on that server only. For local laptop resolution, edit your local hosts file. Confusing the two is a common mistake when debugging distributed systems, always confirm which machine's resolver you are configuring.
Audit your hosts file regularly
Schedule a monthly review:
# Mac/Linux, show non-comment lines
grep -v '^#' /etc/hosts | grep -v '^$'# Windows PowerShell (admin)
Get-Content C:\Windows\System32\drivers\etc\hosts | Where-Object { $_ -notmatch '^s*#' -and $_.Trim() -ne '' }Remove entries you do not recognize. Compare against your last known backup. Unknown redirects of financial, email, or antivirus domains warrant an immediate security scan.
Related guides
- Main guide, all platforms
- Windows 10
- Windows 11
- macOS
- Linux
- Ubuntu
- Permission denied on Mac
- Is editing safe?
*Last tested: Windows 11 24H2, macOS 15, Ubuntu 24.04, June 2026.*
Sources and further reading
Frequently Asked Questions
The hosts file controls domain-to-IP resolution for your entire system. Restricting write access prevents malware from redirecting your traffic to malicious servers without your knowledge.
Right-click Notepad → Run as administrator → open C:\Windows\System32\drivers\etc\hosts → edit → save. See [[edit-hosts-file-windows-11|Windows 10 guide]].
sudo temporarily grants root (superuser) privileges so you can write to system files owned by root. You will be prompted for your account password.
No, not directly. Standard users can read the file but cannot save changes. Workarounds like changing file ownership are not recommended and create security risks.
Windows UAC requires explicit elevation per session. Being in the Administrators group is not enough, you must choose Run as administrator for each edit session.
No. Changing ownership from SYSTEM/root to your user account weakens system security. Always use elevation (Run as admin / sudo) instead.
Yes. Group Policy, MDM profiles, and endpoint security tools can lock the hosts file. Contact your IT administrator if edits are blocked in a managed environment.
Related Articles
Hosts File Permission Denied on MacTroubleshooting
Hosts File Permission Denied on Mac
11 min read
Fix EACCES: permission denied on /etc/hosts (2026)Troubleshooting
Fix EACCES: permission denied on /etc/hosts (2026)
3 min read
Fix NET::ERR_CERT_COMMON_NAME_INVALID with Hosts File (2026)Troubleshooting
Fix NET::ERR_CERT_COMMON_NAME_INVALID with Hosts File (2026)
4 min read