Linux set date and time from command prompt

Sunday, November 9th, 2008 at 6:06 pm by Jiltin
Filed under: Unix Scripts 

Q. How can I set the system date and time from the command prompt (bash shell)? I don’t have GUI installed and I am login over ssh session.

A. Use date command to display the current date and time or set the system date and time over ssh session.

This is useful if the Linux server time and/or date is wrong, and you need to set it to new values from the shell prompt.

Task: Set new date / time

Use the following syntax to set new data and time:
date set=”STRING”

For example, set new data to 2 Oct 2006 18:00:00, type the following command as root user:

> date -s “2 OCT 2006 18:00:00″

> date -s “10 NOV 2008 08:08:00″

=================================

Run the date command to make sure you have the correct time:

[root@server /root]# date
Thu Sep 20 11:00:06 CDT 2001
[root@server /root]#

If you have the wrong timezone set, use “timeconfig” or “linuxconf” to set the correct timezone.

If your timezone is correct but the date and/or time needs updating, the following commands will update the date and time. The second command is needed in order to push the date and time into the PC clock.

[root@server /root]# date 092011082001
Thu Sep 20 11:08:00 CDT 2001
[root@server /root]# hwclock --utc --systohc
[root@server /root]#

Comments