Saturday, 14 November 2015

Set Time, Date Timezone in Linux from Command Line

Correct Date & Time in Operating System is very important and lot of things depends on it such as application compilation, etc. To view Date through command line:
$ date
Sat Nov 14 14:14:18 IST 2015

To set the year:
date -s 'next year'
date -s 'last year'

To set the month:
date -s 'last month'
date -s 'next month'

To set the day:
date -s 'next day'
date -s 'tomorrow'
date -s 'last day'
date -s 'yesterday'
date -s 'friday'

To set all together:
$ date -s '2015-11-15 14:21:30'
Sun Nov 15 14:21:30 IST 2015

To change part of the date, enter the date part that you want to change as a string and remains all others as date formatting variables. Refer below command which sets the 2016 year
$ date -s "$(date +'2016%m%d %H:%M')"
Mon Nov 14 14:25:00 IST 2016

The date formats are:

  • %Y - Year
  • %m - Month
  • %d - Day
  • %H - Hour
  • %M - Minute

Hardware time

Now the system time is set, but you may want to sync it with the hardware clock:

Use --show to print the hardware time:
hwclock --show

You can set the hardware clock to the current system time:
hwclock --systohc

Or the system time to the hardware clock:
hwclock --hctosys

To set the timezone

To set the timezone of your system clock do the following:
cp /usr/share/zoneinfo/Asia/Kolkata  /etc/localtime

Automatically adjust your system clock

To have your system to automatically adjust time we need to install ntp. Get it from your repository. Once installed you can configure it by editing configuration file /etc/ntpd.conf

Make sure to start the daemon, and to make it start automatically when the system boots.
/etc/init.d/ntpd start

To update from the command line against a time server:
ntpdate 10.22.10.4

No comments:

Post a Comment