ntp

1.ntp

   centos7.2 修改时区      #    tzselect  4  - 9 - 1

   hwclock --show   查看系统时间

   使用rpm检查ntp包是否安装

rpm -q ntp
ntp-4.2.6p5-25.el7.centos.2.x86_64

yum -y install ntp
systemctl enable ntpd
systemctl start ntpd

# chkconfig ntpd on

# chkconfig --list ntpd

——————————————————

修改/etc/ntp.conf文件,红色字体是修改的内容

# the administrative functions.
restrict 127.0.0.1
restrict ::1

# Hosts on local network are less restricted.
#restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
restrict 192.168.0.0 mask 255.255.0.0 nomodify notrap   限制访问

# 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 2.cn.pool.ntp.org
server 1.asia.pool.ntp.org
server 2.asia.pool.ntp.org

#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
# 允许上层时间服务器主动修改本机时间
restrict 2.cn.pool.ntp.org nomodify notrap noquery
restrict 1.asia.pool.ntp.org nomodify notrap noquery
restrict 2.asia.pool.ntp.org nomodify notrap noquery

server 127.0.0.1      # local clock
fudge 127.0.0.1 stratum 10

# Enable public key cryptography.
#crypto

includefile /etc/ntp/crypto/pw

————————————

修改完成后重启ntpd服务    

systemctl restart ntpd

查看

 ntpstat 

客户端设置
同样 先安装 ntp
vim /etc/ntp.conf文件

#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 192.168.1.1 iburst

systemctl restart ntpd

启动后,查看同步情况

# ntpq -p
# ntpstat

另一个方法 

date

ntpdate 192.168.0.1

crontab -e
00 01 * * * root /usr/sbin/ntpdate 192.168.0.1; /sbin/hwclock -w         每天晚上1点同步。

格式 00 01 * * *  五个字符表示  分 时 日 月 年

国内常用的时间服务器列表:

  1. 210.72.145.44  (国家授时中心服务器IP地址)  
  2. ntp.sjtu.edu.cn 202.120.2.101 (上海交通大学网络中心NTP服务器地址)  
  3. s1a.time.edu.cn 北京邮电大学  
  4. s1b.time.edu.cn 清华大学  
  5. s1c.time.edu.cn 北京大学  
  6. s1d.time.edu.cn 东南大学  
  7. s1e.time.edu.cn 清华大学  
  8. s2a.time.edu.cn 清华大学  
  9. s2b.time.edu.cn 清华大学  
  10. s2c.time.edu.cn 北京邮电大学  
  11. s2d.time.edu.cn 西南地区网络中心  
  12. s2e.time.edu.cn 西北地区网络中心  
  13. s2f.time.edu.cn 东北地区网络中心  
  14. s2g.time.edu.cn 华东南地区网络中心  
  15. s2h.time.edu.cn 四川大学网络管理中心  
  16. s2j.time.edu.cn 大连理工大学网络中心  
  17. s2k.time.edu.cn CERNET桂林主节点  
  18. s2m.time.edu.cn 北京大学  
在配置前,先使用ntpdate手动同步下时间,免得本机与外部时间服务器时间差距太大,让ntpd不能正常同步

ntpdate -u ntp.api.bz
netstat -tnulp | grep ntp 查看 123 端口

Linux修改时区的正确方法

CentOS和Ubuntu的时区文件是/etc/localtime,但是在CentOS7以后localtime以及变成了一个链接文件

[root@centos7 ~]# ll /etc/localtime 
lrwxrwxrwx 1 root root 33 Oct 12 11:01 /etc/localtime -> /usr/share/zoneinfo/Asia/Shanghai

如果采用直接cp的方法修改系统时区,那么就会把它所链接的文件修改掉,例如把美国的时区文件内容修改成了上海的时区内容,有可能会导致有些编程语言或程序在读取系统时区的时候发生错误,因此正确的修改方法是:

CentOS6、Ubuntu16

# cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

CentOS7、RHEL7、Scientific Linux 7、Oracle Linux 7

最好的方法是使用timedatectl命令

# timedatectl list-timezones |grep Shanghai    #查找中国时区的完整名称
Asia/Shanghai
# timedatectl set-timezone Asia/Shanghai    #其他时区以此类推

或者直接手动创建软链接

# ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime







原文地址:https://www.cnblogs.com/jjp816/p/8930128.html