# hostile

> Simple /etc/hosts manipulation

Latest version **1.4.0** (published 2024-01-12) · MIT license · 0 weekly downloads

## Install

```sh
npm install hostile
pnpm add hostile
yarn add hostile
bun add hostile
```

Provides the command `hostile`.

## Health

**Score 23/100 (F)** — status: abandoned.

Positive: has types package; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.4.0 |
| Published | 2024-01-12 |
| First published | 2013-07-18 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | separate (@types/hostile) |
| Module format | CommonJS |
| Dependencies | 5 |
| Unpacked size | 14.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 795 |
| Author | Feross Aboukhadijeh |
| Maintainers | feross |
| Keywords | /etc/hosts, change hosts, etc hosts, hostname, hosts file |

## Links

- npm: https://www.npmjs.com/package/hostile
- Repository: https://github.com/feross/hostile
- Issues: https://github.com/feross/hostile/issues
- Funding: https://github.com/sponsors/feross
- npm.io page: https://npm.io/package/hostile

## Dependencies (5)

- [once](https://npm.io/package/once.md) ^1.4.0
- [chalk](https://npm.io/package/chalk.md) ^4.1.2
- [split](https://npm.io/package/split.md) ^1.0.1
- [minimist](https://npm.io/package/minimist.md) ^1.2.8
- [@ljharb/through](https://npm.io/package/@ljharb/through.md) ^2.3.11

## Recent versions

- 1.4.0 (latest) — 2024-01-12
- 1.3.3 — 2020-10-27
- 1.3.2 — 2018-05-25
- 1.3.1 — 2018-02-16
- 1.3.0 — 2017-02-26
- 1.2.0 — 2016-12-27
- 1.1.1 — 2016-12-10
- 1.1.0 — 2016-10-30
- 1.0.4 — 2016-10-30
- 1.0.3 — 2016-07-15
- 1.0.2 — 2015-05-13
- 1.0.1 — 2015-05-05
- 1.0.0 — 2014-11-07
- 0.7.0 — 2014-10-07
- 0.6.0 — 2014-04-06
- … 9 more at https://npm.io/package/hostile/versions

## README

# 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)

![hostile](https://raw.github.com/feross/hostile/master/img.png)

## install

```bash
npm install hostile
```

## usage

If 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

```bash
hostile list
```

#### set a domain in the hosts file

```bash
hostile set [ip] [host]
```

examples:
```bash
hostile set localhost domain.com
hostile set 192.168.33.10 domain.com
```

#### remove a domain from the hosts file

```bash
hostile remove [host]
```

example:
```bash
hostile remove domain.com
```

#### load a set of hosts from a file

```bash
hostile load [file_path]
```
hosts.txt
```bash
# hosts.txt
127.0.0.1 github.com
127.0.0.1 twitter.com
```

example:
```bash
hostile load hosts.txt
```

#### unload [remove] a set of hosts from a file

```bash
hostile unload [file_path]
```

```bash
# hosts.txt
127.0.0.1 github.com
127.0.0.1 twitter.com
```

example:
```bash
hostile unload hosts.txt
```

## 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)`](http://nodejs.org/api/process.html#process_process_setuid_id)
before you start accepting requests.

**All methods have sync versions. Just omit the callback parameter.**

#### add a rule to /etc/hosts

```js
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

```js
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

```js
// If `preserveFormatting` is true, then include comments, blank lines and other
// non-host entries in the result
var 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

```js
// If `preserveFormatting` is true, then include comments, blank lines and other
// non-host entries in the result
var 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

- [Feross Aboukhadijeh](http://feross.org) (author)
- [Maayan Glikser](https://github.com/morsdyce)

## license

MIT. Copyright (c) [Feross Aboukhadijeh](http://feross.org).

---
_Source: https://npm.io/package/hostile · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
