# Cheatsheet & Examples: systemctl

systemctl is a command-line utility used to control the systemd system and service manager. It allows users to manage system services, check their status, enable/disable them at boot, and more.

## Check the status of a service  
Example Usage:  
`systemctl status sshd`  

What it does:  
Displays the current status of the `sshd` service, including whether it is active, running, or failed.  

Command-line Arguments Explained:  
- `status`: The main command to check the status of a unit.  
- `sshd`: The name of the service unit (e.g., SSH daemon).  

## Start a service  
Example Usage:  
`systemctl start httpd`  

What it does:  
Starts the `httpd` service immediately.  

Command-line Arguments Explained:  
- `start`: The command to initiate the specified service.  
- `httpd`: The service unit to be started (e.g., Apache web server).  

## Stop a service  
Example Usage:  
`systemctl stop nginx`  

What it does:  
Stops the `nginx` service.  

Command-line Arguments Explained:  
- `stop`: The command to terminate the specified service.  
- `nginx`: The service unit to be stopped (e.g., NGINX web server).  

## Restart a service  
Example Usage:  
`systemctl restart postfix`  

What it does:  
Restarts the `postfix` service, stopping and then starting it again.  

Command-line Arguments Explained:  
- `restart`: The command to stop and then start the specified service.  
- `postfix`: The service unit to be restarted (e.g., mail transfer agent).  

## Reload a service configuration  
Example Usage:  
`systemctl reload docker`  

What it does:  
Reloads the configuration of the `docker` service without restarting it.  

Command-line Arguments Explained:  
- `reload`: The command to reapply the service's configuration.  
- `docker`: The service unit to reload (e.g., container runtime).  

## Enable a service to start at boot  
Example Usage:  
`systemctl enable crond`  

What it does:  
Enables the `crond` service to start automatically during system boot.  

Command-line Arguments Explained:  
- `enable`: The command to configure the service for automatic startup.  
- `crond`: The service unit to enable (e.g., cron daemon).  

## Disable a service from starting at boot  
Example Usage:  
`systemctl disable cups`  

What it does:  
Disables the `cups` service from starting automatically at boot.  

Command-line Arguments Explained:  
- `disable`: The command to prevent the service from starting automatically.  
- `cups`: The service unit to disable (e.g., print spooler).  

## List all active service units  
Example Usage:  
`systemctl list-units --type=service --state=running`  

What it does:  
Lists all active service units that are currently in the "running" state.  

Command-line Arguments Explained:  
- `list-units`: Displays the status of units.  
- `--type=service`: Filters the output to show only service units.  
- `--state=running`: Displays only units in the "running" state.  

## List all unit files  
Example Usage:  
`systemctl list-unit-files`  

What it does:  
Shows all unit files on the system with their loading and activation status.  

Command-line Arguments Explained:  
- `list-unit-files`: Displays a list of all unit files.  

## Reload systemd configuration  
Example Usage:  
`systemctl daemon-reload`  

What it does:  
Reloads the systemd manager configuration after changes to unit files.  

Command-line Arguments Explained:  
- `daemon-reload`: Forces systemd to re-read configuration files.  

## Check if a service is active  
Example Usage:  
`systemctl is-active sshd`  

What it does:  
Reports whether the `sshd` service is active (e.g., "active" or "inactive").  

Command-line Arguments Explained:  
- `is-active`: The command to check the activation state of a unit.  
- `sshd`: The service unit to evaluate.  

## Check if a service is enabled  
Example Usage:  
`systemctl is-enabled firewalld`  

What it does:  
Checks if the `firewalld` service is configured to start at boot.  

Command-line Arguments Explained:  
- `is-enabled`: The command to verify a unit’s enablement status.  
- `firewalld`: The service unit to check.  

## View the content of a unit file  
Example Usage:  
`systemctl cat sshd.service`  

What it does:  
Prints the contents of the `sshd.service` unit file for inspection.  

Command-line Arguments Explained:  
- `cat`: The command to output a unit file's content.  
- `sshd.service`: The specific unit file to display.  

## List dependencies of a service  
Example Usage:  
`systemctl list-dependencies nginx`  

What it does:  
Displays the dependency hierarchy for the `nginx` service.  

Command-line Arguments Explained:  
- `list-dependencies`: Shows dependencies of the specified unit.  
- `nginx`: The service unit to analyze for dependencies.  

## Show failed units  
Example Usage:  
`systemctl --failed`  

What it does:  
Lists all units that have failed to load or start.  

Command-line Arguments Explained:  
- `--failed`: Filters units to show only those in a failed state.  

## Reset failed status of a unit  
Example Usage:  
`systemctl reset-failed systemd-journald`  

What it does:  
Clears the "failed" state flag for the `systemd-journald` unit.  

Command-line Arguments Explained:  
- `reset-failed`: Clears the failed status of a unit.  
- `systemd-journald`: The unit to reset.  

## Disable terminal paging  
Example Usage:  
`systemctl --no-pager status nginx`  

What it does:  
Prevents the output of the `status` command from being paginated.  

Command-line Arguments Explained:  
- `--no-pager`: Disables the use of a pager (e.g., `less`) for output.  
- `status`: Checks the status of the specified unit.  
- `nginx`: The service unit to check.  

## Check logs for a service  

Refer to [`journalctl`](https://tech.mrleong.net/cheatsheet-examples-journalctl).

## Show unit file details  
Example Usage:  
`systemctl show sshd`  

What it does:  
Prints detailed properties of the `sshd` unit file.  

Command-line Arguments Explained:  
- `show`: Outputs detailed information about a unit.  
- `sshd`: The service unit to inspect.  

## Check the unit file’s state  
Example Usage:  
`systemctl is-failed nginx`  

What it does:  
Checks if the `nginx` service has failed to start or load.  

Command-line Arguments Explained:  
- `is-failed`: Reports whether the specified unit has failed.  
- `nginx`: The service unit to check.
