文章转载于:
1、https://blog.csdn.net/u010316188/article/details/79503376?utm_source=blogxgwz9
2、https://blog.csdn.net/weixin_35852328/article/details/79506453
1、手动设置
Linux的时间分为System Clock(系统时间)和Real Time Clock (硬件时间,简称RTC)。
系统时间:指当前Linux Kernel中的时间。
硬件时间:主板上有电池供电的时间。
查看系统时间的命令: #date
设置系统时间的命令: #date –set(月/日/年 时:分:秒)
例:#date –set “10/11/10 10:15”
查看硬件时间的命令: # hwclock
设置硬件时间的命令: # hwclock –set –date = (月/日/年 时:分:秒)
上述提到的是手动设置时间到一个时间点,可能与当前网络的时间有误差。
2、与时间服务器上的时间同步
- 安装ntpdate工具
2、设置时间为阿里服务器的时间
ntpdate ntp1.aliyun.com
3、将系统时间写入硬件时间
hwclock –systohc
3、把自己的一台服务器当成时间服务器,可以让别的服务器来同步
搭建步骤,我这里是centos系统,不同系统可能会不一样
3.1、修改vi /etc/ntp.conf 文件,可参考
[root@learn ~]# cat /etc/ntp.conf
# For more information about this file, see the man pages
# ntp.conf(5), ntp_acc(5), ntp_auth(5), ntp_clock(5), ntp_misc(5), ntp_mon(5).
driftfile /var/lib/ntp/drift
# Permit time synchronization with our time source, but do not
# permit the source to query or modify the service on this system.
restrict default kod nomodify notrap nopeer noquery
restrict -6 default kod nomodify notrap nopeer noquery
# Permit all access over the loopback interface. This could
# be tightened as well, but to do so would effect some of
# the administrative functions.
restrict 127.0.0.1
restrict -6 ::1
# Hosts on local network are less restricted.
#restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
restrict 192.168.37.255 mask 255.255.255.0 nomodify notrap
##开启上面这一行,去掉注释, 这里192.168.37.255是你电脑广播地址,在ifconfig命令中能看到【inet addr:192.168.37.141 Bcast:192.168.37.255 Mask:255.255.255.0】
#Use public servers from the pool.ntp.org project.
#Please consider joining the pool (http://www.pool.ntp.org/join.html).
#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
#由于是把自己电脑机器当成ntpd服务器,不需要连接外网,所以注释下上面四行, 添加下面两行,
server 127.127.1.0 #local clock
fudge 127.127.1.0 startnum 10
#broadcast 192.168.1.255 autokey # broadcast server
#broadcastclient # broadcast client
#broadcast 224.0.1.1 autokey # multicast server
#multicastclient 224.0.1.1 # multicast client
#manycastserver 239.255.254.254 # manycast server
#manycastclient 239.255.254.254 autokey # manycast client
# Enable public key cryptography.
#crypto
includefile /etc/ntp/crypto/pw
# Key file containing the keys and key identifiers used when operating
# with symmetric key cryptography.
keys /etc/ntp/keys
# Specify the key identifiers which are trusted.
#trustedkey 4 8 42
# Specify the key identifier to use with the ntpdc utility.
#requestkey 8
# Specify the key identifier to use with the ntpq utility.
#controlkey 8
# Enable writing of statistics records.
#statistics clockstats cryptostats loopstats peerstats
---------------------
3.2、修改完成开启ntpd服务器
[root@learn ~]# sudo service ntpd start --开启ntpd服务器
[root@learn ~]# sudo chkconfig ntpd on --设置ntpd服务
3.3、其他机器可通过ntpdate命令来同步时间
/usr/sbin/ntpdate learn.hadoop1.com --这里的learn.hadoop1.com是我的机器主机名