欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  网络运营

script_tool_for_linux.bash: Linux 环境下的 hosts 一键部署脚本

程序员文章站 2022-04-25 15:31:41
linux 环境下的 hosts 一键部署脚本,由 @lstoars 贡献; @fluviusmagnus 提供增强版本。 官方网站: #!/bin/sh #...

linux 环境下的 hosts 一键部署脚本,由 @lstoars 贡献; @fluviusmagnus 提供增强版本。

官方网站:

#!/bin/sh
#
# script_tool_for_linux
#
# use command: `sudo sh script_tool_for_linux.sh` or
#    `su -c 'sh script_tool_for_linux.sh'`
# to update your hosts file.
#
# warning: the script can not replace others' hosts rules.
#   if you have hosts rules provided by others, you may get conflict.
#
if [ `id -u` -eq 0 ]; then
 curl -flo /tmp/fetchedhosts 'https://raw.githubusercontent.com/racaljk/hosts/master/hosts'
 sed -i '/# copyright (c) 2014/,/# modified hosts end/d' /etc/hosts

 sed -i "s/localhost/`hostname`/g" /tmp/fetchedhosts

 cat /tmp/fetchedhosts >> /etc/hosts
 rm -f /tmp/fetchedhosts

 echo 'success.'
else
 echo 'permission denied, are you root?'
fi