1)方法一:
Linux Shell脚本更新hosts文件
Linux服务器一般不带图形界面,管理员通常都是通过命令行界面直接操作服务器。在日常的管理运维中,经常需要用到github、gist、githubusercontent等网站上的一些内容,也有时需要用到Google上的一些服务,如Google API之类的。然而这些网站大部分都被GFW防火长城屏蔽掉了,GFW用到的屏蔽手段很多,其中DNS污染就是一种。
在Linux环境下通过各种形式翻墙可能并不是最佳实践,最简单的方法是通过修改/etc/hosts文件避免国内DNS污染。
可用的hosts文件可以通过racaljk/hosts项目获取:
racaljk/hosts
本文只是为Linux提供了一套简单的工具,通过这个Shell脚本结合crontab,可以实现自动更新hosts文件。虽然racaljk/hosts项目中也提供了修改的工具,但自己写一个也是不错的,既可以锻炼自己,又可以学习和备忘。
这里提供一个自用简单粗暴的脚本:
GoogleHosts.sh 密码:uq25
脚本运行参考:Linux中执行shell脚本的4种方法
#!/bin/bash
# Update hosts file
# http://www.jianshu.com/p/443f686a84f6
cd ~
echo 创建hosts保存目录...
mkdir hosts_folder
echo 目录创建完成
cd hosts_folder
echo 下载hosts文件...
wget https://raw.githubusercontent.com/racaljk/hosts/master/hosts
echo 文件下载完成
echo 替换hosts文件...
cp -f ./hosts /etc
echo 替换完成
cd ~
echo 删除hosts保存目录...
rm -rf hosts_folder
echo 任务完成!!
脚本运行效果如下:

2)方法二:
这里推荐一下:老D博客
该站长期更新最新Google、谷歌学术、维基百科、ccFox.info、ProjectH、Battle.NET 、WordPress、Microsoft Live、GitHub、Amazon、Archive、Box.com、Disqus、SoundCloud、inoreader、Feedly、FlipBoard、Twitter、Tumblr、Facebook、Flickr、imgur、Instagram、DuckDuckGo、Ixquick、Yahoo、Google Services、Google apis、Android、Youtube、Google Drive、UpLoad、Appspot、Googl eusercontent、Gstatic、Gmail、Google other、Google Play等hosts。
a.手动更换hosts文件的步骤是这样的:
打开下载hosts文件的网站(例如上面提到的 老D博客);
1.下载hosts 文件
2.定位到hosts 文件所在的系统目录
3.删除(或备份)原hosts 文件
4.把下载的新文件拷贝进来
b.各平台hosts所在文件夹:
Windows 系统hosts位于 C:\Windows\System32\drivers\etc\hosts
Android(安卓)系统hosts位于 /etc/hosts
Mac(苹果电脑)系统hosts位于 /etc/hosts
iPhone(iOS)系统hosts位于 /etc/hosts
Linux系统hosts位于 /etc/hosts
绝大多数Unix系统都是在 /etc/hosts
3)翻墙成功

网友评论