Locahl
Get Locahl
hosts fileadministratorpermissionssudotroubleshooting

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.

L

Locahl Team

Β·Updated Β·7 min read

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:

PlatformFile locationOwnerWrite access
WindowsC:\Windows\System32\drivers\etc\hostsSYSTEMAdministrators (elevated)
macOS/etc/hostsrootroot (via sudo)
Linux/etc/hostsrootroot (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:

TEXT
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

POWERSHELL
# Open Terminal/PowerShell as administrator first, then:
notepad C:\Windows\System32\drivers\etc\hosts

Or edit inline:

POWERSHELL
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

BASH
sudo nano /etc/hosts

Enter your account password (not root's β€” macOS admin accounts use sudo).

Save: Ctrl+O, Enter, Ctrl+X

What goes wrong

Editing without sudo

BASH
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):

BASH
sudo open -a TextEdit /etc/hosts

Forgetting your account is not admin Standard (non-admin) Mac accounts cannot use sudo. Check: System Settings β†’ Users & Groups β€” your account should say "Admin."

See Edit hosts file on Mac.

Linux: sudo and root

Linux follows the same model as macOS β€” /etc/hosts owned by root.

BASH
sudo nano /etc/hosts
# or
sudo vim /etc/hosts

What goes wrong

User not in sudo group

TEXT
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

BASH
lsattr /etc/hosts
# ----i---------e----- /etc/hosts
sudo chattr -i /etc/hosts  # remove immutable flag
# edit file
sudo chattr +i /etc/hosts  # restore if needed

See 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):

BASH
sudo chown $USER /etc/hosts  # DON'T DO THIS

Always 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.

BASH
sudo chmod 666 /etc/hosts  # NEVER

Correct 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/hosts equivalent in Docker --add-host
  • DNS server they control (dnsmasq on a dev VM)

Verification checklist

After editing with proper admin rights:

Windows:

CMD
ipconfig /flushdns
ping mysite.test

macOS:

BASH
sudo dscacheutil -flushcache && sudo killall -HUP mDNSResponder
ping mysite.test

Linux/Ubuntu:

BASH
sudo resolvectl flush-caches
getent hosts mysite.test

If resolution is correct, your admin elevation worked.

Quick reference by error message

ErrorPlatformFix
Access is deniedWindowsRun editor as administrator
Permission deniedMac/LinuxUse sudo before editor command
Operation not permittedmacOSEnable Full Disk Access for Terminal if using certain tools
Read-only file systemLinuxRemount or check if live USB is read-only
Cannot saveWindowsUAC not approved, or Controlled Folder Access
sudo: command not foundLinuxInstall sudo or use su -
immutable flagLinuxsudo 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:

BASH
ssh user@staging-server
sudo nano /etc/hosts

This 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:

BASH
# Mac/Linux β€” show non-comment lines
grep -v '^#' /etc/hosts | grep -v '^$'
POWERSHELL
# 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.

---

*Last tested: Windows 11 24H2, macOS 15, Ubuntu 24.04 β€” June 2026.*

Share this article
Available on Windows, macOS & Linux

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
Get Locahl β€” $5.99One-time payment, no subscription

Reader Reviews

4.7β˜…(3 reviews)
Karen D.
β˜…β˜…β˜…β˜…β˜…

"Finally understood WHY I kept getting access denied. The Windows UAC section fixed my issue permanently."

June 2, 2026

Mohammed A.
β˜…β˜…β˜…β˜…β˜…

"Cross-platform permissions explained clearly. The Mac sudo vs GUI comparison was especially useful."

June 5, 2026

Elena R.
β˜…β˜…β˜…β˜…β˜…

"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

10 min read
hosts filepermissionsmacOS

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.

L

Locahl Team

8 min read
hosts filesafetysecurity

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.

L

Locahl Team

9 min read
hosts filetroubleshootingmacOS

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.

L

Locahl Team

10 min read
hosts filemacOSreboot

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.

L

Locahl Team