# tzselect 修改時間命令html
[root@comput1 ~]# tzselect
Please identify a location so that time zone rules can be set correctly.
Please select a continent, ocean, "coord", or "TZ".
1) Africa
2) Americas
3) Antarctica
4) Asia
5) Atlantic Ocean
6) Australia
7) Europe
8) Indian Ocean
9) Pacific Ocean
10) coord - I want to use geographical coordinates.
11) TZ - I want to specify the time zone using the Posix TZ format.
#? 4
Please select a country whose clocks agree with yours.
1) Afghanistan 18) Israel 35) Palestine
2) Armenia 19) Japan 36) Philippines
3) Azerbaijan 20) Jordan 37) Qatar
4) Bahrain 21) Kazakhstan 38) Russia
5) Bangladesh 22) Korea (North) 39) Saudi Arabia
6) Bhutan 23) Korea (South) 40) Singapore
7) Brunei 24) Kuwait 41) Sri Lanka
8) Cambodia 25) Kyrgyzstan 42) Syria
9) China 26) Laos 43) Taiwan
10) Cyprus 27) Lebanon 44) Tajikistan
11) East Timor 28) Macau 45) Thailand
12) Georgia 29) Malaysia 46) Turkmenistan
13) Hong Kong 30) Mongolia 47) United Arab Emirates
14) India 31) Myanmar (Burma) 48) Uzbekistan
15) Indonesia 32) Nepal 49) Vietnam
16) Iran 33) Oman 50) Yemen
17) Iraq 34) Pakistan
#? 9
Please select one of the following time zone regions.
1) Beijing Time
2) Xinjiang Time
#? 1ios
The following information has been given:shell
China
Beijing Timevim
Therefore TZ='Asia/Shanghai' will be used.
Local time is now: Wed Jul 5 11:15:03 CST 2017.
Universal Time is now: Wed Jul 5 03:15:03 UTC 2017.
Is the above information OK?
1) Yes
2) No
#? 1bash
You can make this change permanent for yourself by appending the line
TZ='Asia/Shanghai'; export TZ
to the file '.profile' in your home directory; then log out and log in again.服務器
Here is that TZ value again, this time on standard output so that you
can use the /usr/bin/tzselect command in shell scripts:
Asia/Shanghai網絡
#例如:在設置中國時區使用亞洲/上海(+8)併發
[root@comput2 ~]# cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
cp: overwrite '/etc/localtime'? yesapp
#查看系統時間和日期
[root@comput1 ~]# date -R
Tue, 04 Jul 2017 23:15:24 -0400less
#修改時間和日期
[root@comput1 ~]# date -s "20170705 11:17:00"
Wed Jul 5 11:17:00 EDT 2017
[root@comput1 ~]# date -R
Wed, 05 Jul 2017 11:17:22 -0400
#刷新到blos
[root@comput1 ~]# hwclock -w
1、安裝ntp服務
yum install -y ntp
[root@localhost ~]# ls -F /usr/share/zoneinfo/ Africa/ Brazil/ Egypt GB-Eire HST Japan Navajo posixrules Turkey zone.tab America/ Canada/ Eire GMT Iceland Kwajalein NZ PRC UCT Zulu Antarctica/ CET EST GMT0 Indian/ Libya NZ-CHAT PST8PDT Universal Arctic/ Chile/ EST5EDT GMT-0 Iran MET Pacific/ right/ US/ Asia/ CST6CDT Etc/ GMT+0 iso3166.tab Mexico/ Poland ROC UTC Atlantic/ Cuba Europe/ Greenwich Israel MST Portugal ROK WET Australia/ EET GB Hongkong Jamaica MST7MDT posix/ Singapore W-SU
在這裏面能夠找到本身所在城市的time zone文件,例如北京時間就是這個文件
[root@localhost ~]# ls -F /usr/share/zoneinfo/Asia/Shanghai /usr/share/zoneinfo/Asia/Shanghai
查看每一個time zone當前的時間可使用zdump命令
[root@localhost ~]# zdump Japan Japan Thu Feb 16 12:01:34 2017 JST
若要修改系統時區可使用以下方法
[root@localhost ~]# rm -rf /etc/localtime [root@localhost ~]# ln -sf /usr/share/zoneinfo/posix/Asia/Shanghai /etc/localtime [root@localhost ~]# ll /etc/localtime lrwxrwxrwx. 1 root root 35 Feb 16 2017 /etc/localtime -> ../usr/share/zoneinfo/Asia/Shanghai [root@localhost ~]# date Thu Feb 16 11:05:24 CST 2017
首先明確一個概念:
在計算機上有兩個時鐘,一個是硬件時鐘(RTC),一個是系統時鐘(System Clock)
硬件時鐘(RTC)是指嵌在主板上的特殊的電路,他的存在就是平時咱們關機後還能夠計算時間的緣由。
系統時鐘(System Clock)是操做系統的kernel用來計算時間的時鐘,它從1970年1月1日00:00:00 UTC時間到目前爲止秒數總和的值 在Linux下系統時間在開機的時候會和硬件時間同步(synchronization),以後也就各自獨立運行了
既然兩個時鐘獨立運行,時間久了就會產生偏差,
[root@localhost ~]# date Thu Feb 16 11:13:04 CST 2017 [root@localhost ~]# hwclock --show 2017-02-16 11:13:05.679891+8:00
經過hwclock –show 命令咱們能夠查看機器上的硬件時間(always in local time zone), 咱們能夠看到它和系統時間仍是有必定的偏差的, 那麼咱們就須要把他們同步
若是咱們想要把硬件時間設置成系統時間咱們能夠運行如下命令
[root@localhost ~]# hwclock --hctosys
反之,咱們也能夠把系統時間設置成硬件時間
[root@localhost ~]# hwclock --systohc
那麼若是想設置硬件時間咱們能夠開機的時候在BIOS裏設定.也能夠用hwclock命令
[root@localhost ~]# hwclock --set --date="mm/dd/yy hh:mm:ss"
若是想要修改系統時間那麼用date命令就最簡單了
[root@localhost ~]# date -s "dd/mm/yyyy hh:mm:ss"
4、修改ntp.conf的配置
vim /etc/ntp.conf
配置示例:
# For more information about this file, see the man pages # ntp.conf(5), ntp_acc(5), ntp_auth(5), ntp_clock(5), ntp_misc(5), ntp_mon(5). # 將本身主機的bios芯片震盪頻率與上層的Time server頻率比較,將偏差記錄在這個文件裏 # 注意: driftfile 後面接的文件須要使用完整的路徑文件名,不能是連接文件,而且文件的權限須要設定成 ntpd守護進程能夠寫入。 # 格式:driftfile 文件名 driftfile /var/lib/ntp/drift # Permit time synchronization with our time source, but do not # permit the source to query or modify the service on this system. # 格式:restrict [IP] mask [netmask_IP] [parameter] # Parameter 的參數主要以下: # ignore :拒絕全部類型的NTP聯機。 # nomodify: 客戶端不能使用ntpc 與ntpq 這兩個程序來修改服務器的時間參數,但客戶端可透過這部主機來進行網絡校時; # noquery:客戶端不可以使用ntpc 與ntpq 等指令來查詢時間服務器,不提供NTP的網絡校時。 # notrap:不提供trap 這個運程事件登入的功能。 # notrust:拒絕沒有認證的客戶端。 # Kod:kod技術能夠阻止「Kiss of Death 「包對服務器的破壞。 # Nopeer:不與其餘同一層的NTP服務器進行時間同步。 # 若是沒有在parameter的地方加上任何參數的話,那麼表示這個ip或網絡不受任何限制 # 表示默認拒絕全部IP的時間同步 #restrict default nomodify notrap nopeer noquery # 容許任何ip的客戶機均可以進行時間同步 restrict default nomodify # Permit all access over the loopback interface. This could # be tightened as well, but to do so would effect some of # the administrative functions. # 容許本機環回地址能夠進行時間同步 restrict 127.0.0.1 restrict ::1 # Hosts on local network are less restricted. #restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap # 容許192.168.159.0/24網段的客戶機均可以進行時間同步 restrict 192.168.159.0 mask 255.255.255.0 nomodify notrap # Use public servers from the pool.ntp.org project. # Please consider joining the pool (http://www.pool.ntp.org/join.html). # 利用server 設定上層NTP服務器 # 格式:server [IP or hostname] [prefer] # perfer:表示優先級最高 # burst :當一個運程NTP服務器可用時,向它發送一系列的併發包進行檢測。 # iburst :當一個運程NTP服務器不可用時,向它發送一系列的併發包進行檢測。 # 注:默認狀況小15分鐘後纔會與上層NTP服務器進行時間校對。 server 210.72.145.44 prefer server time1.aliyun.com iburst server time2.aliyun.com iburst server time3.aliyun.com iburst server time4.aliyun.com iburst # 設置本地服務器爲根時間服務器 server 127.127.1.0 # 這行是時間服務器的層次。設爲0則爲頂級,若是要向別的NTP服務器更新時間,請不要把它設爲0 fudge 127.127.1.0 stratum 6 #broadcast 192.168.1.255 autokey # broadcast server #broadcastclient # broadcast client #broadcast 224.0.1.1 autokey # multicast server #multicastclient 224.0.1.1 # multicast client #manycastserver 239.255.254.254 # manycast server #manycastclient 239.255.254.254 autokey # manycast client # Enable public key cryptography. #crypto includefile /etc/ntp/crypto/pw # Key file containing the keys and key identifiers used when operating # with symmetric key cryptography. # 認證可使用此時間源的客戶端 keys /etc/ntp/keys # Specify the key identifiers which are trusted. #trustedkey 4 8 42 # Specify the key identifier to use with the ntpdc utility. #requestkey 8 # Specify the key identifier to use with the ntpq utility. #controlkey 8 # Enable writing of statistics records. #statistics clockstats cryptostats loopstats peerstats # Disable the monitoring facility to prevent amplification attacks using ntpdc # monlist command when default restrict does not include the noquery flag. See # CVE-2013-5211 for more details. # Note: Monitoring will not be disabled with the limited restriction flag. # 防護NTP放大攻擊和NTP反射 disable monitor
5、啓動ntpd服務,並配置開機啓動
[root@localhost ~]# systemctl restart ntpd [root@localhost ~]# systemctl enable ntpd [root@localhost ~]# systemctl status ntpd ● ntpd.service - Network Time Service Loaded: loaded (/usr/lib/systemd/system/ntpd.service; enabled; vendor preset: disabled) Active: active (running) since Thu 2017-02-16 11:36:29 CST; 12s ago Main PID: 2819 (ntpd) Tasks: 1 (limit: 19660) CGroup: /system.slice/ntpd.service └─2819 /usr/sbin/ntpd -u ntp:ntp -g Feb 16 11:36:29 admin.example.local ntpd[2819]: Listen normally on 4 ens34 192.168.139.135 UDP 123 Feb 16 11:36:29 admin.example.local ntpd[2819]: Listen normally on 5 virbr0 192.168.122.1 UDP 123 Feb 16 11:36:29 admin.example.local ntpd[2819]: Listen normally on 6 ens33 fe80::20c:29ff:febf:9bc9 UDP 123 Feb 16 11:36:29 admin.example.local ntpd[2819]: Listen normally on 7 lo ::1 UDP 123 Feb 16 11:36:29 admin.example.local ntpd[2819]: Listen normally on 8 ens34 fe80::15cc:96ee:f841:b516 UDP 123 Feb 16 11:36:29 admin.example.local ntpd[2819]: Listening on routing socket on fd #25 for interface updates Feb 16 11:36:29 admin.example.local ntpd[2819]: 0.0.0.0 c016 06 restart Feb 16 11:36:29 admin.example.local ntpd[2819]: 0.0.0.0 c012 02 freq_set kernel 0.000 PPM Feb 16 11:36:29 admin.example.local ntpd[2819]: 0.0.0.0 c011 01 freq_not_set Feb 16 11:36:31 admin.example.local ntpd[2819]: 0.0.0.0 c614 04 freq_mode
# watch ntpq -p
各列的釋義:
列值 | 釋義 |
---|---|
remote | 它指的就是本地機器所鏈接的遠程NTP服務器 |
refid | 它指的是給遠程服務器(e.g. 193.60.199.75)提供時間同步的服務器 |
st | 遠程服務器的層級別(stratum). 因爲NTP是層型結構,有頂端的服務器,多層的Relay Server再到客戶端. 因此服務器從高到低級別能夠設定爲1-16. 爲了減緩負荷和網絡堵塞,原則上應該避免直接鏈接到級別爲1的服務器的. |
t | 這個…..我也不知道啥意思^_^ |
when | 我我的把它理解爲一個計時器用來告訴咱們還有多久本地機器就須要和遠程服務器進行一次時間同步 |
poll | 本地機和遠程服務器多少時間進行一次同步(單位爲秒). 在一開始運行NTP的時候這個poll值會比較小,那樣和服務器同步的頻率也就增長了,能夠儘快調整到正確的時間範圍.以後poll值會逐漸增大,同步的頻率也就會相應減少 |
reach | 這是一個八進制值,用來測試可否和服務器鏈接.每成功鏈接一次它的值就會增長 |
delay | 從本地機發送同步要求到服務器的round trip time |
offset | 這是個最關鍵的值, 它告訴了咱們本地機和服務器之間的時間差異.offset越接近於0,咱們就和服務器的時間越接近 |
jitter | 這是一個用來作統計的值. 它統計了在特定個連續的鏈接數裏offset的分佈狀況. 簡單地說這個數值的絕對值越小咱們和服務器的時間就越精確 |
在這裏會發現兩個問題:
1. 咱們鏈接的是0.fedora.pool.ntp.org爲何和remote server不同?
2. 那個最前面的+和*都是什麼意思呢?
第一個問題不難理解,由於NTP提供給咱們的是一個cluster server因此每次鏈接的獲得的服務器都有多是不同。一樣這也告訴咱們了在指定NTP Server的時候應該使用hostname而不是IP。
第二個問題和第一個相關,既然有這麼多的服務器就是爲了在發生問題的時候其餘的服務器還能夠正常地給咱們提供服務.那麼如何知道這些服務器的狀態呢? 這就是第一個記號會告訴咱們的信息
標記 | 釋義 |
---|---|
* | 它告訴咱們遠端的服務器已經被確認爲咱們的主NTP Server,咱們系統的時間將由這臺機器所提供 |
+ | 它將做爲輔助的NTP Server和帶有號的服務器一塊兒爲咱們提供同步服務. 當號服務器不可用時它就能夠接管 |
- | 遠程服務器被clustering algorithm認爲是不合格的NTP Server |
x | 遠程服務器不可用 |