{"id":4289,"date":"2026-09-12T15:40:44","date_gmt":"2026-09-12T10:10:44","guid":{"rendered":"https:\/\/cpanelfree.com\/blog\/how-to-configure-logrotate-systemd-journalctl-linux\/"},"modified":"2026-09-12T15:40:44","modified_gmt":"2026-09-12T10:10:44","slug":"how-to-configure-logrotate-systemd-journalctl-linux","status":"publish","type":"post","link":"https:\/\/cpanelfree.com\/blog\/how-to-configure-logrotate-systemd-journalctl-linux\/","title":{"rendered":"How to Configure Logrotate and Manage systemd Journalctl to Prevent Full Disks"},"content":{"rendered":"<div style=\"background-color: #0f172a;border-left: 4px solid #10b981;padding: 18px 22px;margin-bottom: 25px;border-radius: 6px\">\n  <strong style=\"color: #10b981;font-size: 16px\">Quick Technical Answer:<\/strong><\/p>\n<p style=\"color: #cbd5e1;margin: 8px 0 0 0;font-size: 15px;line-height: 1.6\">\n    To immediately reclaim disk space from bloated logs on Linux, vacuum the systemd journal using <code>sudo journalctl --vacuum-size=200M<\/code>. Then, cap future growth permanently by setting <code>SystemMaxUse=250M<\/code> in <code>\/etc\/systemd\/journald.conf<\/code> and restarting with <code>sudo systemctl restart systemd-journald<\/code>. For application logs in <code>\/var\/log\/<\/code>, create a rule in <code>\/etc\/logrotate.d\/myapp<\/code> specifying <code>daily<\/code>, <code>rotate 7<\/code>, <code>compress<\/code>, <code>delaycompress<\/code>, and <code>copytruncate<\/code>.\n  <\/p>\n<\/div>\n<h2>The Silent Server Killer: How Unchecked Logs Crash Production Applications<\/h2>\n<p>One of the most frequent causes of sudden web server downtime, MySQL database corruption, and crashed Nginx instances is running out of disk space (<code>No space left on device<\/code>). On a budget cloud VPS with 20GB to 50GB of NVMe disk storage, chatty web servers, security scanners, and container outputs can easily generate gigabytes of log entries every single week.<\/p>\n<p>When the root partition reaches 100% capacity, MySQL cannot allocate temporary tables, PHP cannot write session files, and SSH login attempts may fail because PAM cannot create session tokens. Even worse, if millions of tiny log files are created, the filesystem can exhaust available <strong>inodes<\/strong> even when physical gigabytes appear free.<\/p>\n<p>By pairing <strong>logrotate<\/strong> (for file-based application logs) with <strong>systemd-journald retention caps<\/strong> (for binary system logs), you ensure a self-cleaning server that maintains predictable, fixed disk usage indefinitely.<\/p>\n<h2>Step 1: Emergency Diagnosis: Finding What Is Consuming Your Disk<\/h2>\n<p>When investigating high disk consumption, inspect both physical block storage and filesystem inode allocation:<\/p>\n<pre><code style=\"color: #38bdf8\"># Check physical disk space usage\ndf -h \/\n\n# Check filesystem inode consumption\ndf -i \/\n\n# Identify the largest log files in \/var\/log\nsudo du -ah \/var\/log | sort -rh | head -n 15<\/code><\/pre>\n<h2>Step 2: Reclaiming Space from systemd journalctl<\/h2>\n<p>Modern Linux stores system daemon logs in binary format under <code>\/var\/log\/journal\/<\/code>. On high-traffic systems, this directory frequently balloons to 4GB or more.<\/p>\n<pre><code style=\"color: #38bdf8\"># Check current disk space consumed by systemd journal\njournalctl --disk-usage\n\n# Immediately prune logs older than 7 days\nsudo journalctl --vacuum-time=7d\n\n# Or prune logs down to a strict size limit (e.g. 200 Megabytes)\nsudo journalctl --vacuum-size=200M<\/code><\/pre>\n<p>To enforce this size cap permanently so logs never exceed your threshold again, edit the journal configuration:<\/p>\n<pre><code style=\"color: #38bdf8\">sudo nano \/etc\/systemd\/journald.conf<\/code><\/pre>\n<p>Uncomment and configure the following directives:<\/p>\n<pre><code style=\"color: #38bdf8\">[Journal]\nStorage=persistent\nSystemMaxUse=250M\nSystemKeepFree=1G\nMaxRetentionSec=1month<\/code><\/pre>\n<p>Apply the changes by restarting the journal daemon:<\/p>\n<pre><code style=\"color: #38bdf8\">sudo systemctl restart systemd-journald<\/code><\/pre>\n<h2>Step 3: Configuring Custom logrotate Rules for Applications<\/h2>\n<p>While systemd manages binary unit logs, web services like Nginx, Apache, Redis, and custom Python\/Node applications write plain text files directly into <code>\/var\/log\/<\/code>. <strong>logrotate<\/strong> runs automatically via a daily cron\/timer job to compress and rotate these files.<\/p>\n<p>Let us create an optimized rotation rule for a custom web application:<\/p>\n<pre><code style=\"color: #38bdf8\">sudo nano \/etc\/logrotate.d\/mywebapp<\/code><\/pre>\n<p>Insert the following production configuration:<\/p>\n<pre><code style=\"color: #38bdf8\">\/var\/log\/mywebapp\/*.log {\n    daily\n    missingok\n    rotate 14\n    compress\n    delaycompress\n    notifempty\n    create 0640 www-data adm\n    sharedscripts\n    copytruncate\n}<\/code><\/pre>\n<h2>Step 4: Understanding Critical logrotate Directives<\/h2>\n<table style=\"width: 100%;border-collapse: collapse;margin: 25px 0;font-size: 14px;text-align: left\">\n<thead>\n<tr style=\"background-color: #0f172a;color: #10b981\">\n<th style=\"padding: 12px;border: 1px solid #334155\">Directive<\/th>\n<th style=\"padding: 12px;border: 1px solid #334155\">Operational Purpose &amp; Effect<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr style=\"background-color: #1e293b;color: #f8fafc\">\n<td style=\"padding: 10px;border: 1px solid #334155\"><code>daily \/ weekly<\/code><\/td>\n<td style=\"padding: 10px;border: 1px solid #334155\">Rotates the log file once every calendar day or week.<\/td>\n<\/tr>\n<tr style=\"background-color: #0f172a;color: #cbd5e1\">\n<td style=\"padding: 10px;border: 1px solid #334155\"><code>rotate 14<\/code><\/td>\n<td style=\"padding: 10px;border: 1px solid #334155\">Keeps 14 historical log files before deleting the oldest archive.<\/td>\n<\/tr>\n<tr style=\"background-color: #1e293b;color: #f8fafc\">\n<td style=\"padding: 10px;border: 1px solid #334155\"><code>compress &amp; delaycompress<\/code><\/td>\n<td style=\"padding: 10px;border: 1px solid #334155\">Gzips rotated logs to save ~90% disk space; delays compression on the newest rotated file so active daemons can finish writing without errors.<\/td>\n<\/tr>\n<tr style=\"background-color: #0f172a;color: #cbd5e1\">\n<td style=\"padding: 10px;border: 1px solid #334155\"><code>copytruncate<\/code><\/td>\n<td style=\"padding: 10px;border: 1px solid #334155\">Copies the active log and truncates it in place without closing the file handle. Essential for Node\/Python apps that don&#8217;t support log reopen signals.<\/td>\n<\/tr>\n<tr style=\"background-color: #1e293b;color: #f8fafc\">\n<td style=\"padding: 10px;border: 1px solid #334155\"><code>missingok &amp; notifempty<\/code><\/td>\n<td style=\"padding: 10px;border: 1px solid #334155\">Suppresses error alerts if the log file is missing, and skips rotation if the file is 0 bytes.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>Step 5: Testing &amp; Forcing logrotate Execution<\/h2>\n<p>Do not wait until midnight to discover a syntax error in your logrotate configuration. Test it safely in debug mode:<\/p>\n<pre><code style=\"color: #38bdf8\"># Perform dry-run dry debug test (no files changed)\nsudo logrotate -d \/etc\/logrotate.d\/mywebapp\n\n# Force immediate execution of all logrotate rules\nsudo logrotate -f \/etc\/logrotate.conf\n\n# Verify compressed .gz files were successfully created\nls -lh \/var\/log\/mywebapp\/<\/code><\/pre>\n<h2>Frequently Asked Questions (FAQ)<\/h2>\n<div style=\"margin: 20px 0\">\n<h3 style=\"color: #10b981;margin-bottom: 5px\">Why did my disk space not decrease after deleting a huge log file with rm?<\/h3>\n<p style=\"color: #cbd5e1;font-size: 15px\">In Linux, if an active process still holds an open file descriptor to a deleted file, the operating system retains the disk blocks until the process terminates. Run <code>sudo lsof | grep deleted<\/code> to find the offending process, and restart it (e.g. <code>sudo systemctl restart nginx<\/code>) to release the freed disk space.<\/p>\n<h3 style=\"color: #10b981;margin-bottom: 5px\">How can I truncate a massive log file without deleting it or restarting the service?<\/h3>\n<p style=\"color: #cbd5e1;font-size: 15px\">Use shell truncation: <code>sudo truncate -s 0 \/var\/log\/bloated.log<\/code>. This instantly zeroes out the file content while preserving the active file descriptor, preventing application crashes.<\/p>\n<\/div>\n<div style=\"background-color: #0f172a;border-left: 4px solid #10b981;padding: 18px 24px;margin: 30px 0;border-radius: 8px\">\n<h3 style=\"color: #10b981;margin-top: 0\">\ud83d\udd17 Recommended Related Technical Guides<\/h3>\n<ul style=\"margin-bottom: 0;color: #cbd5e1\">\n<li><a href=\"https:\/\/cpanelfree.com\/blog\/how-to-add-swap-memory-ubuntu-vps\/\" style=\"color: #38bdf8;text-decoration: underline\">How to Add Swap Memory on Ubuntu VPS to Prevent OOM Crashes<\/a><\/li>\n<li><a href=\"https:\/\/cpanelfree.com\/blog\/how-to-benchmark-vps-speed-performance\/\" style=\"color: #38bdf8;text-decoration: underline\">Benchmarking Linux VPS Disk I\/O &amp; Network Latency<\/a><\/li>\n<li><a href=\"https:\/\/cpanelfree.com\/blog\/how-to-scan-linux-server-malware-backdoors\/\" style=\"color: #38bdf8;text-decoration: underline\">Scanning Linux Servers for Malware, Web Shells &amp; Injected Files<\/a><\/li>\n<\/ul>\n<\/div>\n<div style=\"background: linear-gradient(135deg, #0284c7 0%, #0369a1 100%);color: #ffffff;padding: 28px;border-radius: 12px;margin: 35px 0;text-align: center\">\n<h3 style=\"color: #ffffff;margin-top: 0;font-size: 22px\">Upgrade to High-Capacity NVMe Storage on CpanelFree<\/h3>\n<p style=\"color: #e0f2fe;font-size: 15px;max-width: 650px;margin: 0 auto 18px auto\">Never worry about disk exhaustion again. Scale your storage seamlessly with pure Enterprise NVMe disk arrays on CpanelFree Cloud VPS.<\/p>\n<p>  <a href=\"https:\/\/cpanelfree.com\/\" style=\"background-color: #ffffff;color: #0284c7;font-weight: 700;padding: 12px 28px;border-radius: 8px;text-decoration: none;display: inline-block\">View High-Storage VPS Plans &rarr;<\/a>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Quick Technical Answer: To immediately reclaim disk space from bloated logs on Linux, vacuum the systemd journal using sudo journalctl &#8211;vacuum-size=200M. Then, cap future growth permanently by setting SystemMaxUse=250M in \/etc\/systemd\/journald.conf and restarting with sudo systemctl restart systemd-journald. For application logs in \/var\/log\/, create a rule in \/etc\/logrotate.d\/myapp specifying daily, rotate 7, compress, delaycompress, and &#8230; <a title=\"How to Configure Logrotate and Manage systemd Journalctl to Prevent Full Disks\" class=\"read-more\" href=\"https:\/\/cpanelfree.com\/blog\/how-to-configure-logrotate-systemd-journalctl-linux\/\" aria-label=\"Read more about How to Configure Logrotate and Manage systemd Journalctl to Prevent Full Disks\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":4288,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[88,64,51],"tags":[],"class_list":["post-4289","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cloud-vps","category-security","category-tutorials"],"_links":{"self":[{"href":"https:\/\/cpanelfree.com\/blog\/wp-json\/wp\/v2\/posts\/4289","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cpanelfree.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cpanelfree.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cpanelfree.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cpanelfree.com\/blog\/wp-json\/wp\/v2\/comments?post=4289"}],"version-history":[{"count":0,"href":"https:\/\/cpanelfree.com\/blog\/wp-json\/wp\/v2\/posts\/4289\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/cpanelfree.com\/blog\/wp-json\/wp\/v2\/media\/4288"}],"wp:attachment":[{"href":"https:\/\/cpanelfree.com\/blog\/wp-json\/wp\/v2\/media?parent=4289"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cpanelfree.com\/blog\/wp-json\/wp\/v2\/categories?post=4289"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cpanelfree.com\/blog\/wp-json\/wp\/v2\/tags?post=4289"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}