Linux Commands

Linux Sync Time with Another Server

Time is a crucial factor in our daily lives and the technical side of things.

Therefore, maintaining an accurate time between the Linux systems is an important factor. It allows you to accurately know when various accounts are logged, file changes, network packets, and other critical system logs. This can, in turn, be useful in troubleshooting and fixing problems.

In this tutorial, you will learn how to synchronize the time between two servers using SSH.

Requirements

Before we get started with the tutorial, you are going to need the following:

  • SSH access to the remote server
  • Sudo or root privileges on the system

Once you have the above, we can get started.

Sync Time over SSH

The very first method to synchronize time between two servers is to use SSH. In my example, I have a CentOS 8 Server with a different time than my local machine running Fedora 34 Beta.

[centos@centos8 ~]$ timedatectl

               Local time: Mon 2021-05-17 10:19:21 IST

           Universal time: Mon 2021-05-17 04:49:21 UTC

                 RTC time: Mon 2021-05-17 04:49:22

                Time zone: Asia/Kolkata (IST, +0530)

System clock synchronized: yes

              NTP service: active

          RTC in local TZ: no

On Fedora, the time is:

[fedora@fedora34 ~]$ timedatectl

               Local time: Mon 2021-05-17 00:48:18 EDT

           Universal time: Mon 2021-05-17 04:48:18 UTC

                 RTC time: Mon 2021-05-17 04:48:18

                Time zone: America/New_York (EDT, -0400)

System clock synchronized: yes

              NTP service: active

          RTC in local TZ: no

To synchronize the time between the two systems, I am going to use SSH. For this, we will update the time of the CentOS 8 server to match one of my local machines.

Enter the command as:

[fedora@fedora34 ~]$ sudo date --set="$(ssh [email protected] ‘date -u’)"

Next, we provide the SSH password to the centos user and sync the time. The output is below:

[fedora@fedora34 ~]$ sudo date --set="$(ssh [email protected] date)"

The authenticity of host '192.168.0.20 (192.168.0.20)' can't be established.

ECDSA key fingerprint is SHA256:MEijFgZPPwA4Du5yb8UpVzhlKByLp25uXCcVQSF5urQ.

Are you sure you want to continue connecting (yes/no/[fingerprint])? yes

Warning: Permanently added '
192.168.0.20' (ECDSA) to the list of known hosts.

[email protected]'
s password:

Mon May 17 01:02:14 AM EDT 2021

You can now confirm that time has been synchronized between the two systems by calling timedatectl, as shown in the output below:

[fedora@fedora34 ~]$ timedatectl

               Local time: Mon 2021-05-17 01:11:25 EDT

           Universal time: Mon 2021-05-17 05:11:25 UTC

                 RTC time: Mon 2021-05-17 05:11:31

                Time zone: America/New_York (EDT, -0400)

System clock synchronized: no

              NTP service: inactive

          RTC in local TZ: no

On CentOS 8, the output is:

[centos@centos8 ~]$ timedatectl

               Local time: Mon 2021-05-17 01:11:04 EDT

           Universal time: Mon 2021-05-17 05:11:04 UTC

                 RTC time: Mon 2021-05-17 05:11:04

                Time zone: America/New_York (EDT, -0400)

System clock synchronized: yes

              NTP service: active

          RTC in local TZ: no

As you can see, the time synchronization between the two devices was successful.

NOTE: In some instances, if the two systems are not in the same time zone, the command will fail, and the synchronization will not happen.

Sync Time with NTP

The other simple method you can use to synchronize your system is to use the NTP protocol, using the ntpdate tool. However, REHL/CENTOS users may have to use chrony for such operations.

To learn how to install and use Chrony and NTP server, consider the following tutorial

https://linuxhint.com/install-ntp-using-chrony-linux/

To synchronize time using ntpdate, use the command:

ntpdate ntp_server_ip

To get a list of NTP servers in your zone, go to https://www.ntppool.org/en/ and search your time zone.

[fedora@fedora34 ~]$ sudo ntpdate  0.north-america.pool.ntp.org

The output is below:

{"time":"2021-05-17T01:26:24.888127-0400","offset":6.253545,"precision":0.131665,"host":"0.north-america.pool.ntp.org","ip":"69.89.207.99","stratum":1,"leap":"no-leap","adjusted":true}

CLOCK: time stepped by 6.253545

Conclusion

In this tutorial, you learned how to use SSH to synchronize the time between servers. Try both the methods above and see which works best for you.

Thank you for reading and sharing!

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list