2015年4月8日 星期三

[Linux 文章收集] Linux Set Date and Time From a Command Prompt

Source From Here
Preface
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. How can I set date under Linux 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 root 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.

Linux Display Current Data and Time
Just type the date command:
$ date
Tue Jan 6 14:43:04 EST 2015

Linux Display The Hardware Clock (RTC)
Type the following hwclock command to read the Hardware Clock and display the time on screen:
[root@route ~]$ hwclock -r
Wed 21 Jan 2015 11:28:32 AM EST -0.604412 seconds
[root@route ~]$ hwclock --show
Wed 21 Jan 2015 11:28:46 AM EST -0.093290 seconds
[root@route ~]$ hwclock --show --utc
Wed 21 Jan 2015 11:28:59 AM EST -0.654893 seconds

Linux Set Date Command Example
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"

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"


How do I set the Hardware Clock to the current System Time? Use the following syntax:
# hwclock --systohc
OR
# hwclock -w

A note about systemd based Linux system
With systemd based system you need to use the timedatectl command to set or view the current date and time. Most modern distro such as RHEL/CentOS v.7.x+, Fedora Linux, Debian, Ubuntu, Arch Linux and other systemd based system need to the timedatectl utility. Please note that the above command should work on modern system too.

timedatectl: Display the current date and time


How do I change the current date using the timedatectl command?
To change the current date, type the following command as root user:
# timedatectl set-time YYYY-MM-DD

For example set the current date to 2015-12-01 (1st, Dec, 2015):
# timedatectl set-ntp 0 // Disable Automatic time synchronization
# timedatectl set-time '2015-12-01'
# timedatectl
Local time: Tue 2015-12-01 00:00:03 CST
...

# timedatectl set-ntp 1 // Enable Automatic time synchronization and wait a few seconds, the time will be changed back

To change both the date and time, use the following syntax:
# timedatectl set-time YYYY-MM-DD HH:MM:SS

Where,
HH : An hour.
MM : A minute.
SS : A second, all typed in two-digit form.
YYYY: A four-digit year.
MM : A two-digit month.
DD: A two-digit day of the month.

For example, set the date ’23rd Nov 2015′ and time to ‘8:10:40 am’, enter:
# timedatectl set-time '2015-11-23 08:10:40'
# date // Show the updated result

How do I set the current time only?
The syntax is:
# timedatectl set-time HH:MM:SS

For example:
# timedatectl set-time '10:42:43'
# date

How do I set the time zone using timedatectl command?
To see list all available time zones, enter:
$ timedatectl list-timezones
$ timedatectl list-timezones | more
$ timedatectl list-timezones | grep -i asia
$ timedatectl list-timezones | grep America/New

To set the time zone to ‘Asia/Taipei’, enter:
# timedatectl set-timezone 'Asia/Taipei'
# timedatectl // Verify it
...
Time zone: Asia/Taipei (CST, +0800)
...

How do I synchronizing the system clock with a remote server using NTP?
Simply type the following command:
# timedatectl set-ntp yes
# timedatectl // Verify it
...
NTP enabled: yes
NTP synchronized: yes
...


Supplement
nixCraft - How To Change Timezone on a CentOS 6 and 7
* You can also set new timzone using this mini-howto.
* Man pages - hwclock(8)date(1)
[Linux 文章收集] 系統時間日期變更的指令
[linux][shell] 關於'date' 應該知道的事
有時候我們會利用shell script來做測試,或者監控系統,這時候如果能知道從測試開始到目前為止花了多少時間,是一件非常方便的事情。 利用剛剛提到的 『date +%s』,加上一點點計算,可以用來計算shell script 所花費的時間,例如:
  1. #!/bin/bash  
  2. function elapsed_time()  
  3. {  
  4.     s_time=$1  
  5.     e_time=$2  
  6.   
  7.     elap_s=$((e_time-s_time))  
  8.     ss=$((elap_s%60))  
  9.     mm=$(((elap_s/60)%60))  
  10.     hh=$((elaps/3600))  
  11.     printf "%i:%02i:%02i" $hh $mm $ss  
  12. }  
  13.   
  14. begin_time=`date "+%s"`  
  15.   
  16. for i in {1..3};  
  17. do  
  18.     now_time=`date "+%s"`  
  19.     elap_time=`elapsed_time $begin_time $now_time`  
  20.     echo "elapsed time=$elap_time"  
  21.     sleep 1  
  22. done  


沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...