Table of Contents
Configuring logrotate on Ubuntu Server 26.04 allows administrators to control how log files are rotated, compressed, retained, and deleted. This prevents large log files from filling disk space and affecting server stability.
On a Linux VPS server, custom logrotate rules are useful for websites, applications, scripts, and services that write logs outside the default system locations.
Step 1: Connect to the Ubuntu Server
Access the server using SSH:
ssh root@your_server_ip
Step 2: Confirm logrotate Is Installed
Check the installed version:
logrotate --version
If logrotate is not installed, install it first:
apt update
apt install logrotate -y
Step 3: Understand logrotate Configuration Paths
The global configuration file is:
/etc/logrotate.conf
Service-specific configuration files are usually stored in:
/etc/logrotate.d/
For custom applications, it is better to create a separate file inside /etc/logrotate.d/ instead of editing the main configuration file directly.
Step 4: Create a Sample Log File
Create a directory for a custom application log:
mkdir -p /var/log/myapp
Create a sample log file:
touch /var/log/myapp/app.log
Add test content:
echo "Test log entry" >> /var/log/myapp/app.log
Step 5: Create a Custom logrotate Rule
Create a new logrotate configuration file:
nano /etc/logrotate.d/myapp
Add the following configuration:
/var/log/myapp/app.log {
daily
rotate 7
compress
missingok
notifempty
create 0640 root adm
}
This rule rotates the log daily, keeps seven old log files, compresses rotated logs, and skips empty or missing log files.
Step 6: Save the Configuration
Save and exit Nano:
CTRL + X
Y
ENTER
Step 7: Test the Configuration in Debug Mode
Run logrotate in debug mode:
logrotate -d /etc/logrotate.d/myapp
This checks the rule and shows what logrotate would do without actually rotating the log file.
Step 8: Force log Rotation for Testing
Force logrotate to run the custom rule:
logrotate -f /etc/logrotate.d/myapp
Check the log directory:
ls -lah /var/log/myapp/
You should see the rotated log file and a new active log file.
Step 9: Configure Size-Based Rotation
For applications that generate logs quickly, rotate logs based on file size:
/var/log/myapp/app.log {
size 100M
rotate 5
compress
missingok
notifempty
create 0640 root adm
}
This rotates the log file when it reaches 100 MB and keeps five old compressed logs.
Step 10: Use postrotate for Services
Some services must be reloaded after log rotation so they continue writing to the correct log file.
Example configuration:
/var/log/myapp/app.log {
daily
rotate 7
compress
missingok
notifempty
create 0640 root adm
postrotate
systemctl reload myapp.service >/dev/null 2>&1 || true
endscript
}
Replace myapp.service with the real service name.
Step 11: Check logrotate Timer
Verify that logrotate runs automatically:
systemctl status logrotate.timer
Check the next scheduled run:
systemctl list-timers | grep logrotate
Step 12: Common logrotate Options
Useful options include:
dailyrotates logs every dayweeklyrotates logs every weekmonthlyrotates logs every monthrotate 7keeps seven old log filescompresscompresses rotated logsmissingokignores missing log filesnotifemptyskips empty log filescreatecreates a new log file after rotation
Configuring logrotate on Ubuntu Server 26.04 helps keep log files organized, reduces disk usage, and prevents logs from growing until they affect VPS performance or service stability.
