linux系统日志

本文涉及的产品
日志服务 SLS,月写入数据量 50GB 1个月
简介:

##日志记录系统每天发生的各种各样的事情,比如监控系统的状况,排查系统的故障等。你可以通过日志来检查错误发生的原因,或者受到攻击时留下的痕迹。日志的主要功能是审计和监测,还有实时的监测系统状态,监测和追踪入侵者

## 经常查看的系统文件是 /var/log/message,它是系统核心日志文件

Linux系统日志

./var/log/messages

./etc/logrotate.conf 日志分割文件

./var/log/dmesg

./last 命令,调用的是/var/log/wtmp

./lastb命令查看登录失败的用户,对应的文件是/var/log/btmp

./var/logsecure



/var/log/messages  这个是经常查看的日志文件  核心系统日志文件,包含启动时间的引导消息,以及系统运行的其他状态消息,I/O错误 网络错误和其他系统错误都会记录到这个文件下

[root@yzllinux123 ~]# less /var/log/messages    #查看系统日志文件的信息
Jan 29 07:01:01 yzllinux123 systemd: Started Session 3 of user root.
Jan 29 07:01:01 yzllinux123 systemd: Starting Session 3 of user root.
Jan 29 07:12:33 yzllinux123 systemd-logind: Removed session 1.
Jan 29 07:12:33 yzllinux123 systemd: Removed slice User Slice of root.
Jan 29 07:12:33 yzllinux123 systemd: Stopping User Slice of root.
Jan 29 07:12:40 yzllinux123 systemd: Created slice User Slice of root.
Jan 29 07:12:40 yzllinux123 systemd: Starting User Slice of root.
Jan 29 07:12:40 yzllinux123 systemd: Started Session 4 of user root.
Jan 29 07:12:40 yzllinux123 systemd-logind: New session 4 of user root.


/etc/logrotate.conf  #日志切割配置文件

[root@yzllinux123 ~]# cat /etc/logrotate.conf  #查看日志切割配置文件内容
# see "man logrotate" for details
# rotate log files weekly
weekly

# keep 4 weeks worth of backlogs
rotate 4

# create new (empty) log files after rotating old ones
create

# use date as a suffix of the rotated file
dateext

# uncomment this if you want your log files compressed
#compress

# RPM packages drop log rotation information into this directory
include /etc/logrotate.d

# no packages own wtmp and btmp -- we'll rotate them here
/var/log/wtmp {
    monthly
    create 0664 root utmp
 minsize 1M
    rotate 1
}

/var/log/btmp {
    missingok
    monthly
    create 0600 root utmp
    rotate 1
}

# system-specific logs may be also be configured here.


[root@yzllinux123 ~]# cat /etc/logrotate.conf  #查看日志切割配置文件
# see "man logrotate" for details
# rotate log files weekly
weekly

# keep 4 weeks worth of backlogs
rotate 4

# create new (empty) log files after rotating old ones
create

# use date as a suffix of the rotated file
dateext

# uncomment this if you want your log files compressed
#compress

# RPM packages drop log rotation information into this directory
include /etc/logrotate.d

# no packages own wtmp and btmp -- we'll rotate them here
/var/log/wtmp {
    monthly
    create 0664 root utmp
 minsize 1M
    rotate 1
}

/var/log/btmp {
    missingok
    monthly
    create 0600 root utmp
    rotate 1
}

# system-specific logs may be also be configured here.

[root@yzllinux123 ~]# du -sh /etc/logrotate.conf   #du -sh 查看切割文件的大小
4.0K /etc/logrotate.conf

dmesg 命令  它可以显示系统的启动信息,如果你的某个硬件有问题(比如有网卡),这个命令也可以看到

[root@yzllinux123 ~]# dmesg |head   #列出系统硬件信息
[    0.000000] Initializing cgroup subsys cpuset
[    0.000000] Initializing cgroup subsys cpu
[    0.000000] Initializing cgroup subsys cpuacct
[    0.000000] Linux version 3.10.0-123.el7.x86_64 (builder@kbuilder.dev.centos.org) (gcc version 4.8.2 20140120 (Red Hat 4.8.2-16) (GCC) ) #1 SMP Mon Jun 30 12:09:22 UTC 2014
[    0.000000] Command line: BOOT_IMAGE=/vmlinuz-3.10.0-123.el7.x86_64 root=UUID=50cdeab8-cfd2-475a-b77a-8f9e904b4fa6 ro vconsole.keymap=us crashkernel=auto vconsole.font=latarcyrheb-sun16 rhgb quiet.UTF-8
[    0.000000] Disabled fast string operations
[    0.000000] e820: BIOS-provided physical RAM map:
[    0.000000] BIOS-e820: [mem 0x0000000000000000-0x000000000009efff] usable
[    0.000000] BIOS-e820: [mem 0x000000000009f000-0x000000000009ffff] reserved
[    0.000000] BIOS-e820: [mem 0x00000000000ca000-0x00000000000cbfff] reserved
[root@yzllinux123 ~]# ^C


/var/log/dmesg  日志  #  记录系统启动的日志

[root@yzllinux123 ~]# cat /var/log/dmesg |head    #系统启动日志
[    0.000000] Initializing cgroup subsys cpuset
[    0.000000] Initializing cgroup subsys cpu
[    0.000000] Initializing cgroup subsys cpuacct
[    0.000000] Linux version 3.10.0-123.el7.x86_64 (builder@kbuilder.dev.centos.org) (gcc version 4.8.2 20140120 (Red Hat 4.8.2-16) (GCC) ) #1 SMP Mon Jun 30 12:09:22 UTC 2014
[    0.000000] Command line: BOOT_IMAGE=/vmlinuz-3.10.0-123.el7.x86_64 root=UUID=50cdeab8-cfd2-475a-b77a-8f9e904b4fa6 ro vconsole.keymap=us crashkernel=auto vconsole.font=latarcyrheb-sun16 rhgb quiet.UTF-8
[    0.000000] Disabled fast string operations
[    0.000000] e820: BIOS-provided physical RAM map:
[    0.000000] BIOS-e820: [mem 0x0000000000000000-0x000000000009efff] usable
[    0.000000] BIOS-e820: [mem 0x000000000009f000-0x000000000009ffff] reserved
[    0.000000] BIOS-e820: [mem 0x00000000000ca000-0x00000000000cbfff] reserved


last 命令  查看正确的登录

[root@yzllinux123 ~]# last |head  #查看正确的登录历史  谁 怎么登录 时间 等
root     pts/0        192.168.12.1     Thu Feb  1 05:00   still logged in   
reboot   system boot  3.10.0-123.el7.x Thu Feb  1 04:59 - 06:04  (01:05)    
root     pts/0        192.168.12.1     Mon Jan 29 07:12 - crash (2+21:46)   
root     pts/0        192.168.12.1     Mon Jan 29 05:01 - 07:12  (02:10)    
reboot   system boot  3.10.0-123.el7.x Mon Jan 29 05:01 - 06:04 (3+01:03)   
root     pts/0        192.168.12.1     Fri Jan 26 08:00 - crash (2+21:00)   
reboot   system boot  3.10.0-123.el7.x Fri Jan 26 07:58 - 06:04 (5+22:05)   
root     tty1                          Fri Jan 26 07:58 - 07:58  (00:00)    
root     pts/0        192.168.12.1     Fri Jan 26 07:29 - down   (00:28)    
reboot   system boot  3.10.0-123.el7.x Fri Jan 26 07:28 - 07:58  (00:29)


lastb  #记录的是登录失败的信息

[root@yzllinux123 ~]# lastb |head  #登录错误的信息

btmp begins Thu Feb  1 05:32:02 2018


/var/log/secure  #记录验证和授权等方面的信息

[root@yzllinux123 ~]# cat /var/log/secure
Jan 29 07:12:33 yzllinux123 sshd[2131]: pam_unix(sshd:session): session closed for user root
Jan 29 07:12:40 yzllinux123 sshd[2538]: Accepted password for root from 192.168.12.1 port 52861 ssh2
Jan 29 07:12:40 yzllinux123 sshd[2538]: pam_unix(sshd:session): session opened for user root by (uid=0)
Feb  1 04:59:25 yzllinux123 polkitd[677]: Loading rules from directory /etc/polkit-1/rules.d
Feb  1 04:59:25 yzllinux123 polkitd[677]: Loading rules from directory /usr/share/polkit-1/rules.d
Feb  1 04:59:25 yzllinux123 polkitd[677]: Finished loading, compiling and executing 2 rules
Feb  1 04:59:25 yzllinux123 polkitd[677]: Acquired the name org.freedesktop.PolicyKit1 on the system bus
Feb  1 04:59:31 yzllinux123 sshd[1107]: Server listening on 0.0.0.0 port 22.
Feb  1 04:59:31 yzllinux123 sshd[1107]: Server listening on :: port 22.
Feb  1 05:00:11 yzllinux123 sshd[2128]: Accepted password for root from 192.168.12.1 port 51159 ssh2
Feb  1 05:00:11 yzllinux123 sshd[2128]: pam_unix(sshd:session): session opened for user root by (uid=0)


screen 工具  (虚拟终端)

为了不让一个任务中断 除了我们一直在线  还可以把任务丢在后台运行  使用:nohup +运行的日志 +&


还有一个办法 就是 screen  虚拟终端

首先我们需要安装screen  # yum install -y screen

安装完成之后,我们只需要输入#screen  然后回车 就进入虚拟终端

然后按Ctrl +A键 在按d 键退出screen会话

#screen -ls #是查看已经打开的screen 会话

#screen -r +编号  #是再次打开screen会话

#exit 是结束screen会话

本文转自 yzllinux博客,原文链接:    http://blog.51cto.com/12947851/2067548    如需转载请自行联系原作者


相关实践学习
【涂鸦即艺术】基于云应用开发平台CAP部署AI实时生图绘板
【涂鸦即艺术】基于云应用开发平台CAP部署AI实时生图绘板
相关文章
|
消息中间件 数据采集 监控
ELK搭建(五):linux系统日志监控平台搭建
现在的生产系统多使用linux系统,在实际生产过程中我们除了需要监控一些业务日志之外,有时也需要监控linux系统本身的日志,来帮助我们进行一些排错和判断。那么这一期,我们就针对linux系统日志监控平台的搭建来进行讲解 与往期一样,我们针对实际搭建教程更多是快速搭建为主,不做过多的原理性讲解,这一类讲解我们放到后期单独开几期博客来探讨。
1366 0
ELK搭建(五):linux系统日志监控平台搭建
|
存储 监控 安全
Linux系统日志
【4月更文挑战第6天】Linux系统日志,关键用于记录系统状态和事件,包括内核、系统、安全和应用日志,助力管理员诊断问题、确保系统正常运行。日志管理涉及收集、分析、备份等,常用工具如rsyslog、systemd-journal和logrotate(用于日志轮转)。重视日志文件的存储管理,防止空间占用过多。
198 1
Linux系统日志
|
存储 监控 安全
深入探索Linux的journalctl命令:系统日志的利器
**journalctl 深入解析:Linux 系统日志的强大工具** journalctl 是 Linux 中用于查询和管理 systemd 日志的命令行工具,与 systemd-journald 配合收集广泛的信息,包括内核消息和服务日志。它提供实时追踪、过滤、导出等功能,如 `-f` 实时监控,`-u` 过滤特定服务日志,`-k` 显示内核消息,`--since` 和 `--until` 选择时间范围。在实际应用中,结合权限管理、日志空间控制和有效过滤,journalctl 成为系统管理员诊断和优化系统的得力助手。
|
存储 监控 安全
linux系统日志
【4月更文挑战第4天】Linux系统日志记录了各种事件,助于系统监控与问题诊断。日志文件位于`/var/log`,包括内核、用户和程序日志。重要日志文件有`messages`、`syslog`和`auth.log`。日志管理涉及收集、分析、备份等,常用工具为rsyslog和systemd-journal。管理员使用`cat`、`tail`等命令分析日志,通过日志轮转(如logrotate)管理存储空间。日志管理对于系统安全和性能至关重要。
210 1
linux系统日志
|
存储 监控 安全
systemd-cat:Linux 系统日志的管道
`systemd-cat` 是 Linux 的一个工具,它将标准输入/输出/错误重定向到 systemd 的 Journald,便于集中管理、实时监控和分析系统及应用日志。它以高效二进制格式存储,提供安全、灵活且易用的日志处理。通过 `--priority` 和 `--identifier` 参数可设置日志级别和来源。示例用法包括将命令输出(如 `ls` 或 `grep`)重定向到日志。注意权限、日志级别选择和来源标识的设定,结合 `journalctl` 可优化日志管理。
|
监控 网络协议 Linux
技术好文共享::Linux系统日志管理日志转储
技术好文共享::Linux系统日志管理日志转储
290 0
|
监控 Linux 应用服务中间件
Linux 系统中查看系统日志方法
Linux 系统中查看系统日志方法
522 0
|
存储 安全 关系型数据库
Linux日志管理rsyslog系统日志管理
本篇文章带大家了解日志管理中的esyslog系统日志管理,了解日志是从哪里来的,怎么观察日志,以及配置日志设备文件,学习怎么查看常见的一些种类的日志信息。本篇文章需要关心的问题:哪类程序--->产生的什么日志--->放到什么地方
580 0
|
存储 运维 监控
Linux运维:系统日志篇
Linux运维:系统日志篇
2627 0