feross / hostile

Simple, programmatic `/etc/hosts` manipulation (in node.js)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Attempting to set new line gives back permission denied error

hpbots opened this issue · comments

I am trying to set a line of code to the hosts file but I keep on getting this error:

Error: EACCES: permission denied, open '/etc/hosts

How do I give permission to edit the hosts file using the code:

hostile.set('127.0.0.1', 'example.com', function (err) {
if (err) {
console.error(err);
} else {
console.log('set /etc/hosts successfully!');
}
})

You need to have write access to /etc/hosts - make it writable for your user or run (this part of) your application running sudo, whatever is acceptable for your application/system.

Im not sure if it is, This is through an electron application. Is there a way to do this all through node? @unilynx

/etc/hosts is generally read-only for good reasons, and no framework (eg electron) can magically solve this.

If you are building an application that requires updating /etc/hosts, you need to think about how you arrange for the necessary privileges.

Personally, I would look into separating your /etc/hosts updater as a separate application and request the user to grant you privileges during installation so you can drop a file into /etc/sudoers.d/ grating your script the necessary root access.

But this is beyond the scope of this project - hostile is about updating /etc/hosts without breaking the file, not about requesting the permissions needed to modify it.

I don't think there's anything we can do here