How to automatically start Kafka upon system startup in Ubuntu?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
On modern Ubuntu systems, the right way to start Kafka automatically at boot is to run it as a systemd service. That gives you proper startup ordering, restart behavior, logs in the journal, and a consistent way to manage the broker with systemctl. The exact configuration depends on whether your Kafka cluster uses KRaft mode or the older ZooKeeper mode, but the service-management pattern is the same.
Create a Dedicated Service User
Kafka should usually run as its own non-root user.
Adjust /opt/kafka to match your installation path.
Running Kafka as a dedicated user reduces risk and makes service ownership clearer.
Write a systemd Unit File
Create /etc/systemd/system/kafka.service:
This example assumes a KRaft configuration file at /opt/kafka/config/kraft/server.properties. If your installation uses a different path, change it accordingly.
Important fields:
- '
UserandGrouprun Kafka under the dedicated account' - '
WorkingDirectorykeeps relative paths predictable' - '
Restart=on-failurerestarts the broker after abnormal exit' - '
SuccessExitStatus=143treats normal Java termination as clean'
If You Still Use ZooKeeper
Older Kafka deployments rely on ZooKeeper. In that case, Kafka should start after ZooKeeper and usually declare that dependency explicitly.
Everything else can stay similar, but the broker config file will be the ZooKeeper-era server.properties rather than a KRaft controller-broker config.
If you are on a current Kafka installation, KRaft is the direction to prefer.
Enable and Start the Service
After writing the unit file:
Check status:
View logs:
At this point the broker should start on boot and be manageable through standard systemd commands.
Test the Boot Path, Not Just the Manual Start
A service that starts manually is not always a service that survives reboot conditions correctly. After enabling it, reboot a test host or VM and verify:
- Kafka starts automatically
- storage directories are mounted before startup
- network is ready
- the configured listeners bind correctly
On real systems, boot-time bugs often come from dependency ordering rather than Kafka itself.
Practical Hardening
You can extend the unit file with environment variables if needed:
These are often better placed in a separate environment file when operations teams need to tune them without editing the unit directly.
Also make sure log and data directories are writable by the Kafka user. A surprising number of startup failures are just permission mistakes.
Common Pitfalls
The most common problem is following old guides that assume ZooKeeper even when the installed Kafka version is using KRaft. Use the correct config file and dependency model for your cluster.
Another issue is putting the wrong path in ExecStart. If the service points at a missing script or the wrong properties file, systemd will fail instantly.
Some setups also rely on shell environment such as JAVA_HOME, which is not always present when systemd launches the process. If Java is not on the service path, set the environment explicitly.
Finally, do not run Kafka as root just because it is easier during setup. Fix ownership and permissions instead.
Summary
- Use a
systemdunit to start Kafka automatically on Ubuntu boot. - Run Kafka as a dedicated non-root user.
- Point
ExecStartat the correct Kafka script and config file for KRaft or ZooKeeper mode. - Enable the service with
systemctl enable kafkaand verify it after a reboot. - Check logs with
journalctland watch for permission, path, and dependency issues.

