Syncing time and date with ntpdate

While there may be other ways of changing the clock on your Linux machine, there is one easy way of doing it automatically. This involves the use of an internet connection to do it. We are going to use a program called ntpdate.

ntpdate

ntpdate(Network Time Protocol) is a popular program in Linux that allows it to connect to the internet and automatically sync with the system time. By default, ntpdate is not installed so we will need to run the following in order to do that:

For Debian/Ubuntu:

sudo apt install ntpdate

CentOS/RHEL:

yum install ntpdate

Once that’s complete, lets go ahead and run a command to sync our time with a server. For this example, we are going to use the server us.pool.ntp.org

sudo ntpdate us.pool.ntp.org

Note: If ntpdate is giving you an error, then it may be due to ntp having been installed and is listening on the same port as ntpdate. Removing ntp via ” sudo apt remove ntp” should remedy the issue quickly.

The date and time should now be synced with the server specified. There are plenty of servers to choose from for each region, depending on where you live, it is best to choose a specific pool to use from.

Look here for a list of servers to choose from. If you are running a Linux distribution with systemd, then you can edit the file /etc/systemd/timesyncd.conf.

ntpdate

Go ahead and configure this file accordingly to what your time zone is. The website mentioned above will provide you with all the information you need to configure a proper NTP server. Again, preferably close to your location for the most accuracy and the lowest latency.

Once you’re finished, save the timesyncd.conf file and type in the following:

sudo timedatectl

Your date and time should now be in sync. If not then type:

sudo timedatectl set-ntp true

And check again. It should be working now.