Linux-配置时间(方式一)
-
查看本地当前时间 :
[root@localhost ~]# date Sat Apr 2 01:58:34 PDT 2022
-
安装
ntpdate
[root@localhost ~]# yum -y install ntpdate Loaded plugins: fastestmirror, langpacks Loading mirror speeds from cached hostfile * base: ftp.sjtu.edu.cn * extras: mirror.lzu.edu.cn * updates: ftp.sjtu.edu.cn base | 3.6 kB 00:00:00 extras | 2.9 kB 00:00:00 updates | 2.9 kB 00:00:00 Resolving Dependencies --> Running transaction check ---> Package ntpdate.x86_64 0:4.2.6p5-28.el7.centos will be updated --> Processing Dependency: ntpdate = 4.2.6p5-28.el7.centos for package: ntp-4.2.6p5-28.el7.centos.x86_64 ---> Package ntpdate.x86_64 0:4.2.6p5-29.el7.centos.2 will be an update --> Running transaction check ---> Package ntp.x86_64 0:4.2.6p5-28.el7.centos will be updated ---> Package ntp.x86_64 0:4.2.6p5-29.el7.centos.2 will be an update --> Finished Dependency Resolution Dependencies Resolved =============================================================================================================================================================== Package Arch Version Repository Size =============================================================================================================================================================== Updating: ntpdate x86_64 4.2.6p5-29.el7.centos.2 base 87 k Updating for dependencies: ntp x86_64 4.2.6p5-29.el7.centos.2 base 549 k Transaction Summary =============================================================================================================================================================== Upgrade 1 Package (+1 Dependent package) Total size: 635 k Downloading packages: warning: /var/cache/yum/x86_64/7/base/packages/ntp-4.2.6p5-29.el7.centos.2.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7 Importing GPG key 0xF4A80EB5: Userid : "CentOS-7 Key (CentOS 7 Official Signing Key) <security@centos.org>" Fingerprint: 6341 ab27 53d7 8a78 a7c2 7bb1 24c6 a8a7 f4a8 0eb5 Package : centos-release-7-6.1810.2.el7.centos.x86_64 (@anaconda) From : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7 Running transaction check Running transaction test Transaction test succeeded Running transaction Updating : ntpdate-4.2.6p5-29.el7.centos.2.x86_64 1/4 Updating : ntp-4.2.6p5-29.el7.centos.2.x86_64 2/4 Cleanup : ntp-4.2.6p5-28.el7.centos.x86_64 3/4 Cleanup : ntpdate-4.2.6p5-28.el7.centos.x86_64 4/4 Verifying : ntp-4.2.6p5-29.el7.centos.2.x86_64 1/4 Verifying : ntpdate-4.2.6p5-29.el7.centos.2.x86_64 2/4 Verifying : ntp-4.2.6p5-28.el7.centos.x86_64 3/4 Verifying : ntpdate-4.2.6p5-28.el7.centos.x86_64 4/4 Updated: ntpdate.x86_64 0:4.2.6p5-29.el7.centos.2 Dependency Updated: ntp.x86_64 0:4.2.6p5-29.el7.centos.2 Complete!
-
通过时钟服务器同步时间
[root@localhost ~]# ntpdate stdtime.sinica.edu.tw
Linux-配置时间(方式二)
-
通过设置时区的方法设置linux时间
[root@localhost ~]# tzselect Please identify a location so that time zone rules can be set correctly. Please select a continent or ocean. 1) Africa 2) Americas 3) Antarctica 4) Arctic Ocean 5) Asia 6) Atlantic Ocean 7) Australia 8) Europe 9) Indian Ocean 10) Pacific Ocean 11) none - I want to specify the time zone using the Posix TZ format. #? 5 Please select a country. 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) 此处删除 10) Cyprus 27) Lebanon 44) Tajikistan 11) East Timor 28) Macau 45) Thailand 12) Georgia 29) Malaysia 46) Turkmenistan 13) 此处删除 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 #? 1 // 选择时区 The following information has been given: China Beijing Time Therefore TZ='Asia/Shanghai' will be used. Local time is now: Sat Apr 2 17:10:23 CST 2022. Universal Time is now: Sat Apr 2 09:10:23 UTC 2022. Is the above information OK? 1) Yes 2) No #? 1 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
-
编辑设置系统的时区文件
vi/etc/sysconfig/clock
加上zone=asia/shanghai
[root@localhost ~]# vi /etc/sysconfig/clock
-
设置北京时区为本地时区,接着删除本地的时区文件
/etc/localtime
[root@localhost ~]# rm /etc/localtime rm: remove symbolic link ‘/etc/localtime’? y [root@localhost ~]# ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime [root@localhost ~]# date Sat Apr 2 17:14:35 CST 2022
Linux设置定时任务
- 设置定时任务为了防止客户机宕机
[root@centos7-3 ~]# crontab -e
追加:*/1 * * * * /usr/sbin/ntpdate us.pool.ntp.org;
每隔1分钟执行指令一次