ntp 时钟同步
运行 ps -p 1 来查看PID 1的进程是什么。如果输出显示systemd,则系统使用的是systemd
对于使用SysVinit的传统系统
放置脚本于/etc/init.d/ :
将您的脚本b.sh复制或链接到/etc/init.d/目录,并确保它具有可执行权限。
Bash
sudo cp b.sh /etc/init.d/b
sudo chmod +x /etc/init.d/b
创建启动链接:
使用chkconfig命令创建启动链接,使脚本能够在不同的运行级别自动启动。首先,您需要确保脚本符合SysVinit的标准格式,包括start、stop等函数。
Bash
sudo chkconfig --add b
sudo chkconfig b on
对于使用Systemd的系统(SUSE Linux Enterprise Server 12及以上或openSUSE Leap/Tumbleweed)
创建Systemd服务单元文件:
在/etc/systemd/system/目录下创建一个新的服务单元文件,比如命名为b.service。
Bash
sudo nano /etc/systemd/system/b.service
编辑服务单元文件,填入以下内容:
Plaintext
[Unit]
Description=Custom Script at Boot
After=network.target
[Service]
Type=simple
ExecStart=/path/to/your/b.sh
User=root # 或者指定执行脚本所需的用户
StandardOutput=journal # 或其他日志处理选项
[Install]
WantedBy=multi-user.target
确保替换/path/to/your/b.sh为您的脚本实际路径。
重新加载Systemd配置并启动服务:
Bash
sudo systemctl daemon-reload
sudo systemctl enable b.service
sudo systemctl start b.service
192.138.80.168时钟源配置
cat /etc/ntp.conf 可见只配置了本地服务器作为时钟服务器
直接启动作为时间源服务器 sudo /etc/init.d/ntp start
设置为开机自启 sudo chkconfig ntp on
sudo service ntp status 再次查看状态
ICBC-DB3:~ # service ntp status
remote refid st t when poll reach delay offset jitter
==============================================================================
*LOCAL(0) .LOCL. 10 l 13 64 377 0.000 0.000 0.001
Checking for network time protocol daemon (NTPD):
suse防火墙
1. SuSE Linux防火墙配置文件路径:/etc/sysconfig/SuSEfirewall2; 修改其中的udp端口
2. 启动、关闭、重启防火墙:
rcSuSEfirewall2 start
rcSuSEfirewall2 stop
rcSuSEfirewall2 restart
注意不能用telnet方式测试,因为telnet只能测试tcp端口,所以 使用 netstat -ulpn | grep :123测试upd端口,如下为正常启动的
ICBC-DB3:~ # netstat -ulpn | grep :123
udp 0 0 192.138.80.168:123 0.0.0.0:* 30200/ntpd
udp 0 0 127.0.0.2:123 0.0.0.0:* 30200/ntpd
udp 0 0 127.0.0.1:123 0.0.0.0:* 30200/ntpd
udp 0 0 0.0.0.0:123 0.0.0.0:* 30200/ntpd
udp 0 0 ::1:123 :::* 30200/ntpd
udp 0 0 fe80::250:56ff:fe9f:123 :::* 30200/ntpd
udp 0 0 :::123 :::* 30200/ntpd
192.138.80.*等 时钟客户机器设置
vi /etc/ntp.conf
把其中的server和fudge 两行都注释掉
增加一行类似
server 192.138.80.168 iburst #本机时钟注释掉,使用192.138.80.168的时钟
重启 Kjlink-ICBC1:~ # sudo /etc/init.d/ntp restart
Shutting down network time protocol daemon (NTPD) done
Starting network time protocol daemon (NTPD)
如果没有启动,则先sudo /etc/init.d/ntp restart
done
检查 Kjlink-ICBC1:~ # ntpq -p
remote refid st t when poll reach delay offset jitter
==============================================================================
192.138.80.168 LOCAL(0) 11 u 12 64 7 0.175 6.562 3.855
检查开机是否启动 Kjlink-ICBC1:~ # chkconfig --list ntp
ntp 0:off 1:off 2:off 3:off 4:off 5:off 6:off
设置开机启动同步 sudo chkconfig ntp on
手动调整时间 源时钟服务器的时间192.138.80.168,客户端时间不用动
首先,停止NTP服务以避免同步过程中可能出现的冲突:
sudo systemctl stop ntpd
我们是在SysVinit系统上可能是:
sudo /etc/init.d/ntp stop
使用date命令手动设置时间,例如:
Bash
sudo date -s "2024-05-12 14:30:00"
这里的时间字符串格式应符合YYYY-MM-DD HH:MM:SS格式。
设置硬件时钟(可选),以确保即使系统重启也能维持准确时间:
Bash
sudo hwclock -w
启动NTP服务并监控:
重新启动NTP服务:
Bash
sudo systemctl start ntpd
或者,在SysVinit系统上:
Bash
sudo /etc/init.d/ntp start
确认NTP服务已经正确启动并同步:
Bash
ntpq -p
-------------------------------------------------------------------
ubuntu上面 安装使用apt不使用yum;
安装 sudo apt install ntp
配置/etc/ntp.conf
启动 sudo systemctl start ntp
配置自启 sudo systemctl enable ntp
查看情况 ntpq -p
ubuntu配置systemd-timesyncd 更简单
使用默认的 systemd-timesyncd(推荐)
Ubuntu 24.04 默认已安装并启用 systemd-timesyncd:
1、# 检查服务状态
sudo systemctl status systemd-timesyncd
2、# 查看时间同步状态
timedatectl status
查看详细的时间同步状态
timedatectl timesync-status
3、如需配置自定义 NTP 服务器:
vi /etc/systemd/timesyncd.conf
取消注释 NTP= 行并添加您偏好的 NTP 服务器,例如:
NTP=192.138.80.168
4、重启服务:
sudo systemctl restart systemd-timesyncd