Hadoop.2.x_时间服务器搭建(CentOs6.6)

一、检查linux系统NTP是否被安装

[liuwl@hadoop09-linux-01 ~]$ 
[liuwl@hadoop09-linux-01 ~]$ rpm -qa | grep ntp
ntpdate-4.2.6p5-10.el6.centos.1.x86_64
ntp-4.2.6p5-10.el6.centos.1.x86_64

  ☹ 如果没有安装

#方案一:
[liuwl@hadoop09-linux-01 ~]$ which ntpd
/usr/sbin/ntpd
[liuwl@hadoop09-linux-01 ~]$ rpm -qf /usr/sbin/ntpd
ntp-4.2.6p5-10.el6.centos.1.x86_64
[liuwl@hadoop09-linux-01 ~]$ which ntpdate
/usr/sbin/ntpdate
[liuwl@hadoop09-linux-01 ~]$ rpm -qf /usr/sbin/ntpdate
ntpdate-4.2.6p5-10.el6.centos.1.x86_64
#找到相应安装包版本rpm 安装即可
rpm -ivh ntp-4.2.6p5-10.el6.centos.1.x86_64.rpm
rpm -ivh ntpdate-4.2.6p5-10.el6.centos.1.x86_64.rpm
#实在不行就使用反查命令,要确保联网 
yum whatprovides */ntp
#或直接使用yum安装
yum -y install ntp
yum -y install ntpdate
关于rpm或yum安装:http://www.cnblogs.com/eRrsr/p/5851411.html 

二、使用命令安装ntp后,/etc/下会有ntp.conf文件

 1. 同步当前系统时间和写入BOIS:

# 同步外网时间服务器到系统时间
ntpdate -u 202.112.10.36
# 同步系统时间到硬件
hwclock -w
# 需要注意的是
    1. 输入命令后显示no server... 
         原因可能是->网络问题(ping时间服务器),加-u测试
    2. 提示正常,但时间设置未变,一般linux不会出这种问题,这个问题原因(我这的原因): 时区不正确,一般设置就是Asia/Shanghai,而我的是美国时区,使用命令:
    cp /usr/zoneinfo/Asia/Shanghai /etc/localtime # ln -sf ... 或创建软连接
    然后修改一下clock文件
    vi /etc/sysconfig/clock 
    ZONE="Asia/Shanghai"
    再次ntpdate设置时间

 2. 修改/etc/ntp.conf文件:

1. 去掉#,并修改成自己的网关
    restrict 10.0.0.1 mask 255.255.255.0 nomodify notrap
2. 注释(可以不注释或添加外网时间服务器)
    #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
3. 配置在无网络下使用本机为时间服务器
    server 127.127.1.0 
    fudge 127.127.1.0 stratum 10
4. 配置完毕后保存退出,重启ntpd服务,如果已经打开的话

 3. 配置启动服务和开机自启(标注# ◆ 是我走的步骤)

# 临时启动
[liuwl@hadoop09-linux-01 ~]$ sudo service ntpd start                     # ◆
# 关闭ntpd服务
[liuwl@hadoop09-linux-01 ~]$ sudo service ntpd stop
# 重启ntpd服务
[liuwl@hadoop09-linux-01 ~]$ sudo service ntpd restart
# 开机自启ntpd服务
[liuwl@hadoop09-linux-01 ~]$ sudo chkconfig ntpd on                      # ◆
# 查看开机自启有哪些服务(查看ntpd是否正常配置) [liuwl@hadoop09-linux-01 ~]$ sudo chkconfig  # ◆
ntpd 0:off 1:off 2:on 3:on 4:on 5:on 6:off

 4. 防火墙配置(标注# ◆ 是我走的步骤)

# 临时启动防火墙
[liuwl@hadoop09-linux-01 ~]$ sudo service iptables start
# 关闭防火墙
[liuwl@hadoop09-linux-01 ~]$ sudo service iptables stop      
# 开机关闭防火墙
[liuwl@hadoop09-linux-01 ~]$ sudo chkconfig iptables on                    # ◆
# 当然也可以开放防火墙123端口
iptables -A INPUT -p UDP -i eno16777736 -s 192.168.1.0/24 --dport 123 -j ACCEPT
#SELinux设置
setsebool -P ntp_disable_trans 1
#允许BIOS与系统时间同步,添加下面一行或使用命令:hwclock -w
vi /etc/sysconfig/ntpd    
SYNC_HWCLOCK=yes                                                        # ◆

 5. 检测NTP服务是否运行

[liuwl@hadoop09-linux-01 ~]$ sudo netstat -tlunp | grep ntp              
udp        0      0 10.0.0.108:123              0.0.0.0:*                               2366/ntpd           
udp        0      0 127.0.0.1:123               0.0.0.0:*                               2366/ntpd           
udp        0      0 0.0.0.0:123                 0.0.0.0:*                               2366/ntpd           
udp        0      0 fe80::20c:29ff:fece:ec86:123 :::*                                   2366/ntpd           
udp        0      0 ::1:123                     :::*                                    2366/ntpd           
udp        0      0 :::123                      :::*                                    2366/ntpd  

 6. 查看ntp服务器与上层ntp的状态

[liuwl@hadoop09-linux-01 ~]$ ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
*LOCAL(0)        .LOCL.          10 l   24   64  377    0.000    0.000   0.000
remote   - 本机和上层ntp的ip或主机名,“+”表示优先,“*”表示次优先
refid    - 参考上一层ntp主机地址
st       - stratum阶层
when     - 多少秒前曾经同步过时间
poll     - 下次更新在多少秒后
reach    - 已经向上层ntp服务器要求更新的次数
delay    - 网络延迟
offset   - 时间补偿
jitter   - 系统时间与bios时间差

 7. 客户端配置计划任务同步系统时间和硬件时间

vi /etc/crontab                                           
01 00 * * * root /usr/sbin/ntpdate -u hadoop09-linux-01.ibeifeng.com; /sbin/hwclock -w
# 规定每晚00:01分执行任务,hadoop09-linux-01.ibeifeng.com为刚才设置的时间服务器   
# 查看规定的任务(-e添加新任务,-r为删除)
crontab -l                                              
posted @ 2016-10-19 22:12  eRrsr  阅读(398)  评论(0编辑  收藏  举报