windows 和ubuntu下host文件

来源:互联网 发布:阿里云pc 编辑:程序博客网 时间:2024/06/07 19:38

有时候,我们需要让某些域名指向本地,方便进行代码调试,这就需要修改host文件。下面就来说一说windows系统和ubuntu系统修改host文件的方式:

Windows系统下:

进入 C:\Windows\System32\drivers\etc,可以看到HOSTS文件,该文件没有后缀名,一般需要通过管理员身份打开修改,下面给出该文件的例子:

# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
# 102.54.94.97 rhino.acme.com # source server
# 38.25.63.10 x.acme.com # x client host


# localhost name resolution is handled within DNS itself.
# 127.0.0.1 localhost
# ::1 localhost

修改时只需要在文件后面追加需要域名解析的ip 和名称即可。


Ubuntu系统的Hosts只需修改/etc/hosts文件,修改完成之后需要重启网络:

sudo gedit  /etc/hosts
需要添加的格式为:
<IP> <HOSTNAME>.<DOMAIN> <ALIAS>
例如:216.239.37.99 www.google.com

修改完成后,重启网络:

sudo /etc/init.d/networking restart