hosts

授权协议 MIT License
开发语言 SHELL
所属分类 应用工具、 终端/远程登录
软件类型 开源软件
地区 不详
投 递 者 谭健柏
操作系统 跨平台
开源组织
适用人群 未知
 软件概览

    __               __
   / /_  ____  _____/ /______
  / __ \/ __ \/ ___/ __/ ___/
 / / / / /_/ (__  ) /_(__  )
/_/ /_/\____/____/\__/____/

Hosts

hosts is a command line program for managinghosts file entries.

hosts works with existing hosts files and entries, making it easier to add,remove, comment, and search hosts file entries using simple, memorablecommands.

hosts is designed to be lightweight, easy to use, and contained in asingle, portable script that can be curled into any environment.

Installation

Homebrew

To install with Homebrew:

brew tap xwmx/taps
brew install hosts

npm

To install with npm:

npm install --global hosts.sh

bpkg

To install with bpkg:

bpkg install xwmx/hosts

Make

To install with Make,clone this repository, navigate to the clone's root directory, and run:

sudo make install

Manual

To install as an administrator, copy and paste one of the following multi-linecommands:

# install using wget
sudo wget https://raw.github.com/xwmx/hosts/master/hosts -O /usr/local/bin/hosts &&
  sudo chmod +x /usr/local/bin/hosts &&
  sudo hosts completions install

# install using curl
sudo curl -L https://raw.github.com/xwmx/hosts/master/hosts -o /usr/local/bin/hosts &&
  sudo chmod +x /usr/local/bin/hosts &&
  sudo hosts completions install
User-only Installation

To install with just user permissions, simply add the hosts script to your$PATH. If you already have a ~/bin directory, for example, you can useone of the following commands:

# download with wget
wget https://raw.github.com/xwmx/hosts/master/hosts -O ~/bin/hosts && chmod +x ~/bin/hosts

# download with curl
curl -L https://raw.github.com/xwmx/hosts/master/hosts -o ~/bin/hosts && chmod +x ~/bin/hosts

Installing with just user permissions doesn't install the completions, buthosts works without them. If you have sudo access and want to install thecompletion scripts, run the following command:

sudo hosts completions install

Arch Linux

A package for Arch users is alsoavailable in the AUR.

Tab Completion

Bash and Zsh tab completion is enabled when hosts is installed usingHomebrew, npm, bpkg, or Make. If you are installing hosts manually,completion can be enabled with a few commands.

Usage

Listing Entries

hosts with no arguments lists the entries in the system's hosts file:

> hosts
127.0.0.1       localhost
255.255.255.255 broadcasthost
::1             localhost
fe80::1%lo0     localhost

hosts called with a string or regular expression will search for entriesthat match.

> hosts localhost
127.0.0.1   localhost
::1         localhost
fe80::1%lo0 localhost

> hosts '\d\d\d'
127.0.0.1         localhost
255.255.255.255   broadcasthost

Adding Entries

To add an entry, use hosts add:

> hosts add 127.0.0.1 example.com
Added:
127.0.0.1 example.com

Run hosts or hosts list to see the new entry in the list:

> hosts
127.0.0.1         localhost
255.255.255.255   broadcasthost
::1               localhost
fe80::1%lo0       localhost
127.0.0.1         example.com

Removing Entries

To remove an entry, use hosts remove, which can take an IPaddress, domain, or regular expression:

> hosts remove example.com
Removing the following entries:
127.0.0.1	example.com
Are you sure you want to proceed? [y/N] y
Removed:
127.0.0.1	example.com

Blocking and Unblocking Domains

hosts provides easy commands for blocking and unblocking domains with IPv4and IPv6 entries:

> hosts block example.com
Added:
127.0.0.1   example.com
Added:
fe80::1%lo0 example.com
Added:
::1         example.com

> hosts unblock example.com
Removed:
127.0.0.1   example.com
Removed:
fe80::1%lo0 example.com
Removed:
::1         example.com

Enabling / Disabling Entries

All entries are enabled by default. Disabiling an entry comments it outso it has no effect, but remains in the hosts file ready to be enabledagain.

> hosts
127.0.0.1         localhost
255.255.255.255   broadcasthost
::1               localhost
fe80::1%lo0       localhost
127.0.0.1         example.com

> hosts disable example.com
Disabling:
127.0.0.1	example.com

> hosts
127.0.0.1         localhost
255.255.255.255   broadcasthost
::1               localhost
fe80::1%lo0       localhost

Disabled:
---------
127.0.0.1         example.com

> hosts enable example.com
Enabling:
127.0.0.1	example.com

> hosts
127.0.0.1         localhost
255.255.255.255   broadcasthost
::1               localhost
fe80::1%lo0       localhost
127.0.0.1         example.com

Backups

Create backups of your hosts file with hosts backups create:

> hosts backups create
Backed up to /etc/hosts--backup-20200101000000

List your backups with hosts backups. If you have existing hosts filebackups, hosts will include them:

> hosts backups
hosts--backup-20200101000000
hosts.bak

hosts backups compare will open your hosts file with diff:

> hosts backups compare hosts--backup-20200101000000
--- /etc/hosts	2020-01-01 00:00:00.000000000
+++ /etc/hosts--backup-20200101000000	2020-01-01 00:00:00.000000000
@@ -8,3 +8,4 @@
 255.255.255.255  broadcasthost
 ::1              localhost
 fe80::1%lo0      localhost
+127.0.0.1        example.com

View a backup with hosts backups show:

> hosts backups show hosts--backup-20200101000000
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##
127.0.0.1       localhost
255.255.255.255 broadcasthost
::1             localhost
fe80::1%lo0     localhost
127.0.0.1       example.com

Restore a backup with hosts backups restore. Before a backup isrestored, a new one is created to avoid data loss:

> hosts backups restore hosts--backup-20200101000000
Backed up to /etc/hosts--backup-20200102000001
Restored from backup: hosts--backup-20200101000000

Viewing and Editing /etc/hosts Directly

hosts file prints the raw contents of /etc/hosts:

> hosts file
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##
127.0.0.1       localhost
255.255.255.255 broadcasthost
::1             localhost
fe80::1%lo0     localhost

hosts edit opens /etc/hosts in your editor:

> hosts edit

--auto-sudo

When the --auto-sudo flag is used, all write operations that requiresudo will automatically rerun the command using sudo when the current userdoes not have write permissions for the hosts file.

To have this option always enabled, add the following line to your shellconfiguration (.bashrc, .zshrc, or similar):

alias hosts="hosts --auto-sudo"

Help

Usage:
  hosts [<search string>]
  hosts add <ip> <hostname> [<comment>]
  hosts backups [create | (compare | delete | restore | show) <filename>]
  hosts block <hostname>...
  hosts completions (check | install [-d | --download] | uninstall)
  hosts disable (<ip> | <hostname> | <search string>)
  hosts disabled
  hosts edit
  hosts enable (<ip> | <hostname> | <search string>)
  hosts enabled
  hosts file
  hosts list [enabled | disabled | <search string>]
  hosts search <search string>
  hosts show (<ip> | <hostname> | <search string>)
  hosts subcommands [--raw]
  hosts remove (<ip> | <hostname> | <search string>) [--force]
  hosts unblock <hostname>...
  hosts --auto-sudo
  hosts -h | --help
  hosts --version

Options:
  --auto-sudo  Run write commands with `sudo` automatically.
  -h --help    Display this help information.
  --version    Display version information.

Help:
  hosts help [<command>]

For full usage, run:

hosts help

For help with a particular command, try:

hosts help <command name>

Subcommands

(default)addbackupsblockcompletionsdisabledisablededitenableenabledfilehelplistremovesearchshowsubcommandsunblockversion

hosts

Usage:
  hosts [<search string>]

Description:
  List the existing IP / hostname pairs, optionally limited to a specified
  state. When provided with a seach string, all matching enabled entries will
  be printed.

  Alias for `hosts list`

hosts add

Usage:
  hosts add <ip> <hostname> [<comment>]

Description:
  Add a given IP address and hostname pair, along with an optional comment.

Exit status:
  0   Entry successfully added.
  1   Invalid parameters or entry exists.

hosts backups

Usage:
  hosts backups
  hosts backups create
  hosts backups compare <filename>
  hosts backups delete  <filename>
  hosts backups restore <filename> [--skip-backup]
  hosts backups show    <filename>

Subcommands:
  backups           List available backups.
  backups create    Create a new backup of the hosts file.
  backups compare   Compare a backup file with the current hosts file.
  backups delete    Delete the specified backup.
  backups restore   Replace the contents of the hosts file with a
                    specified backup. The hosts file is automatically
                    backed up before being overwritten unless the
                    '--skip-backup' flag is specified.
  backups show      Show the contents of the specified backup file.

Description:
  Manage backups.

Exit status:
  0   Success.
  1   Invalid parameters or backup not found.

hosts block

Usage:
  hosts block <hostname>...

Description:
  Block one or more hostnames by adding new entries assigned to `127.0.0.1`
  for IPv4 and both `fe80::1%lo0` and `::1` for IPv6.

Exit status:
  0   <hostname> successfully blocked.
  1   Invalid parameters or entry exists.

Blocklists

hosts completions

Usage:
  hosts completions (check | install [-d | --download] | uninstall)

Options:
  -d, --download  Download the completion scripts and install.

Description:
  Manage completion scripts. For more information, visit:
  https://github.com/xwmx/hosts/blob/master/etc/README.md

Exit status:
  0   Completions successfully installed.
  1   Invalid parameters or other error.

hosts disable

Usage:
  hosts disable (<ip> | <hostname> | <search string>)

Description:
  Disable one or more entries based on a given ip address, hostname, or
  search string.

Exit status:
  0   Entry successfully disabled.
  1   Invalid parameters or entry not found.

hosts disabled

Usage:
  hosts disabled

Description:
  List all disabled entries. This is an alias for `hosts list disabled`.

Exit status:
  0   One or more disabled entries found.
  1   Invalid parameters or no disabled entries found.

hosts edit

Usage:
  hosts edit

Description:
  Open the /etc/hosts file in your $EDITOR.

hosts enable

Usage:
  hosts enable (<ip> | <hostname> | <search string>)

Description:
  Enable one or more disabled entries based on a given ip address, hostname,
  or search string.

Exit status:
  0   Entry successfully enabled.
  1   Invalid parameters or entry not found.

hosts enabled

Usage:
  hosts enabled

Description:
  List all enabled entries. This is an alias for `hosts list enabled`.

Exit status:
  0   One or more enabled entries found.
  1   Invalid parameters or no enabled entries found.

hosts file

Usage:
  hosts file

Description:
  Print the entire contents of the /etc/hosts file.

hosts help

Usage:
  hosts help [<command>]

Description:
  Display help information for hosts or a specified command.

hosts list

Usage:
  hosts list [enabled | disabled | <search string>]

Description:
  List the existing IP / hostname pairs, optionally limited to a specified
  state. When provided with a seach string, all matching enabled entries will
  be printed.

Exit status:
  0   One or more matching entries found.
  1   Invalid parameters or entry not found.

hosts remove

Usage:
  hosts remove (<ip> | <hostname> | <search string>) [--force]
  hosts remove <ip> <hostname>

Options:
  --force  Skip the confirmation prompt.

Description:
  Remove one or more entries based on a given IP address, hostname, or search
  string. If an IP and hostname are both provided, only entries matching the
  IP and hostname pair will be removed.

Exit status:
  0   Entry successfully removed.
  1   Invalid parameters or entry not found.

hosts search

Usage:
  hosts search <search string>

Description:
  Search entries for <search string>.

Exit status:
  0   One or more matching entries found.
  1   Invalid parameters or entry not found.

hosts show

Usage:
  hosts show (<ip> | <hostname> | <search string>)

Description:
  Print entries matching a given IP address, hostname, or search string.

Exit status:
  0   One or more matching entries found.
  1   Invalid parameters or entry not found.

hosts subcommands

Usage:
  hosts subcommands [--raw]

Options:
  --raw  Display the subcommands list without formatting.

Description:
  Display the list of available subcommands.

hosts unblock

Usage:
  hosts unblock <hostname>...

Description:
  Unblock one or more hostnames by removing the entries from the hosts file.

Exit status:
  0   <hostname> successfully unblocked.
  1   Invalid parameters or entry not found

hosts version

Usage:
  hosts (version | --version)

Description:
  Display the current program version.

Tests

To run the test suite, install Bats andrun bats test in the project root directory.


Copyright (c) 2015-present William Melody • See LICENSE for details.

github.com/xwmx/hosts

  • 何为hosts? hosts是一个没有扩展名的系统文件,可以用记事本等工具打开,其作用就是将一些常用的网址域名与其对应的IP地址建立一个关联"数据库",当用户在浏览器中输入一个需要登录的网址时,系统会首先自动从hosts文件中寻找对应的IP地址,一旦找到,系统会立即打开对应网页,如果没有找到,则系统会再将网址提交DNS域名解析服务器进行IP地址的解析。 hosts文件在哪? hosts文件所在路径

  • ❤️欢迎订阅《从实战学python》专栏,用python实现爬虫、办公自动化、数据可视化、人工智能等各个方向的实战案例,有趣又有用!❤️ 更多精品专栏简介点这里 所谓的诗和远方,不过是,要把眼前的苟且,熬过了才有。 前言 哈喽,大家好,我是一条。 相信使用Windows的小伙伴在工作中遇到需要频繁hosts时都非常苦恼。主要有这几个原因: 路径复杂,Windows的hosts文件位于C:\Wind

  • using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Net; using System.Net.Sockets; using System.Text; using System.Threading.Task

  • hosts内容为win10 家庭版 22H2 版本 hosts文件路径:C:\WINDOWS\system32\drivers\etc\hosts 以管理员身份运行记事本,再用记事本打开hosts文件才能保存。 # Copyright (c) 1993-2009 Microsoft Corp. # # This is a sample HOSTS file used by Microsoft TC

 相关资料
  • 问题内容: dnspython会很好地完成我的DNS查找,但它完全忽略的内容。 是否有一个python库调用将做正确的事情?即首先检查,否则仅回退到DNS查找? 问题答案: 我真的不知道,如果你想要做的DNS查找 自己 或者如果你只是想要一台主机的IP地址。如果您想要后者,

  • 有人知道如何编辑 /etc/hosts文件内运行在牛轧糖android工作室模拟器?我将编辑它,这样我就可以在我的本地网络服务器上使用我的虚拟主机。我试图编辑它通过终端使用adb然而,它返回只读文件系统。也尝试使用chmod,但仍然失败。 更新:我还尝试使用adb$拉入和推送文件/adb-s emulator-5554推送~/Desktop/hosts/system/etc/hosts adb:错

  • 问题内容: 我有两台redhat 6.3机器,两者都没有在/ etc / hosts中更改(即,我还没有为它们添加IP地址)。在我的机器上可以正常运行getLocalhost并返回主机名a.domain.com。另一个失败,发生UnknownHostException。 以前,我总是将ip添加到/ etc / hosts,但是我想知道为什么这台机器不需要它。两台机器上的/etc/nsswitch.

  • 问题内容: 我可以将IP地址映射到域名 和 端口吗? 例如,我想映射到 问题答案: 不,那不可能。该端口不是主机名的一部分,因此在-file中没有任何意义。

  • key 含义 hosts 为主机的IP,或者主机组名,或者关键字all user 在远程以哪个用户身份执行。 become 切换成其它用户身份执行,值为yes或者no become_method 与became一起用,指可以为‘sudo’/’su’/’pbrun’/’pfexec’/’doas’ become_user 与bacome_user一起用,可以是root或者其它用户名 脚本里用beca

  • 本文向大家介绍PHP实现抓取Google IP并自动修改hosts文件,包括了PHP实现抓取Google IP并自动修改hosts文件的使用技巧和注意事项,需要的朋友参考一下 无聊中居然又找到个php版本的抓取google hosts的文件,试了下还可以用,ping了下ip,延迟也不是很高,网页打开测试了下速度也很快,大家有兴趣的话可以试试. 自动更新hosts文件, 不覆盖已存在的记录,方便使用

  • 本文向大家介绍批处理添加多个新的域名到hosts,包括了批处理添加多个新的域名到hosts的使用技巧和注意事项,需要的朋友参考一下 要求:批处理(.bat) 添加多个新的域名到hosts,先检测是否已存在,如果已存在的域名就不添加 其他尚未存在的域名,则直接添加 google搜寻是有找到代码  如下: 不过此代码只能检测一行域名 如果要添加以下各种不同域址,如何改写代码才能检测多个的域名? 127

  • 本文向大家介绍linux系统下hosts文件详解及配置,包括了linux系统下hosts文件详解及配置的使用技巧和注意事项,需要的朋友参考一下  hosts文件   hosts —— the static table lookup for host name(主机名查询静态表)。   hosts文件是Linux系统上一个负责ip地址与域名快速解析的文件,以ascii格式保存在/etc/目录下。ho