这个课程的收获就是boot stages, kernel的升级以及linux logging的种类,使用。特别是journald,来自systemd。我从其他文章中补充了一些内容,包括loginctl.
Boot
Linux booting process:
- firmware stage (BIOS or UEFI)
- boot loader stage (grub2)
- kernel stage (ramdisk -> root filesystem)
- initialization stage (systemd)
/boot
directory is about kernel.
grub configuration file:
1 | # providing boot menu and excuting kernel |
讲了一下如何定制grub 的kernel 菜单选项 to add custom boot entry,grub 的菜单会在开机时的图形界面显示。可以在开机时更改kernel line加入systemd rescue or emergency target, refer here:
Kernel
Upgrade kernel version for CentOS:
1 | # uname -r |
The above steps usually cannot help much because the lack of latest version in official repo. We need third-party repo, see this artical for help: How to Upgrade Linux Kernel in CentOS 7
Because we use SSH session to upgrade the kernal, so we are not able to select the kernel version on boot menu, we can do it by configuring the grub2:
1 | # check kernel index list, index starts from 0 |
Switch to 5.4.125-1
version:
1 | # set kernel index 0 |
After rebooting, check the kernel version:
1 | uname -r |
Switch back to old kernel version is easy:
1 | # # set kernel index 2, see above index list |
注意kernel version 和 OS version不一样,比如查看CentOS OS version:
1 | cat /etc/centos-release |
Linux Logging
Linux has 2 logging systems,这 2 个logging systems can run parallelly, or you can use journal alone.
- rsyslog (persistent logs, can log
remotely
) - journald (nonpersistent by default)
syslog vs rsyslog vs syslog-ng: Basically, they are all the same, in the way they all permit the logging of data from different types of systems in a central repository, each project trying to improve the previous one with more reliability and functionalities.
Different logs for differnet purpose, some for failed jobs, some for cron jobs, etc. The rsyslog is a daemon:
1 | systemctl status rsyslog |
/etc/rsyslog.conf
is the configuration file, see section under #### RULES ####
. For example, anything beyond mail, authpriv and cron is logged in /var/log/messages
, in the below file, cron.*
means messages of all priorities will be logged (debug, info, notice, warn, err, crit, alert, emerg), cron.warn
will log warn and above:
1 | #### RULES #### |
Log rotate config is in /etc/logrotate.conf
and there is a cron job for rotate /etc/cron.daily/logrotate
.
If you want to log message to system log file, use logger
command:
1 | # you will see it in /var/log/messages |
How to search and view rsyslog, see this article:
Linux uses a set of configuration files, directories, programs, commands and daemons to create, store and recycle these log messages.
The default location for log files in Linux is /var/log
.
If you check with ls -ltr -S /var/log
, the lastlog
file may have a big size, way bigger than the disk space, it is a sparse file.
At the heart of the logging mechanism is the rsyslog daemon. This service is responsible for listening to log messages from different parts of a Linux system and routing the message to an appropriate log file in the /var/log directory. It can also forward log messages to another Linux server.
/var/log/messages
可以用vim等工具正常查看. command who
, last
其实是使用了/var/run/utmp
and /var/run/wtmp
journalctl
have the same logs as in rsyslogd
, from here, persistent journal can replace rsyslogd.
1 | # persist journald by making a dir |
Or enable in /etc/systemd/journald.conf
, set Storage=persistent
.
You can specify date ranges:
1 | journalctl --since "2020-12-11 15:44:32" |
Some useful commands:
1 | # list boots |
You can use right arrow key
to see full entry if it is too long.
1 | # print all on stdout, no pager with less |
Linux Session
Other capabilities, like log management and user sessions are handled by separated daemons and management utilities (journald/journalctl
and logind/loginctl
respectively).
Get info about user and the processes he is running before:
1 | # list sessions |