
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.
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
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 ToggleHosts
Rather than repeatedly running manual terminal commands with sudo, a native hosts manager like ToggleHosts 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 ToggleHosts 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 MacTroubleshooting
Hosts File Permission Denied on Mac
11 min read
Fix getaddrinfo ENOTFOUND in Docker and Node.js with Hosts File (2026)Troubleshooting
Fix getaddrinfo ENOTFOUND in Docker and Node.js with Hosts File (2026)
3 min read
How to Reset the Hosts File to Default (Windows, Mac, Linux)Editing the hosts file
How to Reset the Hosts File to Default (Windows, Mac, Linux)
4 min read