date// 查看时间
[root@node1 ~]# date
Tue Feb 25 20:15:18 CST 2014
// 修改时间
[root@node1 ~]# date -s "20140225 20:16:00" # yyyymmdd hh:mm:ss
Tue Feb 25 20:16:00 CST 2014
// date 命令支持多种时间格式,详细信息请查看 date --help
hwclockclockclock// 查看硬件时间,可以使用 hwclock,hwclock --show 或 hwclock -r
[root@node1 ~]# hwclock --show
Tue 25 Feb 2014 08:21:14 PM CST -0.327068 seconds// 设置硬件时间
[root@node1 ~]# hwclock --set --date "20140225 20:23:00"
[root@node1 ~]# hwclock
Tue 25 Feb 2014 08:23:04 PM CST -0.750440 seconds
hwclock// 以系统时间为基准,修改硬件时间
[root@node1 ~]# hwclock --systohcntpdate1 [root@node1 ~]# ntpdate [NTP IP/hostname]
2 [root@node1 ~]# ntpdate 192.168.0.1
3 [root@node1 ~]# ntpdate cn.pool.ntp.orgntpdatentpdate -u cn.pool.ntp.org-uno server suitable for synchronization found-uman ntpdate-utime.nist.govntp.fudan.edu.cntime.windows.comasia.pool.ntp.orgcroncrontab0 12 * /usr/sbin/ntpdate 192.168.0.1192.168.0.1/etc/cron.hourly/#!/bin/bash$Id: sync-clock,v 1.6 2009/12/23 15:41:29 jmates Exp $
Use ntpdate to get rough clock sync with department of Genome Sciences
time server.
NTPDATE=/usr/sbin/ntpdate
SERVER="192.168.0.1 "
if running from cron (no tty available), sleep a bit to space
out update requests to avoid slamming a server at a particular time
if ! test -t 0; then
MYRAND=$RANDOM
MYRAND=${MYRAND:=$$}
if [ $MYRAND -gt 9 ]; then
sleep echo $MYRAND | sed 's/.*\(..\)$/\1/' | sed 's/^0//'
fi
fi
$NTPDATE -su $SERVER
update hardware clock on Linux (RedHat?) systems
if [ -f /sbin/hwclock ]; then
/sbin/hwclock --systohc
fi
ntpdntpdatecronntpdatesleeptimerntpdntpdatentpdntpdyum install ntp -y # 安装 NTP 服务获取当前系统的时区,如果时区为 (Shanghai) 则打印,如果不是则利用 || 符号
如果执行失败,则执行 || 后面的命令
timedatectl | grep 'Shanghai' || timedatectl set-timezone Asia/Shanghai # 将当前主机的时区设为 亚洲/上海
systemctl enable ntpd.service # 开机自启动
systemctl restart ntpd.service # 启动 NTP 服务守护程序
firewall-cmd --add-service=ntp
firewall-cmd --add-service=ntp --permanent # 在防火墙上开放 NTP 服务
ntpdntpdntpdatentpdntpdntpdateyum -y install ntpdatevi /var/spool/cron/root(或crontab -e)10 0,8,16 /usr/sbin/ntpdate 10.3.5.5; /sbin/hwclock -w1.txt/var/mail/root0 /8 * /usr/sbin/ntpdate 10.3.5.5 >>/tmp/1.txt; /sbin/hwclock -wcat /etc/sysconfig/ntpdateSYNC_HWCLOCK=yesntpd/etc/ntp.conf/usr/share/zoneinfo//usr/share/zoneinfo/Asia/Shanghai/etc/sysconfig/clock/etc/localtimeclock/usr/share/zoneinfo/Asia/Shanghai/etc/localtime/etc/ntp.conf[root@linux ~]# vi /etc/ntp.conf 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 ::1
restrict 172.16.18.0 mask 255.255.255.0 default nomodify notrap
restrict 192.168.168.0 mask 255.255.255.0 default nomodify notrap
restrict 192.168.57.0 mask 255.255.255.0 default nomodify notrap
允许目标子网同步时间请求,具有默认,不能修改,不能陷阱 等权限
Hosts on local network are less restricted.
restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
Use public servers from the pool.ntp.org project.
server 127.127.1.0 prefer # 设置NTP服务器使用 127.127.1.0 主板BIOS上的时钟为时间源,并且使用 prefer(优先)权限
server 0.centos.pool.ntp.org iburst
server 1.cn.pool.ntp.org iburst # 使用公网上的 NTP 服务器作为时间源服务器
server 192.168.168.100 iburst # 使用内网上的 NTP 服务器作为时间源服务器
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
disable monitor
systemctl restart ntpd.service # 重启 NTP 服务守护程序
timedatectl set-local-rtc falserestrictserverdriftfile[root@node1 ~]# ntpdate cn.pool.ntp.org
25 Feb 21:10:52 ntpdate[9549]: adjust time server 202.112.31.197 offset 0.000101 secntpdsystemctl start ntpdntpd[root@node1 ~]# netstat -ln |grep :123
udp 0 0 12.12.12.100:123 0.0.0.0:
udp 0 0 192.168.0.100:123 0.0.0.0:
udp 0 0 172.18.226.174:123 0.0.0.0:
udp 0 0 10.10.10.100:123 0.0.0.0:
udp 0 0 127.0.0.1:123 0.0.0.0:
udp 0 0 0.0.0.0:123 0.0.0.0:
udp 0 0 fe80::225:90ff:fe98:61ff:123 :::
udp 0 0 fe80::225:90ff:fe98:61fe:123 :::
udp 0 0 fe80::202:c903:1b:afa1:123 :::
udp 0 0 ::1:123 :::
udp 0 0 :::123 :::*[root@node1 ~]# ntpstat
synchronised to NTP server (202.120.2.101) at stratum 4
time correct to within 557 ms
polling server every 64 s该指令可列出NTP服务器是否与上层联机。由上述输出结果可知,时间校正约
为557*10(-6)秒,且每隔64秒会主动更新时间。
unsynchronized time server re-startingpolling server every 64 s
// 或者
25 Apr 15:30:17 ntpdate[11520]: no server suitable for synchronization found
[root@node1 ~] # ntptrace –n
127.0.0.1:stratum 11, offset 0.000000,synch distance 0.95095
222.73.214.125:stratum 2,offset –0.000787,synch distance 0.10857
209.81.9.7:stratum 1,offset 0.000028,synch distance 0.00436,refid 'GPS'这个指令可以列出目前NTP服务器(第一层)与上层NTP服务器(第二层)
彼此之间的关系,注意:该命令需要安装ntp-perl包
ntpq[root@node1 ~]# ntpq -p/etc/sysconfig/ntpd/etc/sysconfig/ntpdSYNC_HWCLOCK=yes