虽说很简单几步,但是每次做添加都添加ntp就很烦,写一个脚本记录下
#!/bin/bash
read -p read -p"请输入主机名: " HOSTNAME
hostnamectl set-hostname $HOSTNAME
echo "nameserver 114.114.114.114" >> /etc/resolv.conf
version=$(cat /etc/redhat-release | awk '{print $4 }' | awk -F '.' '{print $1}')
echo $releasetmp
sleep 5
yum install wget -y
cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-$version.repo
if [[ $? -eq 0 ]];then
echo -e "\033[32m# yum 源已成功更新为 aliyun_repo #\033[0m";
sleep 3;
else
echo -e "\033[31m# yum 源未成功更新为 aliyun_repo #\n3s 后退出...\033[0m";
exit;
fi
wget -P /etc/yum.repos.d/ http://mirrors.aliyun.com/repo/epel-$version.repo
yum clean all && yum makecache && yum update -y
yum update -y
systemctl stop firewalld.service && systemctl disable firewalld.service
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config && setenforce 0
yum -y install ntp lrzsz vim net-tools rsync bash-completion tree screen lsof zip unzip telnet ntp locate
read -p"请输入server NTP ip: " SERVERIP
CLIENTIP=$(ifconfig $( ip route show | awk '/default/ { print $5 }' ) |sed -n 2p |awk -F ' ' '{print$2}')
CLENTPGET=$(ip route show | awk -F '/' ' NF>1{ print $1 }')
CLENTMASK=$(ifconfig $( ip route show | awk '/default/ { print $5 }' ) |sed -n 2p |awk -F ' ' '{print$4}')
sed -i 's/restrict default nomodify notrap nopeer noquery/#restrict default nomodify notrap nopeer noquery/g' /etc/ntp.conf
sed -i 's/restrict 127.0.0.1/restrict '"$CLIENTIP"' nomodify notrap nopeer noquery/g' /etc/ntp.conf
sed -i 's/restrict ::1/restrict '"$CLENTPGET"' mask '"$CLENTMASK"' nomodify notrap/g' /etc/ntp.conf
sed -i 's/server 0.centos.pool.ntp.org iburst/#server 0.centos.pool.ntp.org iburst/g' /etc/ntp.conf
sed -i 's/server 1.centos.pool.ntp.org iburst/#server 1.centos.pool.ntp.org iburst/g' /etc/ntp.conf
sed -i 's/server 2.centos.pool.ntp.org iburst/server '"$SERVERIP"'/g' /etc/ntp.conf
sed -i 's/server 3.centos.pool.ntp.org iburst/Fudge '"$SERVERIP"' stratum 10/g' /etc/ntp.conf
systemctl restart ntpd && systemctl enable ntpd