linux时间和日期设置


系统的基本设置包括网络设置、时间和日期设置、语系设置、防火墙设置等。 CentOS 6.x之前版本有个名为setup的软件将设置做成类图形界面,连防火墙都可以搞定! 新版CentOS 7针对不同的服务提供了大量的命令行设置模式,过去的setup命令已经被最近的命令取代! 这些设置工具都具有bash-complete参数自动补全功能! 如果不习惯指令,很多的图形界面也可以使用,setup的需求就减少很多了!

1.date命令

回到顶部
如果想知道当前系统的时间,可以在命令行中敲入date命令:
              peter@initroot:/home/wwwroot/$ date
              Tue Dec 31 11:24:54 CST 2019
            
更多date命令信息参考:date

date命令可以按照指定的格式显示当前时间或者设置系统时间:

peter@peter-VirtualBox:~$ date
Fri Oct 11 09:29:49 CST 2019

Usage: date [OPTION]... [+FORMAT]
or: date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]
Display the current time in the given FORMAT, or set the system date.
Mandatory arguments to long options are mandatory for short options too.
-d, --date=STRING display time described by STRING, not 'now'
--debug annotate the parsed date,
and warn about questionable usage to stderr
-f, --file=DATEFILE like --date; once for each line of DATEFILE
-I[FMT], --iso-8601[=FMT] output date/time in ISO 8601 format.
FMT='date' for date only (the default),
'hours', 'minutes', 'seconds', or 'ns'
for date and time to the indicated precision.
Example: 2006-08-14T02:34:56-06:00
-R, --rfc-email output date and time in RFC 5322 format.
Example: Mon, 14 Aug 2006 02:34:56 -0600
--rfc-3339=FMT output date/time in RFC 3339 format.
FMT='date', 'seconds', or 'ns'
for date and time to the indicated precision.
Example: 2006-08-14 02:34:56-06:00
-r, --reference=FILE display the last modification time of FILE
-s, --set=STRING set time described by STRING
-u, --utc, --universal print or set Coordinated Universal Time (UTC)
--help display this help and exit
--version output version information and exit
FORMAT controls the output. Interpreted sequences are:
%% a literal %
%a locale's abbreviated weekday name (e.g., Sun)
%A locale's full weekday name (e.g., Sunday)
%b locale's abbreviated month name (e.g., Jan)
%B locale's full month name (e.g., January)
%c locale's date and time (e.g., Thu Mar 3 23:05:25 2005)
%C century; like %Y, except omit last two digits (e.g., 20)
%d day of month (e.g., 01)
%D date; same as %m/%d/%y
%e day of month, space padded; same as %_d
%F full date; same as %Y-%m-%d
%g last two digits of year of ISO week number (see %G)
%G year of ISO week number (see %V); normally useful only with %V
%h same as %b
%H hour (00..23)
%I hour (01..12)
%j day of year (001..366)
%k hour, space padded ( 0..23); same as %_H
%l hour, space padded ( 1..12); same as %_I
%m month (01..12)
%M minute (00..59)
%n a newline
%N nanoseconds (000000000..999999999)
%p locale's equivalent of either AM or PM; blank if not known
%P like %p, but lower case
%q quarter of year (1..4)
%r locale's 12-hour clock time (e.g., 11:11:04 PM)
%R 24-hour hour and minute; same as %H:%M
%s seconds since 1970-01-01 00:00:00 UTC
%S second (00..60)
%t a tab
%T time; same as %H:%M:%S
%u day of week (1..7); 1 is Monday
%U week number of year, with Sunday as first day of week (00..53)
%V ISO week number, with Monday as first day of week (01..53)
%w day of week (0..6); 0 is Sunday
%W week number of year, with Monday as first day of week (00..53)
%x locale's date representation (e.g., 12/31/99)
%X locale's time representation (e.g., 23:13:48)
%y last two digits of year (00..99)
%Y year
%z +hhmm numeric time zone (e.g., -0400)
%:z +hh:mm numeric time zone (e.g., -04:00)
%::z +hh:mm:ss numeric time zone (e.g., -04:00:00)
%:::z numeric time zone with : to necessary precision (e.g., -04, +05:30)
%Z alphabetic time zone abbreviation (e.g., EDT)
By default, date pads numeric fields with zeroes.
The following optional flags may follow '%':
- (hyphen) do not pad the field
_ (underscore) pad with spaces
0 (zero) pad with zeros
^ use upper case if possible
# use opposite case if possible
After any flags comes an optional field width, as a decimal number;
then an optional modifier, which is either
E to use the locale's alternate representations if available, or
O to use the locale's alternate numeric symbols if available.
Examples:
Convert seconds since the epoch (1970-01-01 UTC) to a date
$ date --date='@2147483647'
Show the time on the west coast of the US (use tzselect(1) to find TZ)
$ TZ='America/Los_Angeles' date
Show the local time for 9AM next Friday on the west coast of the US
$ date --date='TZ="America/Los_Angeles" 09:00 next Fri'
GNU coreutils online help: http://www.gnu.org/software/coreutils/ Full documentation at: http://www.gnu.org/software/coreutils/date or available locally via: info '(coreutils) date invocation'

2.cal命令

回到顶部
如果想知道当前月的月历,可以在命令行中敲入cal命令:
peter@initroot:~$ cal
   December 2019      
Su Mo Tu We Th Fr Sa  
 1  2  3  4  5  6  7  
 8  9 10 11 12 13 14  
15 16 17 18 19 20 21  
22 23 24 25 26 27 28  
29 30 31             
            

cal命令用于显示日历,是calendar的缩写。默认没有参数的情况下显示当前月份

                peter@peter-VirtualBox:~/Desktop/test$ cal
                    October 2019      
                Su Mo Tu We Th Fr Sa  
                      1  2  3  4  5  
                6  7  8  9 10 11 12  
                13 14 15 16 17 18 19  
                20 21 22 23 24 25 26  
                27 28 29 30 31
              

选项:

-l:显示单月输出;
-3:显示临近三个月的日历;
-s:将星期日作为月的第一天;
-m:将星期一作为月的第一天;
-j:显示“julian”日期;
-y:显示当前年的日历。

关于cal的详细信息可参考man cal

更多cal命令信息参考:cal

3.timedatectl命令

回到顶部
date指令可以设置系统日期和时间。除了date命令,也可以使用timedatectl命令设置时间和日期, timedatectl命令不仅可以设置时间和日期,还可以显示和设置时区,以及设置ntp时间校准服务器。
timedatectl命令常用格式如下:
            [root@study ~]# timedatectl [commamd]
            
选项与参数:
list-timezones :列出系统上支持的所有时区名称
set-timezone :设置时区位置
set-time :设置时间
set-ntp :设置网络校时系统
timedatectl的时间格式可以是yyyy-mm-dd HH:MM!比较方便记忆喔!
例如将时间调整到2020年9月1号19时02分:
            [root@study ~]# timedatectl set-time "2020-09-01 19:02"
            
如果使用date修改日期,还得使用hwclock命令修正BIOS记录的时间。通过timedatectl全部搞定,方便又轻松!

4.显示与设置时区

回到顶部
地球是圆的,同一时刻每个地区的时间可能是不一样的,太平洋上有条看不见的换日线! 为了统一时间,GMT格林威治标准时间时区!我们会比较早看到太阳,中国比格林威治时间多了8小时! timedatectl命令不加任何参数可以显示当前系统所在的时区与时间等信息:
[root@study ~]# timedatectl
                      Local time: Fri 2020-02-21 17:19:15 CST
                  Universal time: Fri 2020-02-21 09:19:15 UTC
                        RTC time: Thu 2020-02-20 21:07:39
                       Time zone: Asia/Shanghai (CST, +0800)
       System clock synchronized: no
systemd-timesyncd.service active: yes
                 RTC in local TZ: no
                
list-timezones选项可以列出系统中所有的时区,配合grep命令查看是否支持New_York时区:
            [root@study ~]# timedatectl list-timezones | grep -i new
            America/New_York
            America/North_Dakota/New_Salem
            
将系统时区设置为New_York时区:
[root@study ~]# timedatectl set-timezone "America/New_York"
[root@study ~]# timedatectl
                      Local time: Fri 2020-02-21 04:52:10 EST
                  Universal time: Fri 2020-02-21 09:52:10 UTC
                        RTC time: Thu 2020-02-20 21:40:34
                       Time zone: America/New_York (EST, -0500)
       System clock synchronized: no
systemd-timesyncd.service active: yes
                 RTC in local TZ: no
            
通过上面的信息输出可以看出我们成功将系统的时区设置为New_York时区了, 现在设置回上海时区:
            [root@study ~]# timedatectl set-timezone "Asia/Shanghai"
            

5.ntp网络校时

回到顶部
系统默认的自动校时会启动NTP协议,也可以通过ntpdate命令进行手动网络校时!
            [root@study ~]# ntpdate ntp.ntsc.ac.cn
            21 Feb 18:52:28 ntpdate[7971]: step time server 114.118.7.163 offset 0.979462 sec
            [root@study ~]# hwclock -w
            
ntp.ntsc.ac.cn为中国科学院国家授时中心。hwclock将时间写入BIOS时间记录! 可以将上述任务写入crontab来自动更新系统时间!

本文由initroot编辑整理,转载请注明www.initroot.com

100次点赞 100次阅读