DEVOPS, SRE AND CLOUD · PART 1 · LINUX, FROM THE INSIDE · SERVICES · 26 OF 187

Linux Time Sync, Log Rotation and sysctl Tuning

Three housekeeping jobs that prevent real outages: keeping clocks right (UTC on servers, NTP with chrony, why TLS, logs and distributed systems break when clocks drift), rotating logs so disks never fill (logrotate turned a 588,895-byte log into a 215,157-byte gzip), and reading and tuning kernel settings with sysctl (808 on our machine, such as somaxconn 4096).

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
1 / 12

Press play to hear the narration, or step through at your own pace.

Key takeaways

  • UTC + chrony
  • drift breaks TLS, tokens, logs
  • logrotate: rotate, compress, copytruncate or reopen
  • sysctl.d with evidence

Notes

Time on servers

First, time. Servers should run in UTC. Ours did: zero thirty UTC, which was six in the morning India time. Logs from machines in different regions then line up without conversion, and there are no daylight saving jumps. Convert to local time only when you show it to people. Remember episode seventeen, where a two thirty schedule fired at nine in the evening UTC: time zones cause real bugs.

Our machine
$ date -u
Wed Dec  2 00:30:01 UTC 2026
$ TZ=Asia/Kolkata date
Wed Dec  2 06:00:01 IST 2026
/etc/timezone: Etc/UTC

servers: UTC; show local
  time only to humans

NTP

Clocks drift, so servers sync them with NTP, the network time protocol. On modern Ubuntu and Red Hat, chrony does it. chronyc tracking shows how far off the clock is, and timedatectl shows whether it is synchronised. In the cloud, use the provider's own time service, which on AWS is a link-local address every instance can reach. Our sandbox does not run a time daemon, so try these in your lab VM.

chrony
$ chronyc tracking
  how far off, and drifting how
$ chronyc sources -v
  which servers, which is used
$ timedatectl
  look for: synchronized: yes
cloud: use the provider's
  time source (AWS 169.254.
  169.123)

Why it matters

Why does a few minutes matter? TLS checks certificate dates, so a wrong clock makes valid certificates look expired. Signed requests and tokens include times, and AWS rejects request signatures that are more than a few minutes off. Logs from different machines appear in the wrong order during an incident. Distributed systems that use leases and timeouts misbehave. And scheduled jobs fire at the wrong moment.

Clock drift breaks
  • TLS: certificate "not yet valid" or "expired"
  • tokens: JWT, AWS signatures rejected (AWS allows ~5 min)
  • logs: events out of order
  • distributed systems: leases, locks, ordering
  • cron and timers fire at the wrong time

Log rotation

Next, logs. Without rotation they grow until the disk fills, as in episode eleven. logrotate renames the current log, compresses old copies, keeps a fixed number and deletes the rest. Our config: rotate daily, keep seven, compress, ignore a missing log, skip an empty one, and copy-truncate. We forced a rotation: a five hundred and eighty nine thousand byte log became an empty app dot log and a compressed copy of two hundred and fifteen thousand bytes, about a third of the size.

Our logrotate test
/tmp/lr/app.log {
  daily
  rotate 7
  compress
  missingok
  notifempty
  copytruncate
}
before: app.log 588,895 bytes
after:  app.log 0, app.log.1.gz
        215,157 bytes

copytruncate vs reopen

There is a trap here, from episode four. If logrotate just renames a log, the app keeps writing to its open file, now renamed, and space is never freed. Two fixes. Copytruncate copies the log and then empties the original, so the app keeps writing to the same file; a few lines can be lost in the gap. Or rename and then signal the app to reopen its logs, in a postrotate script: nginx uses USR one, many daemons use SIGHUP.

Two ways to rotate
  • copytruncate: copy, then empty the original; the app keeps writing; tiny gap may lose a few lines
  • create + postrotate: rename, then tell the app to reopen (nginx: kill -USR1; many daemons: SIGHUP)
  • rename only → the app writes to the old, renamed file

journald and apps

The journal needs limits too: set a maximum size and retention in journald's config, or vacuum it by hand. In containers the rule changes: apps log to standard output, and the container runtime captures and rotates it, but only if you set a maximum size, which Docker does not by default. Either way, the long-term copy belongs in a central log system, which part eleven builds.

Other logs
/etc/systemd/journald.conf
  SystemMaxUse=1G
  MaxRetentionSec=14day
$ journalctl --vacuum-size=500M
containers: log to stdout;
  the runtime rotates
  (docker json-file max-size)
ship logs off the box (part 11)

sysctl

Finally, sysctl, which reads and writes the kernel settings under slash proc slash sys. Our machine had eight hundred and eight of them. somaxconn, four thousand and ninety six, caps the queue of connections waiting to be accepted. swappiness was sixty. The whole system could open about eight hundred and nineteen thousand files. And outgoing connections use local ports from thirty two thousand seven hundred and sixty eight to sixty thousand nine hundred and ninety nine, about twenty eight thousand ports.

Our machine
$ sysctl -a | wc -l
808
$ sysctl net.core.somaxconn
  4096   listen queue limit
vm.swappiness = 60
fs.file-max = 818942
net.ipv4.ip_local_port_range
  = 32768 60999

Tune with evidence

Change settings only for a measured reason, in a file under sysctl dot d so it survives reboots, then apply with sysctl system. Typical examples: a bigger listen queue for a busy proxy, a wider port range for a server making many outgoing connections, or lower swappiness. Note that per-process limits, like open files per process, are separate: ulimit in a shell, LimitNOFILE in a systemd unit. Too many open files is usually that limit, not the kernel's.

Common, justified changes
# /etc/sysctl.d/90-shop.conf
net.core.somaxconn = 8192
net.ipv4.ip_local_port_range
  = 10240 65535
vm.swappiness = 10
$ sudo sysctl --system  apply
per-process limits are separate:
  ulimit -n, LimitNOFILE=

Common mistakes

Mistakes. Servers set to local time zones. No time sync, or a firewall blocking it. Logs with no rotation. Rotating by rename without telling the app, so space is never freed. Pasting long tuning lists from blogs without measuring. And changing settings with sysctl minus w only, so they vanish at the next reboot.

Avoid these
  • servers in local time zones
  • no NTP, or a blocked NTP port
  • logs with no rotation
  • rename-only rotation (space never freed)
  • copying sysctl "tuning" lists from blogs
  • sysctl -w only: lost on reboot

Say it like this

In the interview: first I free space safely, truncating or rotating rather than deleting open files. Then I add rotation: daily, keep seven, compress, with copytruncate or a postrotate reload so the app reopens its log. I cap journald, set a max size for container logs, ship logs to a central system, and alert on disk usage and its growth rate. I would also check the clock is synced, because rotation and incidents depend on time.

In the interview
  • "I free space safely first, then add logrotate: daily, rotate 7, compress, and copytruncate or a postrotate reload so the app
  • reopens the file. I cap journald, set max-size for container logs, ship logs centrally and alert
  • on disk usage and growth."

Interview questions

Q1Why should servers use UTC?

Logs and events from different regions line up, there are no daylight-saving jumps, and schedules are unambiguous; convert to local time only for display.

Q2What breaks when a server clock drifts?

TLS certificate validation, signed requests and tokens (AWS SigV4, JWT), log ordering, distributed leases and locks, and scheduled jobs.

Q3How do you check time sync?

timedatectl (System clock synchronized), chronyc tracking and chronyc sources.

Q4What is copytruncate in logrotate?

Copy the log, then truncate the original in place, so an app that keeps the file open continues writing to it; a few lines can be lost.

Q5Why does rotation sometimes not free disk space?

The log was renamed but the app still writes to the open, renamed file; use copytruncate or signal the app to reopen logs in postrotate.

Q6How do you make a sysctl change permanent?

Put it in a file under /etc/sysctl.d/ and run sysctl --system (sysctl -w is lost on reboot).

Q7What is net.core.somaxconn?

The maximum length of the queue of connections waiting to be accepted on a listening socket.

Q8"Too many open files": which limit?

Usually the per-process limit (ulimit -n, LimitNOFILE in systemd), not fs.file-max.