Ubuntu系统下ntp服务器搭建2 | 迟思堂工作室
A-A+

Ubuntu系统下ntp服务器搭建2

2015-02-23 20:30 GNU/Linux系统 暂无评论 阅读 5,963 次

上一篇文章主要讲NTP服务器的,在使用过,还遇到其它和时间有关的问题,本篇文章写写在同步时间和改时区方面的笔记。

时区修改

错误的时区:

latelee@ubuntu:~/kernel$ date Thu Aug 21 18:18:29 PDT 2014

使用tzselect选择时区:

latelee@ubuntu:~/kernel$ tzselect Please identify a location so that time zone rules can be set correctly. Please select a continent or ocean. 1) Africa 2) Americas 3) Antarctica 4) Arctic Ocean 5) Asia 6) Atlantic Ocean 7) Australia 8) Europe 9) Indian Ocean 10) Pacific Ocean 11) none - I want to specify the time zone using the Posix TZ format. 。。。 Please select one of the following time zone regions. 1) east China - Beijing, Guangdong, Shanghai, etc. 2) Heilongjiang (except Mohe), Jilin 3) central China - Sichuan, Yunnan, Guangxi, Shaanxi, Guizhou, etc. 4) most of Tibet & Xinjiang 5) west Tibet & Xinjiang #? 1 The following information has been given: China east China - Beijing, Guangdong, Shanghai, etc. Therefore TZ='Asia/Shanghai' will be used. Local time is now: Fri Aug 22 09:17:17 CST 2014. Universal Time is now: Fri Aug 22 01:17:17 UTC 2014. Is the above information OK? 1) Yes 2) No #? 1 # sudo cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime latelee@ubuntu:~/kernel$ date Fri Aug 22 09:19:40 CST 2014

其实,如果知道了所在时区用的是哪一个文件,直接使用cp命令将时区文件复制为/etc/localtime就行了。

NTP同步时间

同步时间命令很简单,如下:

latelee@ubuntu:~/kernel$ sudo ntpdate cn.pool.ntp.org latelee@ubuntu:~/kernel$ date Mon Aug 25 15:53:55 CST 2014

除了用域名的方式,更多的是用IP地址的方式,如上海交大NTP服务器地址是202.120.2.101,国家授时中心服务器地址是210.72.145.44(这个服务器时灵时不灵,亲测所得经验)。命令和上面一样:

ntpdate -u 202.120.2.101

使用ntp命令更新后的时间只是同步到系统,如果要永久生效,还要写到rtc中:

latelee@ubuntu:~/kernel$ sudo hwclock -w --utc

在我所构建的根文件系统中,需要为应用层程序提供多时区的支持,同时为了方便应用程序代码编写,对于时间文件,并不像通用系统采用的时间文件,而是统一使用GMT-8这类的文件,比如东八区,很多国家也用,如果只用Shanghai这一文件表示,则其它东南亚国家怎么办?需要将这些国家名称一一写出来,代码又显示十分繁冗。前面提到的这些文件位于/usr/share/zoneinfo/Etc目录下。需要注意的是,这些文件名称并不代表实际的时区,比如文件GMT-8实际表示的是是东八区的时区,这点网上资料比较少。一个解释如下,可算是官方的了:
http://www.linuxquestions.org/questions/linux-software-2/is-the-linux-gmt-utc-definition-inverted-779838/
我所查的资料核心内容摘录如下:

The offset specifies the time value you must add to the local time to get a Coordinated Universal Time value. It has syntax like [+|-]hh[:mm[:ss]]. This is positive if the local time zone is west of the Prime Meridian and negative if it is east. The hour must be between 0 and 24, and the minute and seconds between 0 and 59. For example, here is how we would specify Eastern Standard Time, but without any Daylight Saving Time alternative: EST+5

在我所参与的项目中,遇到一个bug,修改时区时,就会更改时间文件的内容。比如,我在上位机将东八区改为东六区后,这里东八区的GMT-8文件的内容被改为东六区的时区内容——注意,是文件内容,不是文件名称。后来才发现,原来有一个地方是不必要的,如下:

cp -f /usr/share/zoneinfo/Etc/GMT-8 /usr/share/zoneinfo/localtime

需要说明的是这个做法是以前项目的,而以前项目用的根文件系统都是基于内存的系统,系统运行时,随便改,都没问题,但这个项目却是存储在硬盘上的,一点改动,都将记录、保存下来。这也需要我们在实践时注意某些平台的不同所带来的问题。有时候,细节真的能决定很多东西,而不是像人们所说的:这不是大问题。

李迟,完成于2015年年初,发表于已未年正月初五晚上





如果本文对阁下有帮助,不妨赞助笔者以输出更多好文章,谢谢!
donate



标签:

给我留言