Linux日志文件/ var / log带有示例的教程

Linux provides a lot of different types of logs by default. These files are generally located at /var/log . There may be some exceptions like third party applications but the configuration of log location can be changed to the /var/log directory. In this post, we will look at default log files and how to list, tail, search, filter these logs.

Linux默认提供许多不同类型的日志。 这些文件通常位于/ var / log 。 可能有一些例外,例如第三方应用程序,但是日志位置的配置可以更改为/ var / log目录。 在本文中,我们将研究默认日志文件以及如何列出,尾部,搜索,过滤这些日志。

列出日志文件 (List Log Files)

Logs files can be simply listed by using ls command but keep in mind there are directories they contain different files for logs.

可以使用ls命令简单地列出日志文件,但是请记住,有一些目录包含用于日志的不同文件。

$ ls /var/log/
List Log Files
List Log Files
列出日志文件

We can list in a recursive manner to get files and folder under /var/log directory like below.

我们可以递归方式列出,以在/ var / log目录下获取文件和文件夹,如下所示。

$ ls -R /var/log/
List Log Files
List Log Files
列出日志文件

读取日志文件(Reading Log Files)

There are different methods to read log file but we will use less which have practical solutions while reading the log file.

有多种读取日志文件的方法,但是在读取日志文件时,我们将使用较少的具有实际解决方案的方法。

$ less auth.log
Reading Log Files
Reading Log Files
读取日志文件

Space will skip to the next page also page up / page down will work too.

空格将跳至下一页,也可以向上翻页/向下翻页。

搜索日志文件 (Searching Log File)

Less have the functionality to search a text file were in this situation a log file. After opening log files with less use /auth to search “auth” term down to the file pages.

在这种情况下,很少有具有搜索文本文件的功能的日志文件。 在使用较少的日志文件打开日志后,使用/ auth搜索“ auth ”一词到文件页面。

/auth
Searching Log File
Searching Log File
搜索日志文件

To continue to search term without entering, again and again, press for the next match or for the previous search. After arriving at the file end if no match exists we will get a message like below at the end of the terminal.

要继续搜索词条而无需一次又一次输入,请按n进行下一个匹配,按p进行上一次搜索。 到达文件末尾后,如果不存在匹配项,我们将在终端末尾收到如下消息。

Searching Log File
Searching Log File
搜索日志文件

过滤日志文件(Filtering Log File)

Searching is a way to see occurrences in a log file and previous and next events. An alternative is filtering log files. Grep is a very capable tool to filter log files. We will filter for “auth” for all files named auth.log* . We named files auth.log* because old auth.log files are gzipped and have gz extension.

搜索是查看日志文件中的事件以及上一个和下一个事件的一种方式。 一种替代方法是过滤日志文件。 Grep是一个非常强大的工具,可以过滤日志文件。 我们将过滤所有名为auth.log *的文件的“ auth” 。 我们将文件命名为auth.log *,因为旧的auth.log文件已压缩并具有gz扩展名。

$ zgrep "authen" auth.log*
Filtering Log File
Filtering Log File
过滤日志文件

If we want to colorize findings we can use normal grep with the same filter term as below.

如果我们想给发现着色,我们可以使用具有以下相同过滤条件的普通grep。

$ zgrep "authen" auth.log* | grep "auth"
Filtering Log File
Filtering Log File
过滤日志文件

筛选所有日志文件(Filter All Log Files)

Actually filtering or search all files are not different but as an example, we can look at it by specifying and IP address.

实际上,过滤或搜索所有文件没有什么不同,但是作为示例,我们可以通过指定IP地址来进行查看。

$ zgrep  "192.168.122.1" * | less
Filter All Log Files
Filter All Log Files
筛选所有日志文件

We can use less for search other terms like username “ismail”

我们可以减少搜索其他术语,例如用户名“ ismail”

/ismail
Filter All Log Files
Filter All Log Files
筛选所有日志文件
LEARN MORE  How To Manage Windows Firewall From Command Line With Netsh Command?
了解更多信息如何使用Netsh命令从命令行管理Windows防火墙?

翻译自: https://www.poftut.com/linux-log-files-varlog/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值