
Fix EACCES: permission denied on /etc/hosts (2026)
Fix EACCES: permission denied when editing /etc/hosts in Node.js, CLI tools and shell scripts. Learn safe sudo elevation, tee syntax and permissions.
Table of Contents
- How to fix EACCES: permission denied on /etc/hosts
- Understanding /etc/hosts file permissions
- 4 ways to resolve the error
- 1. The shell redirection trap (and how to fix it)
- 2. Run your Node.js build script with sudo
- 3. Elevate privileges programmatically in Node.js
- 4. Restore standard system permissions
- Why you should NEVER run chmod 777 on /etc/hosts
- Safe hosts management with Sleezr
EACCES: permission denied on /etc/hosts occurs when a Node.js script, CLI tool, or shell command attempts to write to the hosts file without root administrator privileges. This system file is write-protected by default to prevent unauthorized network hijacking.
How to fix EACCES: permission denied on /etc/hosts
To fix EACCES: permission denied on /etc/hosts, run your command with `sudo`, use `echo "127.0.0.1 myapp.test" | sudo tee -a /etc/hosts` for shell redirections, or integrate a privilege elevation helper (such as sudo-prompt) into your Node.js scripts.
Understanding /etc/hosts file permissions
On macOS and Linux, inspect the current permissions of the hosts file:
ls -la /etc/hostsThe terminal output typically shows:
-rw-r--r-- 1 root wheel 213 Aug 17 10:00 /etc/hostsBreaking down the permission bits:
- Owner (
root): Read and write access (rw-). - Group (
wheelorroot): Read-only access (r--). - All other users: Read-only access (
r--).
When a Node.js process calls fs.writeFileSync('/etc/hosts', data), the operating system kernel denies the syscall and returns POSIX error code EACCES (Error Access).
See our guide on hosts file permission denied on Mac for platform-specific details.
4 ways to resolve the error
1. The shell redirection trap (and how to fix it)
If you attempt to append an entry using:
# This will fail:
sudo echo "127.0.0.1 api.local.test" >> /etc/hostsThe terminal will still print permission denied. This happens because sudo only elevates the echo command, while the append redirection >> is executed by your unprivileged user shell.
The correct command:
echo "127.0.0.1 api.local.test" | sudo tee -a /etc/hosts > /dev/nullThe tee command runs with elevated sudo privileges and writes to the file directly using the -a (append) flag.
2. Run your Node.js build script with sudo
For standalone provisioning or setup scripts:
sudo node ./scripts/update-hosts.jsOn Windows (PowerShell): Open PowerShell with Run as administrator before executing your setup command.
3. Elevate privileges programmatically in Node.js
Running an entire development suite or test runner as root exposes your system to risks from third-party npm packages. Instead, isolate the hosts file write operation:
import sudo from 'sudo-prompt';
const options = {
name: 'Local Hosts Manager'
};
const domainEntry = '127.0.0.1 myproject.test';
const command = `sh -c 'echo "${domainEntry}" >> /etc/hosts'`;
sudo.exec(command, options, (error, stdout, stderr) => {
if (error) {
console.error('Failed to update hosts file:', error);
return;
}
console.log('Domain added to hosts file successfully.');
});4. Restore standard system permissions
If file permissions on /etc/hosts were modified accidentally:
On macOS:
sudo chown root:wheel /etc/hosts
sudo chmod 644 /etc/hostsOn Linux (Ubuntu/Debian):
sudo chown root:root /etc/hosts
sudo chmod 644 /etc/hostsWhy you should NEVER run chmod 777 on /etc/hosts
Applying chmod 777 /etc/hosts makes the file writable by every user and background process on your machine.
- Any unvetted npm package or malicious script could silently redirect critical domains (such as package registries or banking sites) to hostile IP addresses.
- Operating system security agents may discard hosts file rules if permissions are detected as insecure.
Safe hosts management with Sleezr
Rather than repeatedly running manual terminal commands with sudo, a native hosts manager like Sleezr groups your domains into toggleable environments and uses a hardened, privileged helper to apply changes safely.
Frequently Asked Questions
The /etc/hosts file is a protected system resource with write permissions restricted to the root user. Your script is running under a standard unprivileged user account.
No. Setting 777 permissions allows any unprivileged background process or third-party npm dependency to alter your DNS resolution silently.
The shell redirection operator (>>) is evaluated by your current unprivileged shell before sudo runs. Use echo ... | sudo tee -a /etc/hosts instead.
A dedicated hosts manager like Sleezr uses an isolated, privileged native helper that applies changes securely without requiring your IDE or scripts to run as root.
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.
Sleezr Team
Fix getaddrinfo ENOTFOUND in Docker and Node.js with Hosts File (2026)
Docker container or Node.js app throwing getaddrinfo ENOTFOUND or EAI_AGAIN despite /etc/hosts? Fix Docker network isolation and IPv4 DNS precedence.
Sleezr Team
Developer tools team
How to Reset the Hosts File to Default (Windows, Mac, Linux)
Reset and restore the hosts file to its default contents on Windows, macOS and Linux. Copy the exact default file, back up first, then flush DNS so the reset takes effect.
Sleezr Team
Developer tools team
/etc/hosts Not Working? Fixes for Windows, Mac & Linux
The hosts file is ignored or /etc/hosts changes do not take effect? Fix it on Windows, Mac and Linux: flush DNS, check syntax, IPv6, line endings, resolver caches and save permissions.
Sleezr Team
Developer tools team
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.
Sleezr Team
Hosts File Not Working After Edit on Mac (2026 Fix Guide)
Hosts file not working after edit on Mac? Fix DNS cache, browser cache, permissions, syntax errors, and VPN overrides β with flush commands and a GUI path.
Sleezr Team