Sleezr
Download
Fix EACCES: permission denied on /etc/hosts (2026)

Fix EACCES: permission denied on /etc/hosts (2026)

S
Sleezr Team
Β·Β·3 min read

Fix EACCES: permission denied when editing /etc/hosts in Node.js, CLI tools and shell scripts. Learn safe sudo elevation, tee syntax and permissions.

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:

BASH
ls -la /etc/hosts

The terminal output typically shows:

TEXT
-rw-r--r--  1 root  wheel  213 Aug 17 10:00 /etc/hosts

Breaking down the permission bits:

  • Owner (root): Read and write access (rw-).
  • Group (wheel or root): 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:

BASH
# This will fail:
sudo echo "127.0.0.1 api.local.test" >> /etc/hosts

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

BASH
echo "127.0.0.1 api.local.test" | sudo tee -a /etc/hosts > /dev/null

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

BASH
sudo node ./scripts/update-hosts.js

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

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

BASH
sudo chown root:wheel /etc/hosts
sudo chmod 644 /etc/hosts

On Linux (Ubuntu/Debian):

BASH
sudo chown root:root /etc/hosts
sudo chmod 644 /etc/hosts

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

Also readFix permission denied on Mac hosts file
Also readTroubleshoot hosts file not working
Share this article

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

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

S

Sleezr Team

4 min read
hosts fileresetdefault

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.

S

Sleezr Team

Developer tools team

4 min read
hosts filetroubleshootingDNS

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

S

Sleezr Team

Developer tools team

8 min read
hosts fileadministratorpermissions

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.

S

Sleezr Team

10 min read
hosts file not workinghosts filetroubleshooting

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.

S

Sleezr Team