<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Ahmad Zia</title>
    <description>The latest articles on DEV Community by Ahmad Zia (@ahmad01).</description>
    <link>https://dev.to/ahmad01</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2889584%2Fbd022ddb-cfdc-4b8f-a152-16185ad138e0.jpg</url>
      <title>DEV Community: Ahmad Zia</title>
      <link>https://dev.to/ahmad01</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ahmad01"/>
    <language>en</language>
    <item>
      <title>Managing Apache Tomcat with systemd on Linux – A DevOps Guide</title>
      <dc:creator>Ahmad Zia</dc:creator>
      <pubDate>Sun, 13 Jul 2025 03:39:39 +0000</pubDate>
      <link>https://dev.to/ahmad01/managing-apache-tomcat-with-systemd-on-linux-a-devops-guide-1j2k</link>
      <guid>https://dev.to/ahmad01/managing-apache-tomcat-with-systemd-on-linux-a-devops-guide-1j2k</guid>
      <description>&lt;p&gt;As a DevOps engineer, automation and service management are critical. While working with Java-based web applications, Apache Tomcat is one of the most widely used tools to serve Java servlets and JSPs. However, starting and stopping Tomcat manually using shell scripts (&lt;code&gt;startup.sh&lt;/code&gt; and &lt;code&gt;shutdown.sh&lt;/code&gt;) isn’t ideal in a production environment.&lt;/p&gt;

&lt;p&gt;In this guide, we’ll walk through setting up Tomcat as a systemd service, allowing us to manage it easily using &lt;code&gt;systemctl&lt;/code&gt; — just like any other system service!&lt;/p&gt;

&lt;h2&gt;
  
  
  ✅ What is Tomcat?
&lt;/h2&gt;

&lt;p&gt;Apache Tomcat is an open-source application server developed by the Apache Software Foundation. It's used to host Java-based web applications, similar to how Apache HTTPD serves websites.&lt;/p&gt;

&lt;h3&gt;
  
  
  🛠️ Setup Steps
&lt;/h3&gt;

&lt;h4&gt;
  
  
  1. Install Required Software
&lt;/h4&gt;

&lt;p&gt;First, install Tomcat and Java:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo yum install java-17-openjdk -y

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Download and extract Tomcat from the official website. You’ll typically extract it using:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tar -xvzf apache-tomcat-9.x.xx.tar.gz

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  2. Start Tomcat Manually (Initial Test)
&lt;/h4&gt;

&lt;p&gt;To make sure everything works initially, run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cd apache-tomcat-9.x.xx/bin./startup.sh

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, open your browser and visit:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;http://&amp;lt;your_server_ip&amp;gt;:8080

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You should see the Tomcat welcome page.&lt;/p&gt;

&lt;h2&gt;
  
  
  ❌ The Problem
&lt;/h2&gt;

&lt;p&gt;By default, Tomcat requires manual start/stop using shell scripts. That’s not ideal for production or automation. We need to integrate it into our system’s service manager – &lt;code&gt;systemd&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  ✅ The DevOps Solution: Use systemctl
&lt;/h3&gt;

&lt;p&gt;We’ll create a custom systemd service file for Tomcat, enabling us to manage it using:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo systemctl start tomcat
sudo systemctl stop tomcat
sudo systemctl enable tomcat
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  🧰 Step-by-Step Guide
&lt;/h3&gt;

&lt;h4&gt;
  
  
  🔹 Step 1: Create a Non-root Tomcat User
&lt;/h4&gt;

&lt;p&gt;It’s a security best practice to run services as non-root users.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo useradd -m -U -d /opt/tomcat -s /bin/false tomcat
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  🔹 Step 2: Move Tomcat Files
&lt;/h4&gt;

&lt;p&gt;Assuming you’ve already extracted Tomcat:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo mv apache-tomcat-9.x.xx /opt/tomcat
sudo chown -R tomcat: /opt/tomcat

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  🔹 Step 3: Create systemd Service File
&lt;/h4&gt;

&lt;p&gt;Create a new file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo nano /etc/systemd/system/tomcat.service
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Paste the following configuration:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
[Unit]
Description=Apache Tomcat Web Application Container
After=network.target

[Service]
User=tomcat
Group=tomcat
Environment=JAVA_HOME=/usr/lib/jvm/jre
Environment=CATALINA_HOME=/opt/tomcat
Environment=CATALINA_BASE=/opt/tomcat
WorkingDirectory=/opt/tomcat
ExecStart=/opt/tomcat/bin/startup.sh
ExecStop=/opt/tomcat/bin/shutdown.sh
Restart=on-failure

[Install]
WantedBy=multi-user.target

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;🔎 Note: Replace JAVA_HOME with the actual path of your installed JDK, if different.&lt;/p&gt;

&lt;h4&gt;
  
  
  🔹 Step 4: Reload systemd and Start Tomcat
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo systemctl daemon-reload
sudo systemctl start tomcat

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To make Tomcat start automatically at boot:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo systemctl enable tomcat
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To check the status:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo systemctl status tomcat
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  🎯 Final Test
&lt;/h4&gt;

&lt;p&gt;Visit your server IP on port 8080 again:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;http://&amp;lt;your_server_ip&amp;gt;:8080
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you see the Tomcat page — congratulations! You’ve successfully integrated Tomcat into your Linux service system.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Granting a User Access to Only apt: A Hands-On Experiment with sudoers</title>
      <dc:creator>Ahmad Zia</dc:creator>
      <pubDate>Sat, 22 Feb 2025 03:33:51 +0000</pubDate>
      <link>https://dev.to/ahmad01/granting-a-user-access-to-only-apt-a-hands-on-experiment-with-sudoers-3bb0</link>
      <guid>https://dev.to/ahmad01/granting-a-user-access-to-only-apt-a-hands-on-experiment-with-sudoers-3bb0</guid>
      <description>&lt;p&gt;So, I wanted to give a specific user the ability to use &lt;code&gt;apt&lt;/code&gt;, but nothing else. I knew this had to be done via the &lt;code&gt;sudoers&lt;/code&gt; file, but I wasn’t exactly sure how. No worries—just open the file and figure it out, right?&lt;/p&gt;

&lt;h2&gt;
  
  
  Opening the sudoers File
&lt;/h2&gt;

&lt;p&gt;I ran:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;visudo
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This opened up the &lt;code&gt;sudoers&lt;/code&gt; file, where I started looking for something that controlled user privileges. I saw this familiar-looking line:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;username &lt;span class="nv"&gt;ALL&lt;/span&gt;&lt;span class="o"&gt;=(&lt;/span&gt;ALL:ALL&lt;span class="o"&gt;)&lt;/span&gt; ALL
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At first, I had no idea what it meant, so I Googled it. Turns out, the last &lt;code&gt;ALL&lt;/code&gt; means the user can run &lt;strong&gt;all&lt;/strong&gt; commands. That was my hint—this is where I had to tweak things.&lt;/p&gt;

&lt;h2&gt;
  
  
  Changing Access to apt
&lt;/h2&gt;

&lt;p&gt;So, I replaced &lt;code&gt;ALL&lt;/code&gt; with &lt;code&gt;apt&lt;/code&gt;, thinking this would restrict the user to only using &lt;code&gt;apt&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;username &lt;span class="nv"&gt;ALL&lt;/span&gt;&lt;span class="o"&gt;=(&lt;/span&gt;ALL:ALL&lt;span class="o"&gt;)&lt;/span&gt; apt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I saved the file, but when I tried to use &lt;code&gt;apt&lt;/code&gt; with the restricted user, I got an error—something about a &lt;strong&gt;path issue&lt;/strong&gt;. I wasn’t sure what was going wrong, so I experimented a bit.&lt;/p&gt;

&lt;h2&gt;
  
  
  Changing apt to APT
&lt;/h2&gt;

&lt;p&gt;Next, I tried changing &lt;code&gt;apt&lt;/code&gt; to uppercase &lt;code&gt;APT&lt;/code&gt;, just in case:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;username &lt;span class="nv"&gt;ALL&lt;/span&gt;&lt;span class="o"&gt;=(&lt;/span&gt;ALL:ALL&lt;span class="o"&gt;)&lt;/span&gt; APT
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This time, the file saved successfully, but the user still couldn’t run &lt;code&gt;apt&lt;/code&gt;. The error message clearly said something about &lt;strong&gt;no access to &lt;code&gt;/usr/bin/apt&lt;/code&gt;&lt;/strong&gt;. That was the real problem.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Final Fix: Specifying the Full Path
&lt;/h2&gt;

&lt;p&gt;So, I copied the path &lt;code&gt;/usr/bin/apt&lt;/code&gt; from the error message and used it explicitly in the &lt;code&gt;sudoers&lt;/code&gt; file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;username &lt;span class="nv"&gt;ALL&lt;/span&gt;&lt;span class="o"&gt;=(&lt;/span&gt;ALL:ALL&lt;span class="o"&gt;)&lt;/span&gt; /usr/bin/apt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Saved the file, tested it, and boom—it worked! Now, the user could run &lt;code&gt;apt&lt;/code&gt;, but nothing else.&lt;/p&gt;

&lt;h2&gt;
  
  
  Lessons Learned
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;sudoers&lt;/code&gt; file controls which commands a user can execute with &lt;code&gt;sudo&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The last &lt;code&gt;ALL&lt;/code&gt; in &lt;code&gt;ALL=(ALL:ALL) ALL&lt;/code&gt; defines which commands a user can run.&lt;/li&gt;
&lt;li&gt;Specifying just &lt;code&gt;apt&lt;/code&gt; doesn’t work—you need the full path (&lt;code&gt;/usr/bin/apt&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;Always test changes in a separate terminal before closing &lt;code&gt;visudo&lt;/code&gt;, so you don’t lock yourself out!&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That’s it! Hope this helps if you ever need to restrict users to specific commands.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
