ubuntu ntp服务器安装

该博客详细介绍了如何在Ubuntu服务器上安装和配置NTP服务端,允许特定网段进行时间同步。接着,通过`ntpdate`命令同步时间,并使用`tzselect`来选择并设置时区为亚洲/上海。文章还解决了在运行`tzselect`时遇到的问题,最后验证了时间已成功更改为北京时间。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ntp服务端安装

root@ubuntu:/# sudo apt-get install ntp

修改配置文件

root@ubuntu:/# vi /etc/ntp.conf
# Clients from this (example!) subnet have unlimited access, but only if
# cryptographically authenticated.
#restrict 192.168.123.0 mask 255.255.255.0 notrust


# If you want to provide time to your local subnet, change the next line.
# (Again, the address is an example only.)
#broadcast 192.168.123.255

# If you want to listen to time broadcasts on your local subnet, de-comment the
# next lines.  Please do this only if you trust everybody on the network!
#disable auth
#broadcastclient

#Changes recquired to use pps synchonisation as explained in documentation:
#http://www.ntp.org/ntpfaq/NTP-s-config-adv.htm#AEN3918

#server 127.127.8.1 mode 135 prefer    # Meinberg GPS167 with PPS
#fudge 127.127.8.1 time1 0.0042        # relative to PPS for my hardware

#server 127.127.22.1                   # ATOM(PPS)
#fudge 127.127.22.1 flag3 1            # enable PPS API
restrict 10.1.40.0 mask 255.255.255.0 nomodify

文件最后添加restrict 10.1.40.0 mask 255.255.255.0 nomodify 主要是允许能同步的服务器所在的内部网段

ntp客户端安装

root@ubuntu:/# sudo apt-get install ntpdate

同步时间

root@ubuntu:/# ntpdate 10.1.37.77

修改时区

先查看当前系统时间

root@ubuntu:/# date -R 

1.运行tzselect

root@ubuntu:/# tzselect

出现如下错误

root@ubuntu:/#tzselect
/usr/bin/tzselect: line 180: /usr/share/zoneinfo/iso3166.tab: No such file or directory
/usr/bin/tzselect: time zone files are not set up correctly

需要安装

root@ubuntu:/# apt-get install tzdata
root@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) Iraq		    35) Pakistan
 2) Antarctica		  19) Israel		    36) Palestine
 3) Armenia		  20) Japan		    37) Philippines
 4) Azerbaijan		  21) Jordan		    38) Qatar
 5) Bahrain		  22) Kazakhstan	    39) Russia
 6) Bangladesh		  23) Korea (North)	    40) Saudi Arabia
 7) Bhutan		  24) Korea (South)	    41) Singapore
 8) Brunei		  25) Kuwait		    42) Sri Lanka
 9) Cambodia		  26) Kyrgyzstan	    43) Syria
10) China		  27) Laos		    44) Taiwan
11) Cyprus		  28) Lebanon		    45) Tajikistan
12) East Timor		  29) Macau		    46) Thailand
13) Georgia		  30) Malaysia		    47) Turkmenistan
14) Hong Kong		  31) Mongolia		    48) United Arab Emirates
15) India		  32) Myanmar (Burma)	    49) Uzbekistan
16) Indonesia		  33) Nepal		    50) Vietnam
17) Iran		  34) Oman		    51) Yemen
#? 10
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:	Thu Jan  6 10:11:52 CST 2022.
Universal Time is now:	Thu Jan  6 02:11:52 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

2.复制文件到/etc目录下

root@ubuntu:~# cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

3.再次查看时间date -R,已经修改为北京时间

root@ubuntu:~# date -R
Thu, 06 Jan 2022 10:16:59 +0800
### 安装和配置 NTP 服务器 要在 Ubuntu 20.04 上安装并配置 NTP (Network Time Protocol) 服务器,可以按照以下方法操作: #### 1. 更新系统软件包 在开始之前,确保系统的软件包列表是最新的。运行以下命令来更新: ```bash sudo apt update && sudo apt upgrade -y ``` #### 2. 安装 NTP 软件包 通过 APT 包管理器安装 `ntp` 和其他必要的工具。执行以下命令完成安装[^1]: ```bash sudo apt install ntp -y ``` #### 3. 修改 NTP 配置文件 默认情况下,NTP 的主要配置文件位于 `/etc/ntp.conf`。编辑此文件以调整时间源和其他选项。 打开配置文件: ```bash sudo nano /etc/ntp.conf ``` - **添加或修改时间源** 找到 `server` 行,并根据需求替换为你信任的时间服务器地址。例如: ```plaintext server 0.pool.ntp.org iburst server 1.pool.ntp.org iburst server 2.pool.ntp.org iburst ``` - **允许本地网络同步** 如果希望让局域网中的设备能够连接到该 NTP 服务器,则需要启用权限控制。找到 `restrict` 条目,在适当位置添加如下内容: ```plaintext restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap ``` 此处假设子网为 `192.168.1.x`,可以根据实际情况更改 IP 地址范围。 保存并关闭文件。 #### 4. 启动和验证服务状态 重启 NTP 服务使新配置生效: ```bash sudo systemctl restart ntp ``` 检查服务的状态以确认其正常工作: ```bash sudo systemctl status ntp ``` 还可以使用以下命令查看当前正在使用的上游时间服务器及其延迟情况: ```bash ntpq -p ``` #### 5. 自定义脚本(可选) 对于无法访问外部互联网的环境,可以通过预下载的方式准备所需的二进制文件及相关依赖项。参考提供的部署脚本示例[^2],其中提到的关键部分在于单独提取核心功能实现自动化安装流程。 --- ### 注意事项 - 确保防火墙规则允许 UDP 流量通过端口 123。 - 对于高精度场景下可能还需要考虑硬件时钟校准等因素影响最终效果表现。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值