Linux who、tty、who am i、whoami命令
who :确定谁在系统中
uptime :了解系统启动时间和忙碌程度
top :列出 资源占用排名靠前的进程
free :了解内存使用情况
vmstat :了解系统负载情况
who命令
命令 who : 列出当前已登录入系统的用户
1 | [root@localhost ~]# who |
who命令的输出分析
第一列:用户名;
第二列:终端设备的设备文件名
第三列:该用户登录的时间
什么是设备文件
设备在文件系统中有一个文件名,设备文件一般放于目录 /dev下,与普通磁盘文件不同的是设备文件的文件类型属于特殊文件
tty命令
tty命令可以打印出当前终端的设备文件名
1 | [root@localhost ~]# tty |
who am i命令
who am i
命令可以 列出当前终端上的登录用户
1 | [root@localhost ~]# who am i |
whoami命令
命令whoami
仅列出当前终端上的登录用户
1 | [root@localhost ~]# whoami |
Linux passwd命令
passwd更换口令
普通用户
使用 passwd 命令更改自己 的口令,在更改之前,系统会先验证原来的口令,通过口令之后才能修改口令。
超级用户root
修改口令之前不验证旧的口令。
root用户可修改自己的口令,还可强迫设置其它用户口令
例如root用户命令执行如下命令:
1 | passwd liu |
可以将用户liu的口令强迫设置为某一已知口令。
超级用户无法读取其它用户的口令
当普通用户忘记口令时,超级用户是无法帮该普通用户查出他原先的口令的,此时可请求超级用户把该用户的口令强制设置为一个新的口令。该普通用户使用新的口令登录后,再修改成更新的口令即可,以免让超级用户知道自己的口令。
修改root的口令后不要急于退出当前终端
修改超级用户root的口令后,先不要急于退出当前的终端,先尝试从别的终端登录root用户,如果发现无法登录,则表示设置的口令有误。此时在当前终端重设root用户的口令即可。
如果修改root的口令后直接退出当前终端,然后再重新登录,如果无法登录,那么将无法再次修改root的口令。
口令的设置与验证
口令信息的保存
不保存明码口令
设置口令的时候系统会生成一个随机序列,然后把该随机序列和用户输入的口令通过哈希算法进行计算。得到一个固定长度的哈希值。
系统保存的是生成的这个随机序列,以及计算后的哈希值A。
这样即使你知道随机序列和计算后的哈希值A,也是无法反推出用户的口令的。
口令的验证过程
当用户登录的时候,再次把随机序列和用户口令通过哈希算法进行计算,得到一个哈希值B,然后把这个哈希值B与保存的哈希值A进行比较。如果两个哈希值一样,则表示口令正确。
这样做是安全的,因为无法根据随机值和哈希值,倒推出用户的口令。
这种方式也可以用在其他的软件设计中
Linux计算器bc
Linux计算器bc
功能强大
- 基本计算器功能
- 支持变量、函数,条件和循环 等编程 功能(类似 C 语言的小编程语言)
- 可以进行任意精度的 计算
精度
命令设置缺省精度
打开计算器的命令 | 描述 |
---|---|
bc | 缺省精度为小数点后 0 位 |
bc l | 缺省精度 为小数点后 20 位 |
整数计算
1 | [root@localhost ~]# bc |
小数计算
1 | [root@localhost ~]# bc -l |
可以通过设置scale自行决定精度(小数点位数)
设置bc计算器精确到小数点后10000位
1 | scale=10000 |
计算效果:
1 | [root@localhost ~]# bc |
设置bc计算器精确到小数点后30位
1 | scale=30 #输入 |
bc编程
使用变量
查看变量
在bc中输入变量的值,然后在输入变量名即可查看该变量的值:
1 | p=20 |
运行效果
1 | p=20 |
使用变量进行计算
1 | [root@localhost ~]# bc |
使用循环
求1~128的阶乘,在bc中分别输入如下三行代码:
1 | s=1; |
计算结果:
1 | [root@localhost ~]# bc |
Linux cal命令打印日历
cal打印当前月的日历
直接输入cal命令会打印当前月的日历,
运行效果
1 | [root@localhost ~]# cal |
cal year打印指定年的日历
1 | [root@localhost ~]# cal 2020 |
cal month year打印指定年月的日历
1 | [root@localhost ~]# cal 6 2020 |
Linux ntpdate命令
ntpdate
NTP协议
NTP是什么
NTP 是网络时间协议(Network Time Protocol),它用来同步网络设备【如计算机、手机】的时间的协议。
NTP实现什么目的
目的很简单,就是为了提供准确时间。因为我们的手表、手机、电脑等设备,经常会跑着跑着时间就出现了误差,或快或慢的少几秒,时间长了甚至误差过分钟。
NTP服务器列表
Windows系统上自带的两个:time.windows.com 和 time.nist.gov
MacOS上自带的两个:time.apple.com 和 time.asia.apple.com
NTP授时快速域名服务:cn.ntp.org.cn
1、阿里云授时服务器
1 | #NTP服务器 |
2、国内大学授时服务器
1 | s1c.time.edu.cn 北京大学 |
3、国外授时服务器
1 | #苹果提供的授时服务器 |
通过 NTP 协议校对系统时间
1 | ntpdate |
如果没有安装ntpdate,查找安装包:
1 | yum list ntpdate |
安装ntpdate:
1 | yum install ntpdate |
1 | [root@localhost ~]# yum list ntpdate |
查询时间ntpdate -q NTP服务器
1 | ntpdate -q 0.pool.ntp.org |
运行效果:
1 | [root@localhost ~]# ntpdate -q 0.pool.ntp.org |
使用阿里云
1 | ntpdate -q ntp.aliyun.com |
运行效果:
1 | [root@localhost ~]# ntpdate -q ntp.aliyun.com |
运行效果:
1 | [root@localhost ~]# ntpdate -q cn.ntp.org.cn |
设置时间ntpdate NTP服务器
1 | ntpdate ntp.aliyun.com |
运行结果:
1 | [root@localhost ~]# ntpdate ntp.aliyun.com |
参考资料
Linux date命令
读取系统日期和时间命令date
1 | date |
运行结果:
1 | [root@localhost ~]# date |
定制date输出格式
1 | date "+%Y-%m-%d %H:%M:%S Day %j" |
运行结果:
1 | [root@localhost ~]# date "+%Y-%m-%d %H:%M:%S Day %j" |
110 指 的是今天是今年的 第 110 天,
date格式控制字符串
第一个字符必须为+
号,%Y
代表年号,%m
代表月份,%M
代表分钟。%H
表示小时%s
表示秒坐标(从 UTC1970 开始),或者叫时间戳常用于计算时间间隔。
1 | date "+%s" |
运行结果
1 | [root@localhost ~]# date "+%s" |
man date
Linux 命令 往往有很多选项和复杂的功能,通过 man date 查阅联机手册
1 | DATE(1) FSF DATE(1) |
date -d String
显示字符串对应的时间
1 | [root@localhost ~]# date -d 2014-2-7 |
date -f 日期文件
解析日期文件中的每个日期
1 | [root@localhost Linux_Test]# cat date_test.txt |
显示date_test.txt文件中每行日期对应的具体时间:
1 | [root@localhost Linux_Test]# date -f date_test.txt |
date -r 文件
显示文件最新更改时间。
1 | [root@localhost Linux_Test]# ls |
date -R
1 | [root@localhost Linux_Test]# date -R |
date -s 日期时间字符串
根据传入的日期时间字符串设置系统的时间
date -u
显示格林威治时间
1 | [root@localhost Linux_Test]# date -u |
markdown表格中如何写竖线
如何在markdown表格中填写Linux管道符号
问题描述
例如我想在表格中写命令:
1 | last -f /var/log/btmp | more |
但是命令中的管道符|
和markdown表格的分隔符相同。
这就会造成,解析后的markdown把管道符错误的识别成单元格的分隔符的情况:
错误示例
/var/log子目录 | 描述 |
---|---|
/var/log/alternatives.log | 更新替代信息都记录在这个文件中。 |
/var/log/btmp | 记录所有失败登录信息。使用last命令可以查看btmp文件。例如,”last -f /var/log/btmp |
/var/log/user.log | 记录所有等级用户信息的日志。 |
使用html转义符
解决方案,使用html转义符,竖线|
的html转义符为|
。
也就是说把上面的命令改成如下即可:
1 | last -f /var/log/btmp | more |
/var/log子目录 | 描述 |
---|---|
/var/log/alternatives.log | 更新替代信息都记录在这个文件中。 |
/var/log/btmp | 记录所有失败登录信息。使用last命令可以查看btmp文件。例如,”last -f /var/log/btmp | more”。 |
/var/log/user.log | 记录所有等级用户信息的日志。 |
参考资料
Linux find命令详解
不递归查找 find -maxdepth 1
例如只查找当前目录下的文件:
(比StartWriting.bat新,比HexoS.bat旧的文件)
1 | find . -maxdepth 1 -newer StartWriting.bat \! \( -newer HexoS.bat \) |
运行效果:
1 | [root@localhost exam]# ls -ltr --full-time |
Java正则表达式Matcher类
Matcher类
Matcher对象是一个状态机器,它依据Pattern对象做为匹配模式对字符串展开匹配检查,此类的实例用于多个并发线程是不安全的。
方法 | 描述 |
---|---|
String group(int group) |
Returns the input subsequence captured by the given group during the previous match operation. |
String group(String name) |
Returns the input subsequence captured by the given named-capturing group during the previous match operation. |
int start(int group) |
Returns the start index of the subsequence captured by the given group during the previous match operation. |
int end(int group) |
Returns the offset after the last character of the subsequence captured by the given group during the previous match operation. |
int end(String name) |
Returns the offset after the last character of the subsequence captured by the given named-capturing group during the previous match operation. |
boolean find(int start) |
Resets this matcher and then attempts to find the next subsequence of the input sequence that matches the pattern, starting at the specified index. |
int groupCount() |
Returns the number of capturing groups in this matcher’s pattern. |
boolean hasAnchoringBounds() |
Queries the anchoring of region bounds for this matcher. |
boolean hasTransparentBounds() |
Queries the transparency of region bounds for this matcher. |
boolean hitEnd() |
Returns true if the end of input was hit by the search engine in the last match operation performed by this matcher. |
boolean lookingAt() |
Attempts to match the input sequence, starting at the beginning of the region, against the pattern. |
boolean matches() |
Attempts to match the entire region against the pattern. |
Pattern pattern() |
Returns the pattern that is interpreted by this matcher. |
static String quoteReplacement(String s) |
Returns a literal replacement String for the specified String. |
Matcher region(int start, int end) |
Sets the limits of this matcher’s region. |
int regionEnd() |
Reports the end index (exclusive) of this matcher’s region. |
int regionStart() |
Reports the start index of this matcher’s region. |
String replaceAll(String replacement) |
Replaces every subsequence of the input sequence that matches the pattern with the given replacement string. |
String replaceAll(Function<MatchResult,String> replacer) |
Replaces every subsequence of the input sequence that matches the pattern with the result of applying the given replacer function to the match result of this matcher corresponding to that subsequence. |
String replaceFirst(String replacement) |
Replaces the first subsequence of the input sequence that matches the pattern with the given replacement string. |
String replaceFirst(Function<MatchResult,String> replacer) |
Replaces the first subsequence of the input sequence that matches the pattern with the result of applying the given replacer function to the match result of this matcher corresponding to that subsequence. |
boolean requireEnd() |
Returns true if more input could change a positive match into a negative one. |
Matcher reset() |
Resets this matcher. |
Matcher reset(CharSequence input) |
Resets this matcher with a new input sequence. |
Stream<MatchResult> results() |
Returns a stream of match results for each subsequence of the input sequence that matches the pattern. |
int start(String name) |
Returns the start index of the subsequence captured by the given named-capturing group during the previous match operation. |
MatchResult toMatchResult() |
Returns the match state of this matcher as a MatchResult. |
String toString() |
Returns the string representation of this matcher. |
Matcher useAnchoringBounds(boolean b) |
Sets the anchoring of region bounds for this matcher. |
Matcher usePattern(Pattern newPattern) |
Changes the Pattern that this Matcher uses to find matches with. |
Matcher useTransparentBounds(boolean b) |
Sets the transparency of region bounds for this matcher. |
方法 | 描述 |
---|---|
boolean find() |
Attempts to find the next subsequence of the input sequence that matches the pattern. |
int end() |
Returns the offset after the last character matched. |
Matcher appendReplacement(StringBuffer sb, String replacement) |
Implements a non-terminal append-and-replace step. |
Matcher appendReplacement(StringBuilder sb, String replacement) |
Implements a non-terminal append-and-replace step. |
StringBuffer appendTail(StringBuffer sb) |
Implements a terminal append-and-replace step. |
StringBuilder appendTail(StringBuilder sb) |
Implements a terminal append-and-replace step. |
int start() |
Returns the start index of the previous match. |
String group() |
Returns the input subsequence matched by the previous match. |
小结
find()
方法匹配子串的时候都会记下本次成功匹配子串的开始位置
和结束位置
appendReplacement(sb
,replacement)
方法先获取所有从上次匹配的结束位置,到本次匹配结束位置的所有字符串
,然后替换其中匹配的部分
,最后追加到sb
中。appendReplacement(sb)
方法把结束位置到文本结尾的所有字符追加到sb
中。- 使用appendReplace()和appendTail()方法可以更加灵活的进行替换操作,注意要结合find()方法使用。