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

Use the date command to display the current date and time or set the system date / time over ssh session. You can also run the date command from X terminal as user.

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.

You must login as root user to use date command.

1. Linux Set Date

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"
OR
# date --set="2 OCT 2006 18:00:00"

You can also simplify format using following syntax:
# date +%Y%m%d -s "20081128"

Linux Set Time

To set time use the following syntax:
# date +%T -s "10:13:13"
Where,

  • 10: Hour (hh)
  • 13: Minute (mm)
  • 13: Second (ss)

Use %p locale’s equivalent of either AM or PM, enter:
# date +%T%p -s "6:10:30AM"
# date +%T%p -s "12:10:30PM"

2. Linux server change or setup the timezone

Q. My timezone is pointing to wrong timezone. How do I setup or change the timezone under Linux operating systems?

A. Unix time, or POSIX time, is a system for describing points in time: it is the number of seconds elapsed since midnight UTC on the morning of January 1, 1970, not counting leap seconds.

The definition for time zones can be written in short form as UTC±n (or GMT±n), where n is the offset in hours.

Change Linux timezone

Select the method as per your Linux distribution:

If you are using Fedora / RHEL / Cent OS Linux

Type the redhat-config-date command at the command line to start the time and date properties tool.
# redhat-config-date
OR type setup and select time zone configuration (good for remote ssh text based Linux server sessiob)
# setup

Select timezone configuration

Fig.01: Redhat / CentOS Server Setting Up Timezone

Fig.01: Redhat / Server Setting Up Timezone

Now, just follow on screen instructions to change timezone.

Set timezone using /etc/localtime configuration file [any Linux distro]

Often /etc/localtime is a symlink to the file localtime or to the correct time zone file in the system time zone directory.

Generic procedure to change timezone

Change directory to /etc
# cd /etc

Create a symlink to file localtime:
# ln -sf /usr/share/zoneinfo/EST localtime
OR some distro use /usr/share/zoneinfo/dirname/zonefile format (Red hat and friends)
# ln -sf /usr/share/zoneinfo/EST localtime
OR if you want to set up it to IST (Asia/Calcutta):
# ln -sf /usr/share/zoneinfo/Asia/Calcutta localtime

OR ifc you want to set up it to Asia ( Ho_Chi_Minh )

ln -sf /usr/share/zoneinfo/Asia/Ho_Chi_Minh /etc/localtime

Please mote that in above example you need to use directory structure i.e. if you want to set the timezone to Calcutta (India) which is located in the Asia directory you will then have to setup using as above.

Use date command to verify that your timezone is changed:
$ date
Output:

Tue Aug 27 14:46:08 EST 2006

Use of environment variable

You can use TZ environment variable to display date and time according to your timezone:
$ export TZ=America/Los_Angeles
$ date

Sample Output:

Thu Aug 27 11:10:08 PST 2006
Print Friendly

Comments

comments

Bài viết liên quan