自动修改hosts文件

来源:互联网 发布:客单价方面的优化 编辑:程序博客网 时间:2024/06/07 07:26

首先上github链接,复制这个shell脚本,放在某个文件夹下,我是放在/etc/lhosts

先打开hosts文件vim /etc/hosts,发现其中内容很少,只有10+行,运行刚刚的脚本sudo bash /etc/lhosts之后再看看hosts文件

    1 # 远程 hosts 开始标记,请在 上方 添加其他内容   2 # Copyright (c) 2014-2017, racaljk.   3 # https://github.com/racaljk/hosts   4 # Last updated: 2017-04-20   5    6 # This work is licensed under a CC BY-NC-SA 4.0 International License.   7 # https://creativecommons.org/licenses/by-nc-sa/4.0/   8    9 # Localhost (DO NOT REMOVE)  10 127.0.0.1       localhost  11 ::1     localhost ip6-localhost ip6-loopback  12   13 # Modified hosts start  14   15 # Armorgames Start  16 93.184.220.39   cache.armorgames.com  17 93.184.220.39   gamemedia.armorgames.com  18 93.184.220.39   quests.armorgames.com  19 93.184.220.39   armatars.armorgames.com   ....(其后省略3000+行)

之后,你懂的。。。

原因

原因是域名解析到IP地址的DNS过程被阻止了,使得域名无法解析到正确的IP地址,进而无法连接到正确的服务器。而DNS解析的过程是先去本地hosts文件中去找有没有对应的缓存,然后再连接DNS服务器,修改hosts中的内容就可以跳过连接DNS服务器解析域名这一步

附1:定时启动脚本

进入/etc文件夹,打开sudo vim crontab

  1 # /etc/crontab: system-wide crontab  2 # Unlike any other crontab you don't have to run the `crontab'  3 # command to install the new version when you edit this file  4 # and files in /etc/cron.d. These files also have username fields,  5 # that none of the other crontabs do.  6   7 SHELL=/bin/sh  8 PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin  9  10 # m h dom mon dow user  command 11 17 *    * * *   root    cd / && run-parts --report /etc/cron.hourly 12 25 6    * * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/    cron.daily ) 13 47 6    * * 7   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/    cron.weekly ) 14 52 6    1 * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/    cron.monthly ) 15 #

在14行之后插入5 12 * * * root bash /etc/lhosts,其中5代表分,12代表小时(以24小时制),后面三列分别为年、月、星期,都为*代表是每年每月的每一天,因此插入的命令代表每天的12点5分以root的身份运行命令bash /etc/lhosts

附2:VIM永久显示行号

运行命令vim ~/.vimrc,在最后添加set nu,保存关闭。注:如果.vimrc文件存在则在最后添加命令,不存在则新建文件。

ubuntu的定时工具Cron

0 0
原创粉丝点击