一、系统时间管理
在Linux中有硬件时钟与系统时钟等两种时钟。硬件时钟是指主机板上的时钟设备,也就是通常可在BIOS画面设定的时钟;系统时钟则是指kernel中 的时钟;所有Linux相关指令与函数都是读取系统时钟的设定
当Linux启动时,系统时钟会去读取硬件时钟的设定,之后系统时钟即独立运作
1、两种时间
查看硬件时间:hwclock
[root@CHENDAJIE chendajie]# hwclock
2019年09月28日 星期六 15时36分21秒 -0.037281 秒
【hwclock】
-w:将系统时间写到硬件上去
-s:将硬件时间写到系统上去
查看系统时间:date
[root@CHENDAJIE chendajie]# date
2019年 09月 28日 星期六 15:39:34 CST
时区:
UTC (Universal Time Coordinated):世界标准时间
GMT (Greenwich Mean Time):格林尼治时间
CST (China standard Time):中国标准时间
图中代码显示的是CST中国标准时间。
如何修改时间
date命令相关参数:
date --help
-s, --set=STRING 把时间设为字符串所描述的时间
例:
[root@CHENDAJIE chendajie]# date -s "2019-9-28 14:44"
2019年 09月 28日 星期六 15:44:00 CST
%F 完整日期格式,等价于 %Y-%m-%d
[root@CHENDAJIE chendajie]# date "+%F"
2019-09-28
%y 年份最后两位数位 (00-99)
[root@CHENDAJIE chendajie]# date "+%y"
19
%Y 年份
[root@CHENDAJIE chendajie]# date "+%Y"
2019
%m month (01…12)
[root@CHENDAJIE chendajie]# date "+%m"
09
%d 按月计的日期(例如:01)
[root@CHENDAJIE chendajie]# date "+%d"
28
%M minute (00…59)
[root@CHENDAJIE chendajie]# date "+%M"
47
%H 小时(00-23)
[root@CHENDAJIE chendajie]# date "+%H"
15
%S 秒(00-60)
[root@CHENDAJIE chendajie]# date "+%S"
18
使用time命令测试一个命令运行的时间
time:一般用来测量一个命令的运行时间
使用方法:time [options] command [arguments…]
[root@CHENDAJIE chendajie]# time find /etc/passwd
/etc/passwd
real 0m0.002s #实际使用时间
user 0m0.002s #用户状态使用的时间
sys 0m0.000s #内核状态使用的时间
帮助命令使用
man: 格式化并显示在线帮助手册页
例:
[root@CHENDAJIE chendajie]# man find
FIND(1) General Commands Manual FIND(1)
NAME
find - 递归地在层次目录中处理文件
总览 SYNOPSIS
find [path...] [expression]
...
使用-h或–help查看命令选项
find --help
root@CHENDAJIE chendajie]# find --help
用法: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]
默认路径为当前目录;默认表达式为 -print
表达式可能由下列成份组成:操作符、选项、测试表达式以及动作:
操作符 (优先级递减;未做任何指定时默认使用 -and):
( EXPR ) ! EXPR -not EXPR EXPR1 -a EXPR2 EXPR1 -and EXPR2
EXPR1 -o EXPR2 EXPR1 -or EXPR2 EXPR1 , EXPR2
...
开关机命令及七个启动级别:
常用的关机、重启命令
shutdown
init
reboot
poweroff
shutdown命令
一种安全的方式关闭系统。所有登陆用户都可以看到关机信息提示
。
作用:关机、重启、定时关机
语法:shutdown [选项]
参数:
-r => 重新启动计算机
-h => 关机
-h 时间 =>定时关机
例:
#十分钟以后关机
[root@CHENDAJIE chendajie]# shutdown -h +10 #十分钟以后关机
Shutdown scheduled for 六 2019-09-28 16:18:19 CST, use 'shutdown -c' to cancel.
[root@CHENDAJIE chendajie]#
Broadcast message from root@CHENDAJIE.cn (Sat 2019-09-28 16:08:19 CST):
The system is going down for power-off at Sat 2019-09-28 16:18:19 CST!
#指定具体的时间点进行关机
[root@CHENDAJIE chendajie]# shutdown -h 23:30 #指定具体的时间点进行关机
Shutdown scheduled for 六 2019-09-28 23:30:00 CST, use 'shutdown -c' to cancel.
#取消关机
[root@CHENDAJIE chendajie]# shutdown -c #取消关机
Broadcast message from root@CHENDAJIE.cn (Sat 2019-09-28 16:10:02 CST):
The system shutdown has been cancelled at Sat 2019-09-28 16:11:02 CST!
#立即关机
[root@CHENDAJIE chendajie]# shutdown -h now #立即关机
#22:22以后重启
[root@CHENDAJIE chendajie]# shutdown -r 22:22 #22:22以后重启
Shutdown scheduled for 六 2019-09-28 22:22:00 CST, use 'shutdown -c' to cancel.
7个启动级别
作用:切换系统运行级别
语法:init 0-6
Linux 7个启动级别:
0 系统停机模式,系统默认运行级别不能设置为0,否则不能正常启动,机器关的
1 单用户模式,root权限,用于系统维护,禁止远程登陆,就像Windows下的安全模式登录
2 多用户模式,没有NFS和网络支持
3 完整的多用户文本模式,有NFS和网络,登陆后进入控制台命令行模式
4 系统未使用,保留一般不用,在一些特殊情况下可以用它来做一些事情。例如在笔记本电脑的电池用尽时,可以切换到这个模式来做一些设置
5 图形化模式,登陆后进入图形GUI模式,X Window系
6 重启模式,默认运行级别不能设为6,否则不能正常启动。运行init 6机器就会重启
例
[chendajie@CHENDAJIE ~]# init 0 #关机
[chendajie@CHENDAJIE ~]# init 3 #进入3级别字符界面
[chendajie@CHENDAJIE ~]# init 5 #进入5级别图形界面
3.5.3 设置默认的运行界别
centos7不再使用/etc/inittab文件进行默认的启动级别配置,而使用比sysvinit的运行级更为自由的target替代。
第3运行级用multi-user.target替代。
第5运行级用graphical.target替代。
设置默认第三启动级别
[chendajie@CHENDAJIE ~]$ systemctl set-default multi-user.target
Removed symlink /etc/systemd/system/default.target.
Created symlink from /etc/systemd/system/default.target to /usr/lib/systemd/system/multi-user.target.
设置默认第五启动级别
[chendajie@CHENDAJIE ~]$ systemctl set-default graphical.target
Removed symlink /etc/systemd/system/default.target.
Created symlink from /etc/systemd/system/default.target to /usr/lib/systemd/system/graphical.target.
[chendajie@CHENDAJIE ~]$ runlevel
3 5 #表示从3级别切换到了5级别
查看当前默认的启动级别
[chendajie@CHENDAJIE ~]$ systemctl get-default
graphical.target