linux同步系统时间
Time is important for systems because all work done according to time and logs get their timestamps. Normally server hardware provides time but it can be false over time because the hardware clock is not precise. Ntp is a protocol designed to get time info from the network. NTP can also provide date and time information to other servers as a daemon. NTP full name is Network Time Protocol.
时间对系统很重要,因为所有根据时间和日志完成的工作都会获得时间戳。 通常,服务器硬件会提供时间,但随着时间的流逝,它可能是错误的,因为硬件时钟不精确。 Ntp是一种旨在从网络获取时间信息的协议。 NTP还可以将日期和时间信息作为守护程序提供给其他服务器。 NTP全名是网络时间协议。
安装Ntpdate (Install Ntpdate)
Ntpdate is simple and fast ntp client used to get information about time.
Ntpdate是一种简单而快速的ntp客户端,用于获取有关时间的信息。
CentOS, Fedora, Red Hat
CentOS,Fedora,Red Hat
$ yum install ntpdate -y
Ubuntu, Debian, Mint, Kali
Ubuntu,Debian,Mint,Kali
$ sudo apt-get install ntpdate
从Ntp服务器获取时间 (Get Time From Ntp Server)
We can use ntpdate client to get the current time from the NTP server. We will specify the NTP server as a parameter to the ntpdate command.
我们可以使用ntpdate客户端从NTP服务器获取当前时间。 我们将NTP服务器指定为ntpdate命令的参数。
$ sudo ntpdate ntp.ubuntu.com
16 Nov 07:06:12 ntpdate[4551]: adjust time server 91.189.89.199 offset -0.000030 sec
We need root privileges to use ntpdate to sync clock
我们需要root特权才能使用ntpdate同步时钟
从本地服务器检查当前时间(Check Current Time From Local Server)
We can list current time in the server by using date command.
我们可以使用date命令列出服务器中的当前时间。
$ date
Wed Nov 16 07:07:42 +03 2016
Enable Firewall For Ntp Related Ports
为Ntp相关端口启用防火墙
We may need to open ports about NTP like below by using iptables
我们可能需要使用iptables来打开有关NTP的端口,如下所示
$ iptables -A output -p udp -s 192.168.1.1 --sport 1024:65535 -d 0/0 --dport 123 -m state --state NEW,ESTABLISHED -j ACCEPT
$ iptables -A input -p udp -s 0/0 --sport 123 -d 192.168.1.1 --dport 1024:65535 -m state --state ESTABLISHED -j ACCEPT
获取当前时间信息 (Get Current Time Info)
ntpdate is an old tool and deprecated we can use datetimectl according to ntpdate
ntpdate是一个旧工具,不赞成使用,我们可以根据ntpdate使用datetimectl
$ timedatectl status
We see that our time is synchronized with ntp with “NTP synchronized” line.
我们看到,我们的时间与“ NTP同步”行的ntp同步。
如何在Linux中同步系统时间? 信息图 (How To Synchronize System Time In Linux? Infographic)
linux同步系统时间