Cài đặt ntp server trên windows server 2012

November 10, 2020 by

Lượt xem: 178

Cài đặt ntp server trên windows server 2012

* Cảnh báo vài ý trước khi vào nội dung chính mình cần chia sẻ :

  • 1. Bài viết này mang tính chất là dành cho cá nhân để lưu trữ kiến thức cũng như kinh nghiệm bản thân đã trải qua. || This article is intended for individuals to store knowledge as well as personal experiences.
  • 2. Bài viết chia sẻ phi lợi nhuân. || Post sharing non-profit.
  • 3. Bài viết có thể làm vài bạn sẽ biểu môi rằng đơn giản thế này ai chẳng biết, post lên làm gì ==> thì nhìn lại mục “1” nhé. || The article can do some of you will demonstrate that this simple one does not know, what to post ==> then look at the “1” section.
  • 4. Bài viết có lấy hình ảnh và một vài nội dung trên internet, nên nếu có gì vi phạm, cảm phiền các bạn báo lại giúp mình. || The article has taken pictures and some content on the internet, so if something breaks, please report it for me.

NTP: Configure NTP Client

*** NOTED ***

  • Cấu hình máy trạm sử dụng NTP để đồng bộ thời gian với máy chủ NTP || Configure NTP Client to Synchronize time with NTP Server
  • Cấu hình mặc định của máy trạm NTP đã đồng bộ thông tin với [time.windows.com], vậy nếu hệ thống có kết nối Internet, thì thời gian sẽ luôn luôn được đồng nhất. || NTP Client setting is configured with NTP Server [time.windows.com] by Windows default, so if Internet connected enviroment computer, time and date is already synchronizing.

Bước 1: khởi chạy PowerShell với quyền Adminitrator và cấu hình như bên dưới.
Step 1: Run PowerShell with Administrator Privilege and Configure as following:

Windows PowerShell
Copyright (C) 2016 Microsoft Corporation. All rights reserved.

# confirm current synchronization NTP Server
PS C:\Users\Administrator> w32tm /query /source 
time.windows.com,0x8

# change target NTP Server (replace to your timezone server)
# number means
# 0x01 : SpecialInterval
# 0x02 : UseAsFallbackOnly
# 0x04 : SymmetricActive
# 0x08 : NTP request in Client mode
PS C:\Users\Administrator> Set-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Services\w32time\Parameters" -Name "NtpServer" -Value "ntp.nict.jp,0x8" 

# restart Windows Time service
PS C:\Users\Administrator> Restart-Service w32Time 

# re-sync manually
PS C:\Users\Administrator> w32tm /resync 
Sending resync command to local computer
The command completed successfully.

# verify status
PS C:\Users\Administrator> w32tm /query /status 

Leap Indicator: 0(no warning)
Stratum: 4 (secondary reference - syncd by (S)NTP)
Precision: -23 (119.209ns per tick)
Root Delay: 0.0252246s
Root Dispersion: 0.0824040s
ReferenceId: 0x85F3EEF3 (source IP:  133.243.238.243)
Last Successful Sync Time: 9/23/2019 10:15:33 PM
Source: ntp.nict.jp,0x8
Poll Interval: 8 (256s)

Bước 2: nếu một máy trạm đã tham gia vào môi trường AD của doanh nghiệp, và máy đó là một máy trạm GỐC trong hệ thống, thì thời gian mặc định của máy trạm đã theo cấu hình [CMOS time] tại BIOS, nếu bạn muốn điều chỉnh việc này thì vui lòng theo hướng dẫn bên dưới
Step 2: if a Computer is in Active Directory Domain Enviroment and he is Forest Root, synchronization target is often configured to [Local CMOS Clock], then, if you want change setting from [Local CMOS Clock] to other NTP Server, please setting as following.


Windows PowerShell
Copyright (C) 2016 Microsoft Corporation. All rights reserved.

# in AD Domain Environment, [Type] is set to [NT5DS]
PS C:\Users\Administrator> (Get-Item -Path "HKLM:\SYSTEM\CurrentControlSet\Services\w32time\Parameters").GetValue("Type") 
NT5DS

# if target is [Local CMOS Clock] but you'd like to change it, change [Type] to [NTP] first
# next, change to NTP server with the same way in [1] section
PS C:\Users\Administrator> Set-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Services\w32time\Parameters" -Name "Type" -Value "NTP" 

DONE 🙂 – Have a good day and enjoy !!!

Cài đặt ntp server trên windows server 2012

Reader Interactions

NTP (Network Time Protocol) là một giao thức dùng để đồng bộ hóa thời gian giữa các máy chủ với nhau, đặc biệt là các hệ thống tài chính, dịch vụ, khoa học… thì nhu cầu này đặc biệt quan trọng.

  • Mô hình
  • Cấu hình
    • NTP Server
    • NTP Client (Linux)
    • NTP Client (Windows)
  • Tham khảo

Giao thức NTP phân chia các time server ra thành nhiều lớp (stratum), số stratum càng nhỏ thì thời gian càng chính xác (stratum nhỏ nhất là 0). Các server có stratum bằng 0 sẽ cung cấp thời gian chuẩn cho các client có stratum là 1, các client không thể truy cập vượt cấp stratum của mình (stratum 4 chỉ có thể truy cập stratum 3 chứ không thể lấy thời gian từ stratum 2).

Cài đặt ntp server trên windows server 2012

Sơ đồ phân lớp các NTP Server

NTP hoạt động bằng cách đo thời gian RTT (round trip time) của các gói tin trao đổi giữa client và server. Client sẽ điều chinh lại thời gian trên máy mình để đồng bộ thời gian với server. Thời gian điều chỉnh sẽ dựa trên hai thông số là timestamp khi gói tin được gởi đi và thời gian di chuyển của gói tin.

Chính vì vậy, ta nên chọn time server có độ delay thấp nhất có thể. Ví dụ như ở VN thì ta có thể chọn time server nằm trong vn.pool.ntp.org

Quy trình cơ bản khi cấu hình NTP Server là:

  1. NTP Server A lấy giờ chuẩn từ 1 server khác có level stratum cao hơn (từ pool.ntp.org)
  2. Các NTP Client khác sẽ lấy giờ từ NTP Server chuẩn này.

Cấu hình

NTP Server

Để cấu hình và cài đặt NTP Server (gói ntpd) trên CentOS 7, ta thực hiện những bước sau:

Cài đặt và cấu hình NTP server, cho phép các server trong subnet 10.0.0.0/24 truy cập được tới NTP server

yum –y install ntp

vi /etc/ntp.conf

[…]

restrict 10.0.0.0 mask 255.255.255.0 nomodify notrap

server vn.pool.ntp.org iburst

Start service và enable tính năng auto start cho ntpd

systemctl start ntpd

systemctl enable ntpd

Kiểm tra NTP Server đã lấy được giờ chuẩn chưa

ntpq -p

Cài đặt ntp server trên windows server 2012

NTP Server đang lấy giờ từ một server chuẩn 128.9.176.30

NTP Client (Linux)

Đối với các client Linux lấy giờ từ NTP server, ta có 2 cách

  1. Cài NTP service deamon tương tự như trên, nhưng lấy thời gian từ NTP Server mà ta chỉ định (ví dụ ở đây là 10.0.0.4)
  2. Dùng ntpdate và set crontab sync time mỗi 5p

Cách 1

Cài NTP service deamon tương tự như trên Server, nhưng ta sẽ trỏ về địa chỉ IP/domain của server của mình và không nhận request đồng bộ thời gian từ các máy client khác.

yum –y install ntp

vi /etc/ntp.conf

[…]

#server <IP_of_your_server> iburst
server 10.0.0.4 iburst

restrict default ignore

Start service và enable tính năng auto start cho ntpd

systemctl start ntpd

systemctl enable ntpd

Kiểm tra NTP Client đã lấy được giờ chuẩn chưa

ntpq -p

Cách 2

Dùng lệnh ntpdate để đồng bộ thời gian + set crontab cho lệnh này check thời gian theo giờ.

Đầu tiên, tiến hành cài đặt ntpdate:

yum –y install ntpdate

Cấu hình ntpdate update thời gian từ Vietnam NTP Server pool

ntpdate vn.pool.ntp.org

hoặc chỉ định một IP

ntpdate <IP_of_your_server>

Enable tính năng auto start cho ntpupdate

systemctl enable ntpdate

Cấu hình sync tự động trong crontab mỗi 5p

sudo crontab -e

*/5 * * * * /usr/sbin/ntpdate 10.0.0.4 2>&1 | tee -a /var/log/ntpdate.log

NTP Client (Windows)

Để đồng bộ thời gian trên Windows, ta có 2 cách:

  1. Dùng giao diện đồ họa
  2. Dùng dòng lệnh

Cách 1 – Cấu hình bằng giao diện

Với giao diện đồ họa, ta truy cập vào phần: Control Panel => Date and Time => Internet Time Settings

Cài đặt ntp server trên windows server 2012

Thay đổi giá trị mặc định của hệ thống thành IP/domain name của NTP server mà ta muốn nhận thông tin và chọn Update now

Cách 2 – Giao diện dòng lệnh

Với giao diện dòng lệnh, ta sử dụng những lệnh sau:

#Tạm ngưng time service

net stop w32time

#Cấu hình trỏ tới các NTP server cần dùng

w32tm /config /manualpeerlist:"vn.pool.ntp.org,0x1" /syncfromflags:manual /reliable:yes

#Khởi động lại time service

net start w32time

#Kiểm tra lại thông tin cấu hình

w32tm /query /configuration

w32tm /query /status

Cài đặt ntp server trên windows server 2012

Cấu hình trỏ NTP bằng dòng lệnh trên Windows Server 2008

Cài đặt ntp server trên windows server 2012

Kiểm tra lại các thông tin cấu hình

Tham khảo

https://www.server-world.info/en/note?os=CentOS_7&p=ntp&f=1

https://www.server-world.info/en/note?os=Windows_Server_2012&p=ntp