feross/hostile
{ "createdAt": "2013-07-18T01:51:02Z", "defaultBranch": "master", "description": "Simple, programmatic `/etc/hosts` manipulation (in node.js)", "fullName": "feross/hostile", "homepage": "", "language": "JavaScript", "name": "hostile", "pushedAt": "2024-12-23T16:51:19Z", "stargazersCount": 793, "topics": [ "hosts", "ip", "javascript", "nodejs" ], "updatedAt": "2025-11-03T21:51:31Z", "url": "https://github.com/feross/hostile"}hostile [![ci][ci-image]][ci-url] [![npm][npm-image]][npm-url] [![downloads][downloads-image]][downloads-url] [![javascript style guide][standard-image]][standard-url]
Section titled “hostile [![ci][ci-image]][ci-url] [![npm][npm-image]][npm-url] [![downloads][downloads-image]][downloads-url] [![javascript style guide][standard-image]][standard-url]”[ci-image] !: https://img.shields.io/github/workflow/status/feross/hostile/ci/master [ci-url] !: https://github.com/feross/hostile/actions [npm-image] !: https://img.shields.io/npm/v/hostile.svg [npm-url] !: https://npmjs.org/package/hostile [downloads-image] !: https://img.shields.io/npm/dm/hostile.svg [downloads-url] !: https://npmjs.org/package/hostile [standard-image] !: https://img.shields.io/badge/code_style-standard-brightgreen.svg [standard-url] !: https://standardjs.com
Simple, programmatic /etc/hosts manipulation (in node.js)
Section titled “Simple, programmatic /etc/hosts manipulation (in node.js)”
install
Section titled “install”npm install hostileIf you use OS X or Linux, this module assumes your hosts file is at /etc/hosts. On
Windows, it assumes your hosts file is at C:/Windows/System32/drivers/etc/hosts.
Commands that modify the hosts file require root privileges.
list all host file records
Section titled “list all host file records”hostile listset a domain in the hosts file
Section titled “set a domain in the hosts file”hostile set [ip] [host]examples:
hostile set localhost domain.comhostile set 192.168.33.10 domain.comremove a domain from the hosts file
Section titled “remove a domain from the hosts file”hostile remove [host]example:
hostile remove domain.comload a set of hosts from a file
Section titled “load a set of hosts from a file”hostile load [file_path]hosts.txt
# hosts.txt127.0.0.1 github.com127.0.0.1 twitter.comexample:
hostile load hosts.txtunload [remove] a set of hosts from a file
Section titled “unload [remove] a set of hosts from a file”hostile unload [file_path]# hosts.txt127.0.0.1 github.com127.0.0.1 twitter.comexample:
hostile unload hosts.txtmethods
Section titled “methods”Commands that modify the hosts file require root privileges.
I wouldn’t recommend running your production node server with admin privileges unless you
downgrade to a normal user with
process.setuid(id)
before you start accepting requests.
All methods have sync versions. Just omit the callback parameter.
add a rule to /etc/hosts
Section titled “add a rule to /etc/hosts”var hostile = require('hostile')hostile.set('127.0.0.1', 'peercdn.com', function (err) { if (err) { console.error(err) } else { console.log('set /etc/hosts successfully!') }})If the rule already exists, then this does nothing.
remove a rule from /etc/hosts
Section titled “remove a rule from /etc/hosts”hostile.remove('127.0.0.1', 'peercdn.com', function (err) { if (err) { console.error(err) } else { console.log('set /etc/hosts successfully!') }})If the rule does not exist, then this does nothing.
get all lines in /etc/hosts
Section titled “get all lines in /etc/hosts”// If `preserveFormatting` is true, then include comments, blank lines and other// non-host entries in the resultvar preserveFormatting = false
hostile.get(preserveFormatting, function (err, lines) { if (err) { console.error(err.message) } lines.forEach(function (line) { console.log(line) // [IP, Host] })})get all lines in any file
Section titled “get all lines in any file”// If `preserveFormatting` is true, then include comments, blank lines and other// non-host entries in the resultvar preserveFormatting = false
hostile.getFile(file_path, preserveFormatting, function (err, lines) { if (err) { console.error(err.message) } lines.forEach(function (line) { console.log(line) // [IP, Host] })})contributors
Section titled “contributors”- Feross Aboukhadijeh (author)
- Maayan Glikser
license
Section titled “license”MIT. Copyright (c) Feross Aboukhadijeh.