タイムサーバの構築

15217 ワード

一.サーバシステムの設定


1.同期時間


1.1手動同期クラスタの各マシン時間

date -s "2017-03-03 03:03:03"
yum install ntpdate

1.2ネットワーク同期時間

  • crontabは、周期的に実行される命令を設定するためのUnixおよびLinuxの命令である.crontabコマンドにより、指定したシステムコマンドまたはshellスクリプトを一定間隔で実行できます.時間間隔の単位は、分、時間、日、月、週以上の任意の組み合わせであってもよい.
  • crontabインストール:
  • yum install crontabs

  • サービス操作説明:
  • service crond start ##     
    service crond stop ##     
    service crond restart ##     
    service crond reload ##       
    service crond status ##    crontab     :
    chkconfig crond –list ##    crontab             
    chkconfig crond on ##         

    1.2コマンドフォーマット

  • crontab [-u user] file
  • crontab [-u user][ -e | -l | -r ]
  • パラメータの説明:
  • -u user:あるユーザを設定するためのcrontabサービス
  • file:fileはコマンドファイルの名前で、fileをcrontabのタスクリストファイルとしてcrontabにロードすることを示します.
  • -e:ユーザーのcrontabファイルの内容を編集します.ユーザーを指定しない場合は、現在のユーザーのcrontabファイルを編集します.
  • -l:ユーザーのcrontabファイルの内容が表示されます.ユーザーを指定しない場合は、現在のユーザーのcrontabファイルの内容が表示されます.
  • -r:タイミングタスク構成を削除し、/var/spool/cronディレクトリからユーザーのcrontabファイルを削除します.ユーザーを指定しない場合は、現在のユーザーのcrontabファイルをデフォルトで削除します.
  • -i:ユーザーのcrontabファイルを削除するときに確認メッセージを表示します.

  • crontab file[-u user]##現在のcrontabを指定したファイルに置き換えます.
  • crontab-l[-u user]##ユーザーの現在のcrontabをリストする.
  • crontab-e[-u user]##ユーザーの現在のcrontabを編集する.


  • 1.3構成の説明、例

  • * * * * * command
  • 分時日月周命令
  • 第1列は分1~59を示す毎分または/1は
  • を示す.
  • 第2列は時間0~23(0は0点)
  • を示す.
  • 第3列は日付1~31
  • を示す.
  • 第4列は月1~12
  • を示す
  • 第5列標識番号曜日0~6(0は日曜日)
  • 第6列実行するコマンド
  • /1 * date >>/root/date.txt
  • 毎分dateコマンド
  • を実行する
  • 30 21 */usr/local/etc/rc.d/httpd restart
  • 毎晩の21:30にapacheを再起動します.
  • 45 4 1,10,22/usr/local/etc/rc.d/httpd restart
  • 毎月1、10、22日の4:45にapacheを再起動します.
  • 10 1 6,0/usr/local/etc/rc.d/httpd restart
  • 毎週6、日曜日の1:10 apacheを再起動します.
  • 0,30 18-23 */usr/local/etc/rc.d/httpd restart
  • 毎日18:00から23:00の間で30分おきにapacheを再起動します.
  • 23-7/1 */usr/local/etc/rc.d/httpd restart
  • 夜11時から朝7時までの間、1時間おきにapache
  • を再起動します.

    2、ローカルエリアネットワーク時間同期構成


    2.1インストール構成

  • ntpdサービス
  • がインストールされているかどうかを確認します.
  • rpm -qa | grep ntp
  • は、次のコマンドで
  • をインストールしていません.
  • yum install -y ntp
  • インストール完了後、起動
  • から設定.
  • chkconfig ntpd on
  • chkconfig ntpd –list
  • ntpd 0:閉じる1:閉じる2:閉じる3:有効にする4:有効にする5:有効にする6:閉じる構成前にntpdateを使用して手動で同期して時間を落とし、本体と外部時間サーバーの時間差が大きすぎてntpdが正常に同期できないようにします.
  • ntpdate -u 202.112.10.36 22 Dec 16:52:38 ntpdate[6400]: adjust time server 202.112.10.36 offset 0.012135 sec

  • イントラネットNTP-Serverの構成(192.168.1.1.35)
  • 以下は主にイントラネットを構成するNPTDサーバ(192.168.1.1135)であり、NTPDサービス構成コアは/etc/ntpである.confファイルは、配置ができたらOKです.
    
    # 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
    
    # Use public servers from the pool.ntp.org project.
    
    
    # Please consider joining the pool (http://www.pool.ntp.org/join.html).
    
    
    #               : http://www.pool.ntp.org/zone/cn
    
    server 202.112.10.36             # 1.cn.pool.ntp.org
    server 59.124.196.83             # 0.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
    
    
    # allow update time by the upper server
    
    
    #                  
    
    restrict 210.72.145.44 nomodify notrap noquery
    restrict 202.112.10.36 nomodify notrap noquery
    restrict 59.124.196.83 nomodify notrap noquery
    
    # Undisciplined Local Clock. This is a fake driver intended for backup
    
    
    # and when no outside source of synchronized time is available.
    
    
    #            ,           
    
    server  127.127.1.0     # local clock
    fudge   127.127.1.0 stratum 10
    
    # 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
    
  • 修正sudo vi/etc/sysconfig/ntpd追加:SYNC_HWCLOCK=yes
  • プロファイルの変更が完了し、保存が終了し、サービスが開始されます.
    service ntpd start
    起動後、外部タイムサーバと同期を開始するには、一般的に5~10分程度かかります.コマンドでNTPDサービス状況を問い合わせることができます.サービス接続およびリスニング
    netstat -tlunp | grep ntp
    udp        0      0 192.168.1.135:123           0.0.0.0:*          23103/ntpd
    udp        0      0 127.0.0.1:123               0.0.0.0:*           23103/ntpd
    udp        0      0 0.0.0.0:123                 0.0.0.0:*           23103/ntpd
    udp        0      0 fe80::6cae:8bff:fe3d:f65:123 :::*               23103/ntpd
    udp        0      0 fe80::6eae:8bff:fe3d:f65:123 :::*               23103/ntpd
    udp        0      0 ::1:123                     :::*                23103/ntpd
    udp        0      0 :::123                      :::*                23103/ntpd
    
  • の表示
    ntpstatコマンドは、通常、接続と同期に成功するには5~10分かかります.そのため、サーバーが起動したら少し待つ必要があります.起動したばかりの頃は、一般的には:
    # ntpstat
    unsynchronised
    time server re-starting
    polling server every 64 s
          :
    synchronised to NTP server (202.112.10.36) at stratum 3
    time correct to within 275 ms
    polling server every 256 s

    OK、イントラネットのNTPDサービスはすでに構成が完了しており、すべてが正常であれば、イントラネットの他のデバイスとこのサーバを時間同期サービスとして構成し始めます.
    イントラネットNTP-Clientsを構成するには、まずNTPDサービスをインストールし、次に自己起動(NTP-Serverと全く同じ)に構成する必要があります.その後、いずれかの構成/etc/ntpを探します.confファイルは、構成が完了して検証が通過した後、他のクライアントマシンにコピーし、直接使用すればよい.
  • ntpサービスのインストール
  • yum install -y ntp

  • 電源オンの設定
  • chkconfig ntp on

  • 修正/etc/ntp.confファイル構成情報
    driftfile /var/lib/ntp/drift
    restrict 127.0.0.1
    restrict -6 ::1
    
    #                 
    
    server 192.168.1.135
    restrict 192.168.1.135 nomodify notrap noquery
    server  127.127.1.0     # local clock
    fudge   127.127.1.0 stratum 10
    includefile /etc/ntp/crypto/pw
    keys /etc/ntp/keys
  • 修正して保存終了し、サーバを要求する前にntpdateを使用して手動同期してくださいntpdate-u 192.168.0.135 Dec 17:09:57 ntpdate[6439]:adjust time server 192.168.1135 offset 0.004882 sec
  • タイミング同期時間を設定します.ここでは10分間に1回同期するように設定します.
  • rootユーザの下にcrontab-eを追加する必要がある.一般ホストはネットワークの
  • である.
    ## sync cluster time
    0-59/10 * * * * /usr/sbin/ntpdate cn.pool.ntp.org
  • 他のサーバもタイミングタスクを設定し、ホストを同期する時間
  • rootユーザの下でcrontab-e
  • を追加する必要がある.
    ## sync cluster time
    10 * * * * /usr/sbin/ntpdate        IP