1.手动清除日志
cat /dev/null > /var/log/boot.log
cat /dev/null > /var/log/btmp
cat /dev/null > /var/log/cron
cat /dev/null > /var/log/dmesg
cat /dev/null > /var/log/firewalld
cat /dev/null > /var/log/grubby
cat /dev/null > /var/log/lastlog
cat /dev/null > /var/log/mail.info
cat /dev/null > /var/log/maillog
cat /dev/null > /var/log/messages
cat /dev/null > /var/log/secure
cat /dev/null > /var/log/spooler
cat /dev/null > /var/log/syslog
cat /dev/null > /var/log/tallylog
cat /dev/null > /var/log/wpa_supplicant.log
cat /dev/null > /var/log/wtmp
cat /dev/null > /var/log/yum.log
2.利用 crontab 定时清理日志文件
1.创建脚本文件
vim cleanLog.sh
2.编辑内容
按需添加或修改如下内容:
注解:sed 命令,清除前 50 行内容
echo 命令清空日志内容
rm 命令删除后缀以日期命名的日志文件
注意:以上日志清理并没有导致系统或者其他服务出错
#!/bin/bash
echo "开始清除日志文件"
sed -i '1,50d' /var/log/cron #清除定时任务记录
sed -i '1,50d' /var/log/xferlog #清除ftp记录
sed -i '1,50' /var/log/mariadb/mariadb.log #清除数据库日志
sed -i '1,50d' /var/log/secure #清除sshd信息
sed -i '1,50d' /var/log/firewalld
sed -i '1,50d' /var/log/httpd/access_log
sed -i '1,50d' /var/log/httpd/ssl_access_log
sed -i '1,50d' /var/log/httpd/ssl_error_log
sed -i '1,50d' /var/log/httpd/ssl_request_log
sed -i '1,50d' /var/log/messages #清除系统开机发生的错误
sed -i '1,50d' /var/log/tuned/tuned.log
sed -i '1,50d' /var/log/hawkey.log
sed -i '1,50d' /var/log/yum.log
sed -i '1,50d' /var/log/dnf.log
sed -i '1,50d' /var/log/dnf.rpm.log
sed -i '1,50d' /var/log/ntp.log
sed -i '1,50d' /var/log/audit/audit.log
history -c #清除历史执行命令
echo > /var/log/wtmp #清除系统登录成功的记录
echo > /var/log/btmp #清除系统登录失败的记录
echo > /var/log/lastlog
rm -fr /var/log/boot.log-* /var/log/btmp-* /var/log/cron-* /var/log/maillog-* /var/log/messages-* /var/log/secure-* /var/log/spooler-* /var/log/xferlog-* /var/log/messages-* /var/log/multi-nic-util/*
rm -fr /var/log/httpd/access_log-* /var/log/httpd/error_log-* /var/log/httpd/ssl_access_log-* /var/log/httpd/ssl_error_log-* /var/log/httpd/ssl_request_log-*
3.设置定时任务
crontab -e
设置每 30 天清理一次(日志清理太频繁不方便以后按日志排错)
0 0 */30 * * ./cleanLog.sh