关于/tmp目录的清理规则

关于/tmp目录的清理规则

Sam

RHEL6

tmpwatch命令

tmpwatch 是专门用于解决“删除 xxx天没有被访问/修改过的文件”这样需求的命令。

安装:

[root@sam01 ~]# yum install tmpwatch.x86_64

使用:

man tmpwatch

tmpwatch  -  removes files which haven't been accessed for a period of time.

By  default, tmpwatch dates files by their atime (access time), not their mtime (modification time).

The  time  parameter  defines the threshold for removing files.
If the file has not been accessed for time, the file is removed.
The time  argument is a number with an optional single-character suffix specifying the units: m for minutes, h for hours, d for  days.
If no suffix is specified, time is in hours.

   -u, --atime
          Make the decision about deleting a file based on the  file's
          atime (access time). This is the default.

          Note  that  the periodic updatedb file system scans keep the
          atime of directories recent.

   -m, --mtime
          Make the decision about deleting a file based on the  file's
          mtime (modification time) instead of the atime.

   -c, --ctime
          Make  the decision about deleting a file based on the file's
          ctime (inode change time) instead of the atime; for directo‐
          ries, make the decision based on the mtime.

   -M, --dirmtime
          Make  the  decision  about deleting a directory based on the
          directory's mtime (modification time) instead of the  atime;
          completely ignore atime for directories.

举例: (清除/tmp目录下30天没有被访问文件)

[root@sam01 ~]# tmpwatch --atime 30d /tmp

RHEL7

systemd-tmpfiles-clean.service服务

服务: systemd-tmpfiles-clean.service

服务何时被执行呢?

Linux下该服务的执行可以根据systemd-tmpfiles-clean.timer进行管理

[root@sam01 ~]# cat /usr/lib/systemd/system/systemd-tmpfiles-clean.timer
#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.

[Unit]
Description=Daily Cleanup of Temporary Directories
Documentation=man:tmpfiles.d(5) man:systemd-tmpfiles(8)

[Timer]
OnBootSec=15min
OnUnitActiveSec=1d

# OnBootSec 表示相对于机器被启动的时间点
# 表示相对于匹配单元(本标签下Unit=指定的单元)最后一次被启动的时间点

上述配置文件表示两种情况会执行该服务

  1. 开机15分钟执行服务
  2. 距离上次执行该服务1天后执行服务

服务如何执行呢?

[root@sam01 ~]# cat /usr/lib/systemd/system/systemd-tmpfiles-clean.service
#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.

[Unit]
Description=Cleanup of Temporary Directories
Documentation=man:tmpfiles.d(5) man:systemd-tmpfiles(8)
DefaultDependencies=no
Conflicts=shutdown.target
After=systemd-readahead-collect.service systemd-readahead-replay.service local-fs.target time-sync.target
Before=shutdown.target

[Service]
Type=oneshot
ExecStart=/usr/bin/systemd-tmpfiles --clean
IOSchedulingClass=idle

# Type=oneshot 这一选项适用于只执行一项任务、随后立即退出的服务
# 命令文件 /usr/bin/systemd-tmpfiles
# 命令参数 --clean
# 通过定期执行 /usr/bin/systemd-tmpfiles --clean 完成清理

命令: /usr/bin/systemd-tmpfiles

[root@sam01 ~]# /usr/bin/systemd-tmpfiles --help
systemd-tmpfiles [OPTIONS...] [CONFIGURATION FILE...]

Creates, deletes and cleans up volatile and temporary files and directories.

  -h --help                 Show this help
     --version              Show package version
     --create               Create marked files/directories
     --clean                Clean up marked directories
     --remove               Remove marked files/directories
     --boot                 Execute actions only safe at boot
     --prefix=PATH          Only apply rules with the specified prefix
     --exclude-prefix=PATH  Ignore rules with the specified prefix
     --root=PATH            Operate on an alternate filesystem root

# --clean 将会清理被标记的文件目录

哪些目录被标记,又是什么样的标记呢?

定义在配置文件/usr/lib/tmpfiles.d/tmp.conf中

配置文件: /usr/lib/tmpfiles.d/tmp.conf

[root@sam01 ~]# cat /usr/lib/tmpfiles.d/tmp.conf
#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.

# See tmpfiles.d(5) for details

# Clear tmp directories separately, to make them easier to override
v /tmp 1777 root root 10d
v /var/tmp 1777 root root 30d

# Exclude namespace mountpoints created with PrivateTmp=yes
x /tmp/systemd-private-%b-*
X /tmp/systemd-private-%b-*/tmp
x /var/tmp/systemd-private-%b-*
X /var/tmp/systemd-private-%b-*/tmp

x

在根据"寿命"字段清理过期文件时, 忽略指定的路径及该路径下的所有内容。 可以在"路径"字段中使用shell风格的通配符。 注意,这个保护措施对 r 与 R 无效。

X

在根据"寿命"字段清理过期文件时, 仅忽略指定的路径自身而不包括该路径下的其他内容。 可以在"路径"字段中使用shell风格的通配符。 注意,这个保护措施对 r 与 R 无效。

上述配置表示:

  1. 清理/tmp目录超过10天的内容,但是匹配/tmp/systemd-private-%b-*的目录及其路径下的全部内容会被保留

  2. 清理/var/tmp目录超过30天的内容,但是匹配/var/tmp/systemd-private-%b-*的目录及其路径下的全部内容被保留

总结

  1. RHEL6 根据文件的访问时间等条件使用tmpwatch命令进行/tmp目录的清理,可以使用crond daemon进行定期执行

  2. RHEL7 根据服务systemd-tmpfiles-clean.service 进行临时文件的清理,清理规则定义在配置文件/usr/lib/tmpfiles.d/tmp.conf,调用命令为/usr/bin/systemd-tmpfiles --clean,执行时间依靠systemd-tmpfiles-clean.timer进行管理

<think>好的,用户之前问过如何在Linux中防止/tmp下的文件被自动清理,现在他问的是如何在CentOS中配置/tmp清理策略。CentOS属于RHEL系,所以应该用systemd-tmpfiles来处理。我需要先回顾之前的回答,确保这次的内容不重复,但更具体针对CentOS。 首先,用户可能需要知道CentOS默认使用systemd,所以清理策略由systemd-tmpfiles管理。我需要详细说明如何查看默认配置,如何覆盖或修改这些配置,比如创建自定义的配置文件在/etc/tmpfiles.d目录下。要强调x标记的作用,即排除清理。同时,可能需要提到如何设置保留时间和调整参数,比如修改文件的保存天数。 另外,用户可能需要了解如何测试配置是否生效,比如手动触发清理并检查文件是否存在。还有注意事项,比如不要直接修改系统默认配置文件,而是通过新建.conf文件来覆盖,这样可以避免升级时被覆盖。可能还需要提到其他方法,比如使用touch命令或chattr,但主推systemd的方法,因为这是CentOS推荐的方式。 还需要考虑用户可能的误区,比如直接修改/usr/lib下的文件,这不好,应该用/etc下的自定义配置。另外,检查用户是否有权限问题,是否需要sudo,以及重启服务是否必要。可能还要补充如何验证服务状态,确保配置生效。最后,最佳实践部分可以建议优先使用/var/tmp或者自定义目录,避免依赖/tmp的长期存储。</think>在 CentOS 系统中,`/tmp` 目录清理策略由 `systemd-tmpfiles` 服务管理。以下是完整的配置方法: --- ### 一、查看默认清理规则 CentOS 默认配置文件路径: ```bash cat /usr/lib/tmpfiles.d/tmp.conf ``` 输出示例: ```conf # 默认规则:删除超过 10 天的未访问文件 D /tmp 1777 root root 10d ``` --- ### 二、自定义清理策略(推荐) #### 1️⃣ 创建覆盖规则 新建配置文件(优先级高于默认配置): ```bash sudo vim /etc/tmpfiles.d/my_tmp.conf ``` #### 2️⃣ 配置语法说明 | 指令 | 格式 | 作用示例 | |------|-------------------------------|-----------------------------| | `D` | `D <路径> <权限> <用户> <组> <保留时间>` | 设置目录清理规则 | | `x` | `x <路径>` | 完全跳过指定路径的清理 | #### 3️⃣ 配置示例 ```conf # 保留 /tmp 目录中所有文件至少 30 天(覆盖默认 10 天) D /tmp 1777 root root 30d # 排除特定文件不被清理 x /tmp/keep_this_file x /tmp/myapp/* ``` --- ### 三、生效与测试 1. **手动触发清理**(测试用) ```bash sudo systemd-tmpfiles --clean ``` 2. **查看清理效果** ```bash ls -l /tmp # 验证目标文件是否保留 ``` 3. **重启服务**(非必须,系统每天自动执行) ```bash sudo systemctl restart systemd-tmpfiles-clean.timer ``` --- ### 四、高级配置选项 #### 1. 按文件类型设置保留时间 ```conf # 保留所有 .sock 文件 60 天 D /tmp/*.sock 1777 root root 60d ``` #### 2. 混合策略 ```conf # 主目录保留 15 天 D /tmp 1777 root root 15d # 子目录保留更久 D /tmp/longterm 1777 root root 90d ``` --- ### 五、注意事项 1. **时间单位对照表** | 单位 | 含义 | 示例 | |------|------------|---------| | `s` | 秒 | 3600s | | `h` | 小时 | 24h | | `d` | 天 | 30d | 2. **日志监控** 清理记录可通过以下命令查看: ```bash journalctl -u systemd-tmpfiles-clean ``` --- ### 六、替代方案 | 方法 | 适用场景 | 操作示例 | |---------------------|-----------------------------|-----------------------------| | **定时任务 + 脚本** | 需要复杂清理逻辑时 | `0 3 * * * find /tmp -atime +30 -delete` | | **修改挂载参数** | 使用 `tmpfs` 内存文件系统时 | `/etc/fstab` 中添加 `tmpfs /tmp tmpfs defaults,noatime,size=1G 0 0` | --- ### 最佳实践建议 1. **优先使用 `/var/tmp`** CentOS 默认保留 `/var/tmp` 中的文件直到系统重启 2. **避免锁死所有文件** 使用 `x /tmp/*` 会导致磁盘空间耗尽风险 3. **容器环境特殊处理** 如果使用 Docker,可通过 `--tmpfs` 参数控制容器内 `/tmp` 生命周期 通过以上配置,您可以精确控制 CentOS 系统中 `/tmp` 目录清理行为。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值