//TODO
这篇总结是来自PluralSight上的LPIC-1
课程的Essential章节。
备注:2020年4月份pluralsight在搞活动,免费注册学习!这次lock down是个机会补补课。
Environment: CentOS 7 Enterprise Linux
or RedHat
.
Essentials
Reading OS data
1 | # system version |
Shutdown
Send message to others
1 | # send to individual user terminal |
Shutdown system and prompt
1 | # reboot now |
Changing runlevels
what is runlevel
in linux?
https://www.liquidweb.com/kb/linux-runlevels-explained/
比如
runlevel 1 就只能root user且没有network enabled,也叫作rescue.target,可以做一些需要隔离的操作。
runlevel 3 是默认的multi-user + network enabled (多数情况是这个状态)
runlevel 5 是Desktop interface + runlevel 3的组合。
1 | # show current runlevel |
More about systemd, see my systemd blog.
Manage processes
1 | # show process on current shell |
$$
the PID of current running process
1 | cd /proc/$$ |
top
命令的options还记得吗? 比如切换memory显示单位,选择排序的依据CPU/MEM occupied…
Process priority
if something runs in foreground and prevent you from doing anything, use ctrl+z
to suspend it (still in memory, not takeing CPU time), then put it in background.
1 | sleep 10000 |
如果你在一个bash shell中sleep 1000& 然后exit bash shell,则这个sleep process will hand over to init process. can check via ps -F -p $(pgrep sleep)
, 会发现PPID是1
了。进入另一个bash shell jobs
并不会显示之前bash shell的background process.
1 | # show PRI(priority) and NI(nice) number |
PRI value for real time is from [60,99] and [100,139] for users, the bigger the better. NI value is from [-20,19], higher the nicer so less CPU time to take. 在相同PRI 之下,NI 决定了多少资源.
比如说你有一个build task并不urgent, 不想它在后台占用太多资源,可以设置nice value.
1 | # set nice value to 19 |
要注意的是只有root可以设置负数nice value和降低nice value. root可以去vim /etc/security/limits.conf
设置对不同user/group的nice value。
Monitor linux performance
这个很重要,一般关注网络,硬盘,CPU
List content of the package procps-ng
, procps
is the package that has a bunch of small useful utilities that give information about processes using the /proc
filesystem. The package includes the programs ps, top, vmstat, w, kill, free, slabtop, and skill.
1 | # see executable files under procps package via rpm |
Introduce 2 new commands: pmap
and pwdx
1 | # pmap, show memory map of a process |
1 | # check how long the system has been running |
监控load or output
1 | # execute a program periodically, showing output fullscreen |
1 | # -b 使用batch mode 输出所有process情况 |
sysstat toolkit
The package contains many performance measurement tools.
Install sysstat
(a bunch of command: iostat
, netstat
, etc).
1 | yum install -y sysstat |
The config file for sysstat can be found by:
1 | # -q: query |
在安装后,其实用的cron在背后操作收集数据, configuration is in file cat /etc/sysconfig/sysstat
,这里面可以设置记录的周期,默认是28天。
1 | # cron config for sysstat |
start and enable:
1 | systemctl start sysstat |
来看看sysstat下的工具命令:
1 | # show in mega byte |
Let’s see sar
(system activity report), gather statistics and historical data, 通过分析一天的bottleneck(cpu/memory/disk/network/loadavg)可以更好的schedule任务,比如发现某个时间cpu, memory的使用比较多。这里并没有深入讲解怎么解读这些数据,并且你需要了解各个部分数据的含义,以及什么样的数据可能是异常.
sar
的数据在/var/log/sa
里面,每天一个文件,周期性覆盖。
1 | # sar specific processor, cpu 0/cpu1 |
图形化sar数据,可以用ksar:https://www.cyberciti.biz/tips/identifying-linux-bottlenecks-sar-graphs-with-ksar.html
Log and logrotate
Auditing login events,这个还挺有用的,看哪个user什么时候login了, w
是查看当前哪些user正在使用中。
1 | # see user login info |
Auditing root access,看su/sudo的使用情况,在/var/log/secure
文件中,这里其实有多个secure文件,有日期区分。
1 | # there are some secure and auditing files |
我会专门总结一下awk的笔记,这个挺有用的。
journalctl
是一个常用的system log查询工具。当时查看一些docker的log在里面也能看到。
1 | # show last 10 lines |
Selinux
O’Reilly有过相关的课程,在我工作邮件中连接还在。目前只需要知道什么是selinux,如何打开,关闭它即可。
SELINUX= can take one of these three values:
enforcing
- SELinux security policy is enforced.
permissive
- SELinux prints warnings instead of enforcing.
disabled
- No SELinux policy is loaded.
1 | # see if selinux is permissive, enforcing or disabled |
如果最开始是disabled的,则要去config file /etc/selinux/config
设置permissive,然后重启。
不能setenforce 去disable,也只能在config文件中disable然后重启机器。
1 | # setenforce [ Enforcing 1| Permissive 0] |
显示selinux的labels, flag Z
对于其他命令也有用。
1 | # see user selinux config |