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/

<think>嗯,用户现在遇到了Linux系统打印文件时报错,显示“不安全的权限许可”。我需要先理解这个问题可能的原因,然后给出解决方案。 首先,用户之前询问过关于“stopping job because the scheduler”的错误,现在的问题是关于权限的。这两个问题可能有关联,但这次的重点是权限不安全。Linux系统中打印服务(如CUPS)对权限非常敏感,所以可能涉及文件或目录的权限设置不正确。 接下来,我应该考虑用户可能遇到的具体情况。当打印任务因为权限问题被拒绝时,常见的原因可能包括:打印队列或临时目录的权限不正确,过滤器或驱动程序没有执行权限,或者用户没有足够的权限提交打印任务。 我需要分步骤来引导用户解决问题。首先,检查CUPS临时目录的权限,通常是/var/spool/cups/tmp,权限应为700,所有者是root。如果权限不对,需要调整。然后,检查过滤器目录/usr/lib/cups/filter下的文件是否有执行权限,确保所有文件都是可执行的,并且属于root用户。 另外,用户可能使用了错误的命令导致临时文件权限过松,比如用sudo创建文件但权限太开放,这时候需要修改umask设置,确保生成的文件权限正确。还有可能是SELinux或AppArmor等安全模块阻止了CUPS的正常操作,需要检查这些安全策略的日志,暂时禁用或调整策略来测试。 此外,用户权限不足也可能导致问题,需要确认用户是否在lpadmin组中,如果没有,添加用户到该组并重启服务。最后,如果以上步骤都无效,可能需要重新配置或重新安装CUPS来修复潜在的配置错误。 需要提醒用户查看CUPS的错误日志/var/log/cups/error_log,根据具体错误信息进一步排查。同时,操作时要注意备份配置文件,避免不必要的损失。总结起来,应该按照检查目录权限、过滤器权限、安全模块、用户组权限、临时文件权限的顺序逐步排查,并提供相应的命令和解决方案。</think>在Linux系统中,打印任务因 **"不安全的权限许可"(Insecure Permissions)** 报错时,通常是因为CUPS(通用Unix打印系统)检测到与打印任务相关的文件、目录或配置权限不符合安全规范。以下是分步骤解决方案: --- ### **1. 问题根源** CUPS对权限敏感,以下场景会触发此错误: - 打印队列临时文件(如PDF、PostScript)权限过于开放(例如 `777`)。 - CUPS相关目录(如 `/var/spool/cups`)权限不正确。 - 用户或进程以不安全权限创建了打印任务文件。 --- ### **2. 解决方案** #### **2.1 修复CUPS临时目录权限** CUPS默认临时目录为 `/var/spool/cups/tmp`,其权限必须严格限制: ```bash # 检查目录权限(应为 drwx------,所有者 root) ls -ld /var/spool/cups/tmp # 修复权限(若权限异常) sudo chmod 700 /var/spool/cups/tmp sudo chown root:root /var/spool/cups/tmp ``` #### **2.2 检查打印过滤器权限** CUPS过滤器(位于 `/usr/lib/cups/filter/`)必须为 `root` 所有且可执行: ```bash # 检查过滤器权限(应为 -rwxr-xr-x) ls -l /usr/lib/cups/filter/ # 修复权限 sudo chmod 755 /usr/lib/cups/filter/* sudo chown root:root /usr/lib/cups/filter/* ``` #### **2.3 修复打印文件本身的权限** 如果用户手动指定了不安全权限的文件(例如通过 `sudo` 生成的文件): ```bash # 修改文件权限为 600(仅所有者可读写) chmod 600 你的打印文件.pdf # 或通过 umask 确保新建文件默认安全 umask 077 # 临时生效,或写入 ~/.bashrc ``` #### **2.4 检查SELinux/AppArmor限制** 若系统启用了强制访问控制(如SELinux),可能拦截CUPS操作: ```bash # 查看SELinux日志 sudo grep cups /var/log/audit/audit.log | audit2why # 临时禁用SELinux测试(生产环境慎用) sudo setenforce 0 ``` 若问题消失,需为CUPS添加SELinux策略或调整AppArmor配置。 #### **2.5 重置CUPS配置** 若权限问题由错误配置引起: ```bash # 备份原配置 sudo cp /etc/cups/cupsd.conf /etc/cups/cupsd.conf.bak # 恢复默认配置 sudo cp /usr/share/cups/cupsd.conf.default /etc/cups/cupsd.conf # 重启CUPS sudo systemctl restart cups ``` --- ### **3. 扩展场景:用户权限不足** 若普通用户因权限问题无法打印: ```bash # 确认用户是否在 lpadmin 组 groups 你的用户名 # 添加用户到打印管理组 sudo usermod -aG lpadmin 你的用户名 # 重启CUPS服务 sudo systemctl restart cups ``` --- ### **4. 日志排查** 通过CUPS日志定位具体问题: ```bash # 实时查看错误日志 tail -f /var/log/cups/error_log # 典型错误示例: # "Permissions on /var/spool/cups/tmp/xxx are too permissive (0100777)" ``` --- ### **总结** | **步骤** | **关键命令/操作** | **目标** | |-------------------------|--------------------------------------------|----------------------------------| | 检查临时目录权限 | `chmod 700 /var/spool/cups/tmp` | 确保临时文件不被非授权访问 | | 修复过滤器权限 | `chmod 755 /usr/lib/cups/filter/*` | 允许CUPS正确执行过滤操作 | | 检查打印文件权限 | `chmod 600 文件名` | 避免CUPS拒绝不安全文件 | | 禁用SELinux测试 | `setenforce 0` | 排除强制访问控制干扰 | | 重置CUPS配置 | `cp /usr/share/cups/cupsd.conf.default ...`| 恢复无冲突的默认配置 | 操作后若问题仍存在,建议提供 `/var/log/cups/error_log` 中的具体报错内容进一步分析。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值