centos同步服务器时间

集群同步,不同步互联网

查看所有节点ntpd服务状态

sudo systemctl status ntpd
sudo systemctl start ntpd
sudo systemctl is-enabled ntpd

查询自己的网段

ip addr

修改hadoop102的ntp.conf配置文件

sudo vim /etc/ntp.conf

先按i,添加如下

restrict 192.168.56.0 mask 255.255.255.0 nomodify notrap

注释以前,去除互联网时间同步

#server 0.centos.pool.ntp.org iburst
#server 1.centos.pool.ntp.org iburst
#server 2.centos.pool.ntp.org iburst
#server 3.centos.pool.ntp.org iburst

当该节点丢失网络连接,采用本地时间作为时间服务器为集群中的其他节点提供时间同步,添加如下

server 127.127.1.0
fudge 127.127.1.0 stratum 10

按esc, 输入!wq退出

修改hadoop102的/etc/sysconfig/ntpd 文件

vim /etc/sysconfig/ntpd

增加内容如下(让硬件时间与系统时间一起同步)

SYNC_HWCLOCK=yes

按esc, 输入!wq退出

重新启动ntpd服务
sudo systemctl start ntpd

设置ntpd服务开机启动
sudo systemctl enable ntpd

其他机器配置(必须root用户)

(1)关闭所有节点上ntp服务和自启动

systemctl stop ntpd
systemctl disable ntpd

(2)在其他机器配置1分钟与时间服务器同步一次

crontab -e

编写定时任务如下:

*/1 * * * * /usr/sbin/ntpdate hadoop102

(3)修改任意机器时间

date -s "2021-9-11 11:11:11"

(4)将当前时间和日期写入BIOS,避免重启后失效

hwclock -w或者hwclock --systohc

(5)1分钟后查看机器是否与时间服务器同步
date

同步互联网时间

下载 ntpdate,用ntp来实现时间同步
yum install -y ntpdate

调整时区为上海,我也好神奇为什么是上海,也就是北京时间 + 8 区
注:想改其他时区也可以去看看 / usr/share/zoneinfo 目录
cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

先启动,否则会如提示 no server suitable for synchronization found
service ntpd start

使用 NTP 来同步时间
ntpdate us.pool.ntp.org
ntpdate ntp.ntsc.ac.cn

查看状态
service ntpd status

查看当前服务器运行的时区
timedatectl status

查看一下服务器是否有上海时区
timedatectl list-timezones | grep Shanghai

设置服务器的时区为上海时区
timedatectl set-timezone Asia/Shanghai

这个时候再使用这个命令service ntpd start回车
会看到18 Mar 22:53:11 ntpdate[1230]: the NTP socket is in use, exiting

如使用中,要再次同步,先停止
service ntpd stop

手动配置当前时间

timedatectl set-time  "2022-09-22 16:48:00"

来验证下时间
date

定时同步时间(每隔 10 分钟同步时钟)
echo “*/10 * * * * /usr/sbin/ntpdate us.pool.ntp.org | logger -t NTP” >> /tmp/crontab.bak

启动定时任务
crontab /tmp/crontab.bak

其他参考 https://blog.51cto.com/u_11239407/2921519

你可能感兴趣的:(服务器,Java,大数据,服务器,centos,linux)