Software & AppsOperating SystemLinux

How To Change System Clock Using Command Line in Ubuntu?

Ubuntu 19

In this article, we will explore how to change the system clock using the command line in Ubuntu. This can be a handy skill for system administrators and users who prefer to work with the terminal or are working on a server without a graphical interface.

Quick Answer

To change the system clock using the command line in Ubuntu, you can use the date command or the timedatectl command. The date command allows you to set the date and time manually, while the timedatectl command is used for systems with systemd. Additionally, you can change the time zone using the timedatectl command and synchronize the system clock with an NTP server using the ntpdate command.

Understanding the System Clock

The system clock, also known as the software clock, in Ubuntu is based on the number of seconds passed since the epoch, i.e., January 1, 1970. It can be displayed and set using the date command. The system clock can be set manually or it can be synchronized with a remote server using the Network Time Protocol (NTP).

Changing the System Clock Using the date Command

The date command in Ubuntu is used to display or set the system’s date and time. To change the date and time, you can use the --set option followed by the desired date and time in quotes. Here is an example:

sudo date --set="1971-01-02 23:31:59.990"

In this command, sudo is used to run the command with root privileges. The --set option is used to set the date and time, and the string "1971-01-02 23:31:59.990" specifies the desired date and time. The format is YYYY-MM-DD HH:MM:SS.sss.

Changing the System Clock Using the timedatectl Command

For systems with systemd, the timedatectl command can be used to change the system clock. Here is an example:

sudo timedatectl set-time "1971-01-02T23:31:59.990"

In this command, sudo is used to run the command with root privileges. The set-time option is used to set the date and time, and the string "1971-01-02T23:31:59.990" specifies the desired date and time. The format is YYYY-MM-DDTHH:MM:SS.sss.

Changing the Time Zone

To change the system’s time zone, you can use the timedatectl command with the set-timezone option followed by the desired time zone. Here is an example:

sudo timedatectl set-timezone America/New_York

In this command, sudo is used to run the command with root privileges. The set-timezone option is used to set the time zone, and America/New_York is the desired time zone.

You can find a list of all available time zones here.

Synchronizing the System Clock with an NTP Server

The ntpdate command can be used to synchronize the system clock with an NTP server. Here is an example:

sudo ntpdate -s ntp.ubuntu.com

In this command, sudo is used to run the command with root privileges. The -s option is used to log messages to the system’s syslog facility instead of the standard output. ntp.ubuntu.com is the NTP server.

Conclusion

In this article, we have learned how to change the system clock and time zone using the command line in Ubuntu. We have also learned how to synchronize the system clock with an NTP server. Whether you are a system administrator or a regular user, knowing how to manipulate the system clock from the command line can be a useful skill.

How do I check the current system date and time in Ubuntu using the command line?

You can use the date command without any options to display the current system date and time. Simply type date in the terminal and press Enter.

Can I change the system clock in Ubuntu without root privileges?

No, changing the system clock requires root privileges. You need to use the sudo command before the date or timedatectl command to run them with elevated privileges.

What is the format for setting the date and time using the `date` command?

The format for setting the date and time using the date command is YYYY-MM-DD HH:MM:SS.sss, where YYYY represents the year, MM represents the month, DD represents the day, HH represents the hour, MM represents the minutes, SS represents the seconds, and sss represents the milliseconds.

How can I change the time zone in Ubuntu using the command line?

You can use the timedatectl command with the set-timezone option followed by the desired time zone. For example, sudo timedatectl set-timezone America/New_York will set the time zone to America/New_York.

How do I synchronize the system clock with an NTP server using the command line?

You can use the ntpdate command followed by the NTP server’s address. For example, sudo ntpdate -s ntp.ubuntu.com will synchronize the system clock with the ntp.ubuntu.com NTP server.

Leave a Comment

Your email address will not be published. Required fields are marked *