#!/bin/bash
###Script by tuanna@lowendviet.com
###Version 1.0@20220618
echo -e "Copyright (c)2022 by https://lowendviet.com.\nThis script will set time for linux to network time.\n\n"
if [ -f /etc/os-release ]; then
    # freedesktop.org and systemd
    . /etc/os-release
    OS=$NAME
    VER=$VERSION_ID
elif type lsb_release >/dev/null 2>&1; then
    # linuxbase.org
    OS=$(lsb_release -si)
    VER=$(lsb_release -sr)
elif [ -f /etc/lsb-release ]; then
    # For some versions of Debian/Ubuntu without lsb_release command
    . /etc/lsb-release
    OS=$DISTRIB_ID
    VER=$DISTRIB_RELEASE
elif [ -f /etc/debian_version ]; then
    # Older Debian/Ubuntu/etc.
    OS=Debian
    VER=$(cat /etc/debian_version)
elif [ -f /etc/SuSe-release ]; then
    # Older SuSE/etc.
    ...
elif [ -f /etc/redhat-release ]; then
    # Older Red Hat, CentOS, etc.
    ...
else
    # Fall back to uname, e.g. "Linux <version>", also works for BSD, etc.
    OS=$(uname -s)
    VER=$(uname -r)
fi

if [ "$OS" = "CentOS Linux" ]; then
  #CentOS
  yum install ntp ntpdate -y
elif [ "$OS" = "Ubuntu" ]; then
  #Ubuntu
  apt-get install ntp ntpdate -y
fi

systemctl start ntpd
systemctl enable ntpd
ntpdate -u -s 103.130.217.41 103.130.217.42 time.google.com
systemctl restart ntpd
timedatectl
