How to view running units → systemctl list-units --type=service

How to View Running Units → systemctl list-units --type=service Table of Contents 1. [Introduction](#introduction) 2. [Prerequisites](#prerequisites) 3. [Understanding systemd and Service Units](#understanding-systemd-and-service-units) 4. [Basic Command Syntax](#basic-command-syntax) 5. [Step-by-Step Instructions](#step-by-step-instructions) 6. [Command Options and Variations](#command-options-and-variations) 7. [Practical Examples and Use Cases](#practical-examples-and-use-cases) 8. [Understanding Output Format](#understanding-output-format) 9. [Advanced Filtering and Sorting](#advanced-filtering-and-sorting) 10. [Common Issues and Troubleshooting](#common-issues-and-troubleshooting) 11. [Best Practices](#best-practices) 12. [Related Commands](#related-commands) 13. [Conclusion](#conclusion) Introduction The `systemctl list-units --type=service` command is one of the most fundamental tools for Linux system administrators and users who need to monitor and manage system services. This command provides a comprehensive view of all service units currently loaded in the systemd service manager, showing their current status, activity state, and basic information. In this comprehensive guide, you'll learn how to effectively use this command to monitor running services, understand the output format, apply various filters and options, and troubleshoot common issues. Whether you're a beginner learning Linux system administration or an experienced professional looking to refine your skills, this article will provide you with the knowledge and practical examples needed to master service monitoring with systemctl. By the end of this guide, you'll be able to confidently view, filter, and analyze running services on any systemd-based Linux distribution, making you more effective at system monitoring, troubleshooting, and maintenance tasks. Prerequisites Before diving into the details of viewing running units with systemctl, ensure you have the following prerequisites: System Requirements - A Linux distribution that uses systemd (most modern distributions including Ubuntu 16.04+, CentOS 7+, Fedora, Debian 8+, RHEL 7+) - Terminal or command-line access to the system - Basic familiarity with Linux command-line interface User Permissions - Regular user privileges for viewing service status - Root or sudo privileges may be required for certain advanced operations - Understanding of basic Linux file permissions and user roles Knowledge Prerequisites - Basic understanding of Linux command-line navigation - Familiarity with terminal operations - Basic knowledge of what services are in the context of operating systems Verification Steps To verify your system is ready, run these commands: ```bash Check if systemd is running systemctl --version Verify you can access systemctl systemctl list-units --help ``` If these commands execute without errors, your system is ready to proceed. Understanding systemd and Service Units What is systemd? systemd is a system and service manager for Linux operating systems that has become the standard init system for most major Linux distributions. It manages the boot process, system services, and various system resources through a concept called "units." Service Units Explained Service units are one type of systemd unit that represents system services or daemons. These units define: - How services should be started and stopped - Dependencies between services - Resource requirements and limitations - Restart policies and failure handling Unit States Service units can exist in several states: - loaded: The unit configuration has been loaded into memory - active: The unit is currently running - inactive: The unit is not currently running - failed: The unit failed to start or crashed - activating: The unit is in the process of starting - deactivating: The unit is in the process of stopping Basic Command Syntax The basic syntax for viewing service units is straightforward: ```bash systemctl list-units --type=service ``` Command Components Breakdown - `systemctl`: The main command for controlling systemd - `list-units`: Subcommand to list units - `--type=service`: Filter to show only service-type units Alternative Syntax Forms You can also use these equivalent forms: ```bash Short form systemctl list-units -t service Explicit all units (default behavior) systemctl list-units --type=service --all Show only active units (default) systemctl list-units --type=service --state=active ``` Step-by-Step Instructions Step 1: Open Your Terminal First, open your terminal application. This varies by desktop environment: - GNOME: Press `Ctrl+Alt+T` or search for "Terminal" - KDE: Press `Ctrl+Alt+T` or open Konsole - Command line systems: You're already in the terminal Step 2: Execute the Basic Command Run the fundamental command to view all loaded service units: ```bash systemctl list-units --type=service ``` Step 3: Analyze the Output The command will display a table with columns showing: - UNIT: The name of the service unit - LOAD: Whether the unit configuration is loaded - ACTIVE: The high-level activation state - SUB: The low-level activation state - DESCRIPTION: A brief description of the service Step 4: Navigate Through Results If you have many services, the output might be lengthy. Use these navigation techniques: ```bash Pipe through less for easier navigation systemctl list-units --type=service | less Pipe through grep to search for specific services systemctl list-units --type=service | grep ssh ``` Step 5: Verify Specific Services To check if specific services are running, you can filter the output: ```bash Look for web servers systemctl list-units --type=service | grep -E "(apache|nginx|httpd)" Check database services systemctl list-units --type=service | grep -E "(mysql|postgresql|mariadb)" ``` Command Options and Variations Common Options Show All Units (Including Inactive) ```bash systemctl list-units --type=service --all ``` This displays all service units, including those that are inactive or failed. Filter by State ```bash Show only active services systemctl list-units --type=service --state=active Show only failed services systemctl list-units --type=service --state=failed Show only inactive services systemctl list-units --type=service --state=inactive ``` Disable Paging ```bash systemctl list-units --type=service --no-pager ``` This prevents the output from being piped through a pager, useful for scripting. Show Full Names ```bash systemctl list-units --type=service --full ``` This displays full unit names without truncation. Advanced Options Limit Output Lines ```bash systemctl list-units --type=service --lines=20 ``` Show Plain Output (No Colors) ```bash systemctl list-units --type=service --plain ``` Quiet Mode ```bash systemctl list-units --type=service --quiet ``` Practical Examples and Use Cases Example 1: System Health Check Perform a quick system health check by viewing all active services: ```bash systemctl list-units --type=service --state=active ``` Expected Output: ``` UNIT LOAD ACTIVE SUB DESCRIPTION accounts-daemon.service loaded active running Accounts Service acpid.service loaded active running ACPI event daemon apache2.service loaded active running The Apache HTTP Server bluetooth.service loaded active running Bluetooth service cron.service loaded active running Regular background program processing daemon dbus.service loaded active running D-Bus System Message Bus ``` Example 2: Troubleshooting Failed Services Identify services that have failed to start: ```bash systemctl list-units --type=service --state=failed ``` Expected Output: ``` UNIT LOAD ACTIVE SUB DESCRIPTION networking.service loaded failed failed Raise network interfaces postgresql.service loaded failed failed PostgreSQL RDBMS ``` Example 3: Web Server Status Check Check the status of common web servers: ```bash systemctl list-units --type=service | grep -E "(apache|nginx|httpd)" ``` Expected Output: ``` apache2.service loaded active running The Apache HTTP Server ``` Example 4: Database Services Monitoring Monitor database-related services: ```bash systemctl list-units --type=service | grep -E "(mysql|postgresql|mariadb|redis|mongodb)" ``` Example 5: Security Services Audit Check security-related services: ```bash systemctl list-units --type=service | grep -E "(ssh|firewall|fail2ban|apparmor|selinux)" ``` Example 6: System Services Overview Get a complete overview with counts: ```bash echo "=== SERVICE STATUS SUMMARY ===" echo "Active services:" systemctl list-units --type=service --state=active --no-pager | wc -l echo "Failed services:" systemctl list-units --type=service --state=failed --no-pager | wc -l echo "Inactive services:" systemctl list-units --type=service --state=inactive --no-pager | wc -l ``` Understanding Output Format Column Descriptions UNIT Column - Contains the full name of the service unit - Always ends with `.service` - Names are typically descriptive of the service function LOAD Column - loaded: Unit configuration successfully loaded - not-found: Unit file not found - bad-setting: Configuration contains errors - error: Other loading errors - masked: Unit is masked and cannot be started ACTIVE Column - active: Unit is active and running - inactive: Unit is not running - activating: Unit is starting up - deactivating: Unit is shutting down - failed: Unit failed to start or crashed - reloading: Unit is reloading its configuration SUB Column - Provides more detailed state information - For active services: usually "running" - For inactive services: usually "dead" - For failed services: usually "failed" - Other states: "start-pre", "start-post", "reload", "stop", etc. DESCRIPTION Column - Human-readable description of the service - Helps identify the service purpose - May be truncated in standard output Sample Output Analysis ```bash UNIT LOAD ACTIVE SUB DESCRIPTION ssh.service loaded active running OpenBSD Secure Shell server systemd-resolved.service loaded active running Network Name Resolution cron.service loaded active running Regular background program processing daemon ``` Analysis: - ssh.service: SSH server is loaded, active, and running normally - systemd-resolved.service: DNS resolution service is operational - cron.service: Task scheduler is functioning correctly Advanced Filtering and Sorting Using grep for Pattern Matching Case-Insensitive Search ```bash systemctl list-units --type=service | grep -i PATTERN ``` Multiple Pattern Search ```bash systemctl list-units --type=service | grep -E "(pattern1|pattern2|pattern3)" ``` Exclude Patterns ```bash systemctl list-units --type=service | grep -v "unwanted-pattern" ``` Combining with Other Tools Sort by Service Name ```bash systemctl list-units --type=service --no-pager | sort ``` Count Services by State ```bash systemctl list-units --type=service --all --no-pager | awk '{print $3}' | sort | uniq -c ``` Extract Only Service Names ```bash systemctl list-units --type=service --no-pager | awk '{print $1}' | grep '\.service$' ``` Creating Custom Scripts Service Status Report Script ```bash #!/bin/bash echo "=== SYSTEM SERVICE REPORT ===" echo "Generated on: $(date)" echo "" echo "Active Services:" systemctl list-units --type=service --state=active --no-pager | wc -l echo "" echo "Failed Services:" systemctl list-units --type=service --state=failed --no-pager echo "" echo "Critical Services Status:" for service in ssh apache2 mysql postgresql nginx; do if systemctl is-active --quiet $service.service; then echo "$service: RUNNING" else echo "$service: NOT RUNNING" fi done ``` Common Issues and Troubleshooting Issue 1: Permission Denied Errors Problem: Getting permission denied when running systemctl commands. Solution: ```bash If you need elevated privileges sudo systemctl list-units --type=service Check your user groups groups $USER Add user to systemd-journal group if needed sudo usermod -a -G systemd-journal $USER ``` Issue 2: Command Not Found Problem: `systemctl: command not found` Diagnosis: ```bash Check if systemd is installed which systemctl ls -la /bin/systemctl /usr/bin/systemctl Check system init ps -p 1 ``` Solution: - Ensure you're on a systemd-based system - Install systemd if missing (distribution-specific) - Use alternative commands on non-systemd systems (service, chkconfig) Issue 3: Truncated Output Problem: Service names or descriptions are cut off. Solution: ```bash Use --full option systemctl list-units --type=service --full Adjust terminal width export COLUMNS=200 systemctl list-units --type=service Use --no-pager for complete output systemctl list-units --type=service --no-pager ``` Issue 4: Too Much Output Problem: Output is overwhelming with too many services. Solution: ```bash Filter by state systemctl list-units --type=service --state=active Use pagination systemctl list-units --type=service | less Filter specific services systemctl list-units --type=service | grep "important-service" ``` Issue 5: Outdated Information Problem: Service status seems outdated or incorrect. Solution: ```bash Reload systemd manager configuration sudo systemctl daemon-reload Check specific service status systemctl status service-name.service Force refresh sudo systemctl restart systemd-logind ``` Issue 6: No Output or Empty Results Problem: Command returns no results when services should exist. Diagnosis and Solution: ```bash Check if systemd is running systemctl status Verify systemd is the init system ls -la /sbin/init Check for masked units systemctl list-unit-files --type=service | grep masked Look for all unit types systemctl list-units --all ``` Best Practices Regular Monitoring Practices Daily Health Checks ```bash Create a daily service check alias alias service-check='systemctl list-units --type=service --state=failed' ``` Automated Monitoring ```bash #!/bin/bash Add to cron for regular monitoring FAILED_SERVICES=$(systemctl list-units --type=service --state=failed --no-pager --quiet) if [ -n "$FAILED_SERVICES" ]; then echo "Failed services detected:" | mail -s "Service Alert" admin@example.com echo "$FAILED_SERVICES" | mail -s "Service Alert Details" admin@example.com fi ``` Documentation and Logging Service Inventory ```bash Create service inventory systemctl list-units --type=service --all --no-pager > /var/log/service-inventory-$(date +%Y%m%d).txt ``` Change Tracking ```bash Track service changes systemctl list-units --type=service --no-pager | sort > /tmp/services-current diff /tmp/services-previous /tmp/services-current mv /tmp/services-current /tmp/services-previous ``` Performance Considerations Efficient Filtering ```bash Use specific state filters instead of grepping all output systemctl list-units --type=service --state=active Instead of: systemctl list-units --type=service --all | grep active ``` Scripting Best Practices ```bash Use --no-pager in scripts systemctl list-units --type=service --no-pager Use --quiet for boolean checks if systemctl is-active --quiet service-name; then echo "Service is running" fi ``` Security Considerations Information Disclosure - Be cautious when sharing systemctl output as it may reveal system configuration - Filter sensitive service names when creating reports - Use appropriate user permissions for monitoring scripts Access Control ```bash Limit access to service information sudo chmod 750 /path/to/monitoring/scripts sudo chown root:admin /path/to/monitoring/scripts ``` Related Commands Complementary systemctl Commands Individual Service Status ```bash Detailed status of specific service systemctl status service-name.service Check if service is active systemctl is-active service-name.service Check if service is enabled systemctl is-enabled service-name.service ``` Service Management ```bash Start a service sudo systemctl start service-name.service Stop a service sudo systemctl stop service-name.service Restart a service sudo systemctl restart service-name.service Reload service configuration sudo systemctl reload service-name.service ``` Unit File Management ```bash List all unit files systemctl list-unit-files --type=service Show unit file content systemctl cat service-name.service Edit unit file sudo systemctl edit service-name.service ``` Alternative Monitoring Tools Using journalctl ```bash View service logs journalctl -u service-name.service Follow service logs journalctl -u service-name.service -f View logs for all services journalctl --no-pager ``` Process Monitoring ```bash Show processes for services ps aux | grep service-name Show systemd process tree systemctl status --no-pager ``` Integration with Other Tools Combining with ps ```bash Show running processes for active services systemctl list-units --type=service --state=active --no-pager | awk '{print $1}' | while read service; do echo "=== $service ===" systemctl show -p MainPID $service 2>/dev/null done ``` Integration with monitoring systems ```bash Export for monitoring systems systemctl list-units --type=service --output=json ``` Conclusion The `systemctl list-units --type=service` command is an essential tool for Linux system administration, providing comprehensive visibility into system services. Throughout this guide, we've covered everything from basic usage to advanced filtering techniques, troubleshooting common issues, and implementing best practices. Key Takeaways 1. Basic Monitoring: Use `systemctl list-units --type=service` for quick service overviews 2. State Filtering: Apply `--state` options to focus on specific service conditions 3. Troubleshooting: Regularly check for failed services and understand output formats 4. Automation: Integrate commands into scripts for automated monitoring 5. Best Practices: Implement regular monitoring routines and proper documentation Next Steps To further develop your systemd service management skills: 1. Practice Regular Monitoring: Set up daily service health checks 2. Learn Service Management: Master starting, stopping, and configuring services 3. Explore Advanced Features: Study systemd unit files and dependencies 4. Implement Automation: Create monitoring scripts and alerts 5. Study Related Tools: Learn journalctl for log analysis and systemd-analyze for performance Final Recommendations - Bookmark commonly used command variations for quick reference - Create aliases for frequently used combinations - Document your system's critical services for faster troubleshooting - Practice these commands in a test environment before using in production - Stay updated with systemd developments and new features By mastering the `systemctl list-units --type=service` command and its variations, you'll have a powerful tool for maintaining system health, troubleshooting issues, and ensuring optimal service performance on any systemd-based Linux system.