Bird
Raised Fist0
Linux CLIscripting~5 mins

Startup and init systems (systemd) in Linux CLI - Commands & Configuration

Choose your learning style10 modes available
Introduction
When you turn on a Linux computer, many programs need to start automatically. Systemd is the tool that manages these programs, making sure they start in the right order and keep running. It helps your system boot up smoothly and keeps services running.
When you want a web server to start automatically after your Linux machine boots.
When you need to check if a background service like a database is running.
When you want to stop or restart a service without rebooting the whole system.
When you want to enable a custom script to run at startup.
When you want to see the status or logs of a service to troubleshoot problems.
Commands
This command starts the nginx web server immediately. Use it when you want to run the service now without rebooting.
Terminal
systemctl start nginx
Expected OutputExpected
No output (command runs silently)
This shows if the nginx service is running, stopped, or has errors. It helps you check the current state of the service.
Terminal
systemctl status nginx
Expected OutputExpected
● nginx.service - A high performance web server and a reverse proxy server Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled) Active: active (running) since Fri 2024-06-14 10:00:00 UTC; 5min ago Main PID: 1234 (nginx) Tasks: 3 (limit: 4915) Memory: 5.0M CGroup: /system.slice/nginx.service ├─1234 nginx: master process /usr/sbin/nginx -g daemon on; master_process on; ├─1235 nginx: worker process └─1236 nginx: worker process
This command makes sure nginx starts automatically every time the system boots. Use it to keep the service running after reboots.
Terminal
systemctl enable nginx
Expected OutputExpected
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /lib/systemd/system/nginx.service.
This stops nginx from starting automatically on boot. Use it if you want to prevent the service from running after reboot.
Terminal
systemctl disable nginx
Expected OutputExpected
Removed symlink /etc/systemd/system/multi-user.target.wants/nginx.service.
This shows the logs for the nginx service from the last 5 minutes. Logs help you understand what the service did or if it had any errors.
Terminal
journalctl -u nginx --since "5 minutes ago"
Expected OutputExpected
Jun 14 10:00:00 my-server systemd[1]: Started A high performance web server and a reverse proxy server. Jun 14 10:00:01 my-server nginx[1234]: nginx started successfully.
→
-u - Specifies the service unit to show logs for
→
--since - Shows logs starting from a specific time
Key Concept

If you remember nothing else from this pattern, remember: systemd controls starting, stopping, enabling, and checking services on Linux.

Common Mistakes
Trying to start a service without root or sudo privileges
Starting or managing services usually requires administrator rights, so the command will fail with permission denied.
Use sudo before the systemctl command, for example: sudo systemctl start nginx
Forgetting to enable a service after starting it
The service will run now but will not start automatically after reboot, causing downtime after restarts.
Run systemctl enable service_name to make it start on boot.
Using systemctl status without checking logs when a service fails
Status shows if the service is running but not detailed error messages, so troubleshooting is incomplete.
Use journalctl -u service_name to see detailed logs and find the cause of failure.
Summary
Use systemctl start to run a service immediately.
Use systemctl enable to make a service start automatically on boot.
Use systemctl status and journalctl to check service health and logs.

Practice

(1/5)
1. What is the main purpose of systemd in a Linux system?
easy
A. To manage how the system starts and stops programs automatically
B. To edit text files in the system
C. To compile software from source code
D. To monitor network traffic

Solution

  1. Step 1: Understand systemd's role

    systemd is a system and service manager that controls how programs start and stop on Linux.
  2. Step 2: Compare options

    Only To manage how the system starts and stops programs automatically describes managing startup and shutdown of programs, which matches systemd's purpose.
  3. Final Answer:

    To manage how the system starts and stops programs automatically -> Option A
  4. Quick Check:

    systemd manages startup and shutdown = B [OK]
Hint: Remember: systemd controls startup and shutdown of services [OK]
Common Mistakes:
  • Confusing systemd with text editors
  • Thinking systemd compiles code
  • Mixing systemd with network tools
2. Which of the following is the correct command to enable a service named nginx to start automatically at boot?
easy
A. systemctl enable nginx
B. systemctl start nginx
C. systemctl status nginx
D. systemctl stop nginx

Solution

  1. Step 1: Understand systemctl commands

    To enable a service to start at boot, the command is systemctl enable [service].
  2. Step 2: Match command to action

    systemctl enable nginx uses enable with nginx, which is correct for auto-start.
  3. Final Answer:

    systemctl enable nginx -> Option A
  4. Quick Check:

    Enable means auto-start = A [OK]
Hint: Enable means auto-start at boot, start means immediate run [OK]
Common Mistakes:
  • Using start instead of enable for auto-start
  • Confusing status with enable
  • Using stop instead of enable
3. What will be the output of the command systemctl is-active sshd if the SSH service is running?
medium
A. inactive
B. failed
C. active
D. unknown

Solution

  1. Step 1: Understand systemctl is-active

    This command checks if a service is running and returns its state.
  2. Step 2: Identify expected output when running

    If the SSH service is running, the output is active.
  3. Final Answer:

    active -> Option C
  4. Quick Check:

    Running service status = active [OK]
Hint: Active means running, inactive means stopped [OK]
Common Mistakes:
  • Confusing active with inactive
  • Expecting failed when service is running
  • Thinking unknown means running
4. You run systemctl enable apache2 but the service does not start on boot. What is the most likely reason?
medium
A. The service is already running
B. You used start instead of enable
C. The systemctl command is not installed
D. The service file for apache2 is missing or incorrect

Solution

  1. Step 1: Understand what enable does

    Enable creates links so the service starts at boot but does not start it immediately.
  2. Step 2: Identify why enable might fail

    If the service file is missing or broken, enabling won't work properly, so it won't start on boot.
  3. Final Answer:

    The service file for apache2 is missing or incorrect -> Option D
  4. Quick Check:

    Missing service file blocks enable = D [OK]
Hint: Enable needs a valid service file to work [OK]
Common Mistakes:
  • Confusing enable with start
  • Assuming systemctl is missing
  • Thinking running service means enable failed
5. You want to create a custom service that runs a script at startup. Which two steps must you perform to make sure it starts automatically with systemd?
hard
A. Add your script to /etc/rc.local and make it executable
B. Create a service file in /etc/systemd/system, then run 'systemctl enable yourservice' to auto-start it
C. Place your script in /usr/bin and reboot the system
D. Run 'systemctl start yourservice' and then 'systemctl status yourservice' only

Solution

  1. Step 1: Create a systemd service file

    Write a service file describing your script and place it in /etc/systemd/system/ to define how systemd runs it.
  2. Step 2: Enable the service

    Run systemctl enable yourservice to make it start automatically at boot.
  3. Final Answer:

    Create a service file in /etc/systemd/system, then run 'systemctl enable yourservice' to auto-start it -> Option B
  4. Quick Check:

    Create service file + enable = auto-start [OK]
Hint: Create service file and enable it to auto-start [OK]
Common Mistakes:
  • Only starting service without enabling
  • Using old /etc/rc.local method
  • Placing script without service file
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard