In previous posts, we explored in great detail what Logstash is and how to use it, mainly to transform and forward our logs.
Logstash is a powerful data pipeline tool. It ingests data, transforms it, and ships it to your desired destination. In previous versions, by default, Logstash ran as the root user. That’s risky. For that reason, with the latest versions (v9+), Logstash cannot be run as superuser.
Whether you need it for newer versions or not, running Logstash as non root user is a good idea. Running Logstash without root reduces attack surfaces. It limits accidental damage. It aligns with security best practices.
In this blog post, we will see how we can run Logstash under a different user, avoiding superuser access.
1. Create a Dedicated System User
We start by creating a new user for Logstash. This user has no password, no login shell, and no home directory. It exists only to run Logstash.sudo adduser --system --no-create-home --group --disabled-login logstashuser
2. Give the Right Permissions
Now we assign the correct ownership to the Logstash folders. We ensurelogstashuser
can read and write what it needs. Run these commands:sudo chown -R logstashuser:logstashuser /usr/share/logstash
sudo chown -R logstashuser:logstashuser /etc/logstash
sudo chown -R logstashuser:logstashuser /var/log/logstash
sudo chown -R logstashuser:logstashuser /var/lib/logstash
sudo chmod -R 755 /usr/share/logstash
3. Run Logstash Without Root
Now we start Logstash as our new user. Replace the config path with your own.sudo -u logstashuser /usr/share/logstash/bin/logstash -f /etc/logstash/conf.d/pipeline.conf
sudo -u logstashuser bash -c "export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64 && /usr/share/logstash/bin/logstash -f /etc/logstash/conf.d/pipeline.conf"
4. Run Logstash as a Service (Optional)
To run Logstash as a service under our new user, we edit the systemd unit file:sudo nano /etc/systemd/system/logstash.service
[Service]
section. Add or change these lines:User=logstashuser
Group=logstashuser
sudo systemctl daemon-reexec
sudo systemctl daemon-reload
sudo systemctl restart logstash
5. Verify the Process
Run this to confirm:ps -ef | grep logstash
logstashuser
as the owner.