2022-12-18 13:24:47 +08:00
|
|
|
|
#!/bin/bash
|
2022-12-18 13:29:13 +08:00
|
|
|
|
#by spiritlhl
|
|
|
|
|
#from https://github.com/spiritLHLS/one-click-installation-script
|
|
|
|
|
#version: 2022.12.18
|
|
|
|
|
|
|
|
|
|
red(){ echo -e "\033[31m\033[01m$1$2\033[0m"; }
|
|
|
|
|
green(){ echo -e "\033[32m\033[01m$1$2\033[0m"; }
|
|
|
|
|
yellow(){ echo -e "\033[33m\033[01m$1$2\033[0m"; }
|
|
|
|
|
reading(){ read -rp "$(green "$1")" "$2"; }
|
|
|
|
|
|
|
|
|
|
head() {
|
|
|
|
|
# 支持系统:Ubuntu 12+,Debian 6+
|
|
|
|
|
ver="2022.12.18"
|
2022-12-18 13:30:49 +08:00
|
|
|
|
changeLog="修复sudo: unable to resolve host警告"
|
2022-12-18 13:29:13 +08:00
|
|
|
|
clear
|
|
|
|
|
echo "#######################################################################"
|
2022-12-18 13:30:49 +08:00
|
|
|
|
echo "# ${YELLOW}修复sudo: unable to resolve host xxx: Name or service not known警告${PLAIN} #"
|
2022-12-18 13:29:13 +08:00
|
|
|
|
echo "# 版本:$ver #"
|
|
|
|
|
echo "# 更新日志:$changeLog #"
|
|
|
|
|
echo "# ${GREEN}作者${PLAIN}: spiritlhl #"
|
|
|
|
|
echo "# ${GREEN}作仓库${PLAIN}: https://github.com/spiritLHLS/one-click-installation-script #"
|
|
|
|
|
echo "#######################################################################"
|
|
|
|
|
echo "支持系统:Ubuntu 18+,Debian 8+,centos 7+,Fedora,Almalinux 8.5+"
|
|
|
|
|
echo "检测修复sudo: unable to resolve host xxx: Name or service not known爆错"
|
|
|
|
|
# Display prompt asking whether to proceed with fixing
|
|
|
|
|
reading "Do you want to proceed with fixing? [y/n] " confirm
|
|
|
|
|
echo ""
|
|
|
|
|
|
|
|
|
|
# Check user's input and exit if they do not want to proceed
|
|
|
|
|
if [ "$confirm" != "y" ]; then
|
|
|
|
|
exit 0
|
|
|
|
|
fi
|
|
|
|
|
}
|
2022-12-18 13:24:47 +08:00
|
|
|
|
|
|
|
|
|
# Check if the hostname is set correctly in /etc/hosts
|
|
|
|
|
HOSTNAME=$(cat /etc/hostname)
|
|
|
|
|
HOSTS_LINE="$(grep $HOSTNAME /etc/hosts)"
|
|
|
|
|
|
|
|
|
|
if [ -z "$HOSTS_LINE" ]; then
|
|
|
|
|
# Hostname not found in /etc/hosts. Add it.
|
2022-12-18 13:29:13 +08:00
|
|
|
|
yellow "Updating /etc/hosts with hostname: $HOSTNAME"
|
2022-12-18 13:24:47 +08:00
|
|
|
|
sudo bash -c "echo '127.0.0.1 $HOSTNAME' >> /etc/hosts"
|
|
|
|
|
else
|
|
|
|
|
# Hostname found in /etc/hosts. Check if the IP address is correct.
|
|
|
|
|
HOSTS_IP="$(awk '{print $1}' <<<$HOSTS_LINE)"
|
|
|
|
|
if [ "$HOSTS_IP" != "127.0.0.1" ]; then
|
|
|
|
|
# IP address is incorrect. Update it.
|
2022-12-18 13:29:13 +08:00
|
|
|
|
yellow "Updating IP address for $HOSTNAME in /etc/hosts"
|
2022-12-18 13:24:47 +08:00
|
|
|
|
sudo sed -i "s/$HOSTS_IP/127.0.0.1/g" /etc/hosts
|
|
|
|
|
else
|
|
|
|
|
# Hostname and IP address are correct. No changes needed.
|
2022-12-18 13:29:13 +08:00
|
|
|
|
green "Hostname and IP address in /etc/hosts are correct."
|
2022-12-18 13:24:47 +08:00
|
|
|
|
fi
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
# Check if the sudo command works
|
2022-12-18 13:29:13 +08:00
|
|
|
|
sudo yellow "Testing sudo command..."
|
2022-12-18 13:24:47 +08:00
|
|
|
|
if [ $? -eq 0 ]; then
|
|
|
|
|
# Sudo command works. Exit the script.
|
2022-12-18 13:29:13 +08:00
|
|
|
|
green "Fix successful. Exiting script."
|
2022-12-18 13:24:47 +08:00
|
|
|
|
else
|
|
|
|
|
# Sudo command failed. Try restarting the networking interface.
|
2022-12-18 13:29:13 +08:00
|
|
|
|
yellow "Sudo command still failing. Restarting networking interface."
|
2022-12-18 13:24:47 +08:00
|
|
|
|
sudo service networking restart
|
|
|
|
|
|
|
|
|
|
# Check if the sudo command works after restarting the networking interface
|
|
|
|
|
sudo echo "Testing sudo command after restarting networking interface..."
|
|
|
|
|
if [ $? -eq 0 ]; then
|
|
|
|
|
# Sudo command works. Exit the script.
|
2022-12-18 13:29:13 +08:00
|
|
|
|
green "Fix successful. Exiting script."
|
2022-12-18 13:24:47 +08:00
|
|
|
|
else
|
|
|
|
|
# Sudo command failed. Try restarting the DNS server.
|
2022-12-18 13:29:13 +08:00
|
|
|
|
yellow "Sudo command still failing. Restarting DNS server."
|
2022-12-18 13:24:47 +08:00
|
|
|
|
sudo service bind9 restart
|
|
|
|
|
|
|
|
|
|
# Check if the sudo command works after restarting the DNS server
|
|
|
|
|
sudo echo "Testing sudo command after restarting DNS server..."
|
|
|
|
|
if [ $? -eq 0 ]; then
|
|
|
|
|
# Sudo command works. Exit the script.
|
2022-12-18 13:29:13 +08:00
|
|
|
|
green "Fix successful. Exiting script."
|
2022-12-18 13:24:47 +08:00
|
|
|
|
else
|
|
|
|
|
# Sudo command still failing. Exiting script.
|
2022-12-18 13:29:13 +08:00
|
|
|
|
red "Unable to fix problem. Exiting script."
|
2022-12-18 13:24:47 +08:00
|
|
|
|
fi
|
|
|
|
|
fi
|
|
|
|
|
fi
|