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.
Locahl Team
Table of Contents
- Why admin rights are required
- Windows: Run as administrator
- Correct method
- What goes wrong
- PowerShell as administrator
- Windows security features that block saves
- macOS: sudo and root privileges
- Correct method
- What goes wrong
- Linux: sudo and root
- What goes wrong
- What NOT to do
- Do not change file ownership
- Do not chmod 666
- Do not disable UAC permanently
- GUI tools and admin rights
- Managed and corporate environments
- Verification checklist
- Quick reference by error message
- Remote editing and SSH
- Audit your hosts file regularly
- Related guides
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.
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
1. Press Windows key, type Notepad 2. Right-click Notepad β Run as administrator 3. Click Yes on UAC 4. Open C:\Windows\System32\drivers\etc\hosts 5. 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 Locahl):
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:
- Locahl β 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:
1. Check Group Policy (Windows): gpedit.msc β look for DNS or hosts restrictions 2. Check MDM profiles (Mac): System Settings β Privacy & Security β Profiles 3. Check endpoint security logs for blocked writes 4. 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.*
Ready to simplify your workflow?
Stop wasting time with the terminal. Locahl lets you manage your hosts file in a few clicks, with automatic validation and no risk of errors.
- Intuitive visual interface
- Automatic DNS flush
- Multi-environment management
- Automatic backups
- JSON Import/Export
Reader Reviews
"Finally understood WHY I kept getting access denied. The Windows UAC section fixed my issue permanently."
June 2, 2026
"Cross-platform permissions explained clearly. The Mac sudo vs GUI comparison was especially useful."
June 5, 2026
"Good troubleshooting reference. Kept this bookmarked for onboarding new team members."
June 9, 2026
Frequently Asked Questions
Why do I need administrator rights to edit the hosts file?
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.
How do I edit the hosts file as administrator on Windows?
Right-click Notepad β Run as administrator β open C:\Windows\System32\drivers\etc\hosts β edit β save. See [[edit-hosts-file-windows|Windows 10 guide]].
What does sudo do when editing /etc/hosts on Mac or Linux?
sudo temporarily grants root (superuser) privileges so you can write to system files owned by root. You will be prompted for your account password.
Can a standard user edit the hosts file without admin rights?
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.
Why does Windows say Access Denied even when I am an administrator?
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.
Is it safe to change ownership of the hosts file?
No. Changing ownership from SYSTEM/root to your user account weakens system security. Always use elevation (Run as admin / sudo) instead.
Can corporate policy block hosts file editing?
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 Mac
Fix permission denied errors when editing the Mac hosts file: sudo access, SIP, file ownership, chmod permissions and disk checks.
Locahl Team
Is Editing the Hosts File Safe? (2026)
Is it safe to edit the hosts file? Risks, backups, what can go wrong, when to use hosts vs DNS, and how to revert changes on Windows, Mac and Linux.
Locahl Team
Hosts File Not Working on Mac: Fix Guide
Your hosts file changes not taking effect? Learn how to fix DNS cache issues, browser cache, file permissions, syntax errors, and encoding problems on macOS.
Locahl Team
Hosts File Changes Disappear After Reboot
Hosts file changes gone after restarting Mac? Learn the causes, SIP limits, daemon overwrites and backup strategies to keep changes.
Locahl Team
How to Edit the Host File on Windows, Mac and Linux (2026)
Edit the host file on Windows, macOS and Linux: file location, admin rights, syntax, DNS flush, and troubleshooting when changes do not apply.
Locahl Team