您的位置:首页 > 运维架构 > Linux

linux服务器修改时间为北京时间

2018-03-09 21:38 330 查看
删除本地的时间文件,加入上海时区
rm -rf /etc/localtime

ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
系统第一次使用这两个命令之后系统时间变成北京时间了,但是我后面用date -s "2018-03-09 22:25:00" 改了时间之后,又想同步一下改成北京时间,于是又执行上面两行的命令,最后发现时间没有变,时间还是我用date改的那个时间,,我估计可能是存在时区的缓存问题,于是
/usr/sbin/ntpdate ntp1.aliyun.com 命令同步下阿里的时间,发现报错

“the NTP socket is in use, exiting“,这个错误比较好解决,就是说ntpd服务启动了,然后我关掉了service ntpd stop,然后再执行/usr/sbin/ntpdate ntp1.aliyun.com就没问题了,然后时间就正确了.
########################################################################
1、这里说一下怎么把自己的一台服务器当成时间服务器,可以让别的服务器来同步,当然了,首先需要保证当时间服务器的这台机器的时间是正确的。
2、搭建步骤,我这里是centos系统,不同系统可能会不一样
修改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、修改完成开启ntpd服务器
[root@learn ~]# sudo service ntpd start  --开启ntpd服务器
[root@learn ~]# sudo chkconfig ntpd on --设置ntpd服务
4、其他机器可通过ntpdate命令来同步时间
/usr/sbin/ntpdate learn.hadoop1.com --这里的learn.hadoop1.com是我的机器主机名
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: