设置时区为 Asia/Shanghai

设置时区:timedatectl set-timezone Asia/Shanghai

查看时区:timedatectl

#有两种情况

一、 外网环境,向互联网的NTP服务器同步时间

1、 确保ntp服务已安装,并启动

android11 修改ntp服务器 安卓9修改ntp服务器_网络


2、 将ntp配置文件多余的“#”和“空白行”过滤掉重新写入到配置文件

cat /etc/ntp.conf | grep -v "#" | tr -s "\n" >/etc/ntp.conf-01
mv /etc/ntp.conf-01	/etc/ntp.conf

android11 修改ntp服务器 安卓9修改ntp服务器_运维_02


3、 将国外的NTP服务器注释掉,新添加一台NTP服务器(cn.pool.ntp.org)国内的NTP服务器

android11 修改ntp服务器 安卓9修改ntp服务器_android11 修改ntp服务器_03


重启,并查看ntp服务器状态,在进行时间同步

android11 修改ntp服务器 安卓9修改ntp服务器_客户端_04


android11 修改ntp服务器 安卓9修改ntp服务器_服务器_05


ntpdate -u 192.168.44.150 #已非特权端口请求服务器

#这里就已完成时间同步

二、 局域网环境搭建NTP服务器,客户端向指定NTP服务器同步时间
#局域网环境搭建NTP服务器,客户端向指定NTP服务器同步时间

服务端: 192.168.44.150
客户端: 192.168.44.160

1、 服务端配置:
vim /etc/ntp.conf

driftfile /var/lib/ntp/drift
restrict default nomodify notrap nopeer noquery
restrict 127.0.0.1
server 127.127.1.0					#使用本地的时间
fudge  127.127.1.0 stratum 10		#服务器的层级。作为局域网的时间同步
restrict ::1
includefile /etc/ntp/crypto/pwkeys 
/etc/ntp/keys
disable monitor

2、 客户端设置

driftfile /var/lib/ntp/drift
restrict default nomodify notrap nopeer noquery
restrict 127.0.0.1 
server 192.168.44.150				#设置服务端的IP
restrict ::1
includefile /etc/ntp/crypto/pw
keys /etc/ntp/keys
disable monitor

#配置完记得重启ntpd服务

测试:

#在客户端向服务端请求时间同步

android11 修改ntp服务器 安卓9修改ntp服务器_运维_06


#配置成功

三、 详细讲解
配置好NTP服务器时,在客户端配置NTP服务器的地址。NTP客户端会缓慢的向服务器同步时间,直至时间一致,ntpdate -u 服务器地址 命令是直接同步到服务器的当前时间,时钟的跃变,对于某些程序会导致很严重的问题。如果ntp客户端与服务器时间相差太大,就不能平滑同步,这时候就需要使用ntpdate直接同步了。

ntpq  -p				打印服务端列表以及它们的状态摘要。
timedatectl				查询和修改系统时钟及其设置
server    127.127.1.0		本地时间
fudge 	  127.127.1.0 stratum 10		服务器的层级。作为局域网的时间同步服务器,一般设置为10
ntpstat					打印系统时钟同步状态
restrict 192.168.0.0 mask 255.255.255.0		限制只允许192.168.0.0 ~ 192.168.0.254网段的主机访问ntp服务器

 restrict 		可用参数 :
 Ignore			忽略NTP请求
 nomodify	    不允许更改服务端的时间
 notrust 		不信任子网 
 noquery		不提供时间查询服务
 notrap		    不提供陷阱服务 
 nopeer	   	    禁用对等体连接
 kod    		访问违规时发送KoD 包 
 restrict -6 	设置IPV6地址的权限。