更新hosts,sudo:无法解析主机,rcnscd: command not found

时间:2021-08-25 02:40:11

修改hosts后【点击此处方法】,使其生效

sudo rcnscd restart

sudo:无法解析主机:hyc-csu

sudo:rcnscd: command not found

============================

【1】问题:sudo:rcnscd: command not found

解决方案:

安装nscd(Name Service Cache Daemon

sudo apt-get install nscd

启动nscd

sudo /etc/init.d/nscd restart

之后hosts生效

【2】问题:sudo:无法解析主机:hyc-csu

解决方案:

sudo gedit /etc/hosts

添加 127.0.1.1  hyc-csu(主机名$HOSTNAME)

# Localhost (DO NOT REMOVE)
127.0.0.1   localhost
127.0.1.1   hyc-csu
255.255.255.255    broadcasthost
::1 localhost
fe80::1%lo0 localhost

以下是shell脚本,下载hosts后(下载后20分钟内执行该脚本),执行该shell(命令:bash fileName.sh),一键导入hosts,并解决以上问题(如果先执行了sudo apt-get install nscd,安装了name service cache daemon,下面shell中的apt-get install nscd要注释掉)

#!/bin/bash
#author hyc
read -s -p "Please input passwords:" PASSWORD #输入密码
DIR_PATH=`echo ${PASSWORD} | sudo -S find / -name "hosts" -user "${USER}" -amin -20` #查找20分钟内修改的hosts,也就是最新下载的hosts
sed -i '/^127.0.0.1/a 127.0.1.1 '$HOSTNAME'' ${DIR_PATH}
sudo cp ${DIR_PATH} /etc/hosts
sudo apt-get install nscd #安装name service cache daemon
sudo /etc/init.d/nscd restart
echo -e "\ncomplete!"