如何在Ubuntu16和Ubuntu18上安装NTP服务器和客户端
第一修改时区
NTP服务器和客户端都设置统一的时区
首先输入date查看自己的时间时区,可以看到是UTC时区,如果是CST时区则可以跳过这个步骤:
然后输入tzselect,选择4 9 1 1 (具体情况具体选择,最后是的结果是
TZ=‘Asia/Shanghai’ CST )
输入1确认,执行TZ=‘Asia/Shanghai’; export TZ生效,并将这些指令写入.bashrc文件中,source一下,重启电脑,再次运行date可看到已经到了CST时区
root@VM-4-11-ubuntu:~# date
Sat May 28 22:19:29 CST 2022
root@VM-4-11-ubuntu:~# tzselect
Please identify a location so that time zone rules can be set correctly.
Please select a continent, ocean, "coord", or "TZ".
1) Africa
2) Americas
3) Antarctica
4) Asia
5) Atlantic Ocean
6) Australia
7) Europe
8) Indian Ocean
9) Pacific Ocean
10) coord - I want to use geographical coordinates.
11) TZ - I want to specify the time zone using the Posix TZ format.
#? 4
Please select a country whose clocks agree with yours.
1) Afghanistan 18) Israel 35) Palestine
2) Armenia 19) Japan 36) Philippines
3) Azerbaijan 20) Jordan 37) Qatar
4) Bahrain 21) Kazakhstan 38) Russia
5) Bangladesh 22) Korea (North) 39) Saudi Arabia
6) Bhutan 23) Korea (South) 40) Singapore
7) Brunei 24) Kuwait 41) Sri Lanka
8) Cambodia 25) Kyrgyzstan 42) Syria
9) China 26) Laos 43) Taiwan
10) Cyprus 27) Lebanon 44) Tajikistan
11) East Timor 28) Macau 45) Thailand
12) Georgia 29) Malaysia 46) Turkmenistan
13) Hong Kong 30) Mongolia 47) United Arab Emirates
14) India 31) Myanmar (Burma) 48) Uzbekistan
15) Indonesia 32) Nepal 49) Vietnam
16) Iran 33) Oman 50) Yemen
17) Iraq 34) Pakistan
#? 9
Please select one of the following time zone regions.
1) Beijing Time
2) Xinjiang Time
#? 1
The following information has been given:
China
Beijing Time
Therefore TZ='Asia/Shanghai' will be used.
Selected time is now: Sat May 28 22:19:59 CST 2022.
Universal Time is now: Sat May 28 14:19:59 UTC 2022.
Is the above information OK?
1) Yes
2) No
#? 1
You can make this change permanent for yourself by appending the line
TZ='Asia/Shanghai'; export TZ
to the file '.profile' in your home directory; then log out and log in again.
Here is that TZ value again, this time on standard output so that you
can use the /usr/bin/tzselect command in shell scripts:
Asia/Shanghai
root@VM-4-11-ubuntu:~# TZ='Asia/Shanghai'; export TZ
root@VM-4-11-ubuntu:~# date
Sat May 28 22:24:09 CST 2022
root@VM-4-11-ubuntu:~#
第二 设置服务器
在服务端安装相关软件
sudo apt-get install ntp
/etc/init.d/ntp start
然后配置系统的UFW防火墙,以使传入的连接可以通过UDP端口号123访问NTP服务器
以sudo身份运行以下命令以打开端口123以接收传入流量:
sudo apt install ufw
sudo ufw allow from any to any port 123 proto udp
显示如下,表明配置成功
第三设置客户端
1.客户端安装ntp
sudo apt-get install ntpdate
ntpdate 192.168.33.200 //服务器地址ip
2.客户端定时更新
上述指令是一次性更新,如果我们想要让客户端定期去更新维护,则需要用到crontab指令
sudo vi /etc/crontab
30 08 * * * root /usr/sbin/ntpdate 192.168.33.200 #表示每天8:30自动执行ntpdate指令,与服务器时间同步