Linux Cheatsheet
systemd and Services
Use this Linux reference while you build software engineering projects, review code for technical interview prep, or polish examples for a software engineer resume.
systemctl — Service Control
systemctl start nginx # start service systemctl stop nginx # stop service systemctl restart nginx # stop + start systemctl reload nginx # reload config (no downtime if supported) systemctl status nginx # status + recent log lines systemctl enable nginx # start at boot systemctl disable nginx # don't start at boot systemctl enable --now nginx # enable + start immediately systemctl disable --now nginx # disable + stop immediately systemctl is-active nginx # active / inactive (exit 0/1) systemctl is-enabled nginx # enabled / disabled systemctl is-failed nginx # failed / other
Listing Units
systemctl list-units # all active units systemctl list-units --type=service # services only systemctl list-units --state=failed # failed units systemctl list-units --state=running # running services systemctl list-unit-files # all installed units + state systemctl list-unit-files --type=service # service unit files systemctl list-dependencies nginx # dependency tree
System State
systemctl poweroff # power off systemctl reboot # reboot systemctl suspend # suspend to RAM systemctl hibernate # suspend to disk systemctl rescue # single-user rescue mode systemctl emergency # minimal emergency shell systemctl daemon-reload # reload unit files after editing them systemctl reset-failed # clear failed state from all units systemctl reset-failed nginx # clear failed state for nginx
journalctl — Log Viewer
journalctl # all logs (oldest first) journalctl -r # reverse (newest first) journalctl -f # follow (tail -f equivalent) journalctl -n 50 # last 50 lines journalctl -u nginx # logs for nginx only journalctl -u nginx -f # follow nginx logs journalctl -u nginx --since today journalctl --since "2024-01-15 10:00" --until "2024-01-15 11:00" journalctl -p err # priority: emerg alert crit err warning notice info debug journalctl -p err..crit # range of priorities journalctl -k # kernel messages only (dmesg) journalctl -b # current boot journalctl -b -1 # previous boot journalctl --list-boots # list all boots journalctl -o json-pretty # JSON output journalctl --disk-usage # journal disk size journalctl --vacuum-size=500M # trim journal to 500 MB journalctl --vacuum-time=2weeks # delete logs older than 2 weeks
Unit Files
Unit files live in:
- /lib/systemd/system/ — package-provided (don't edit)
- /etc/systemd/system/ — local overrides and custom units
- ~/.config/systemd/user/ — user units
Service Unit Example
# /etc/systemd/system/myapp.service
[Unit]
Description=My Application
After=network.target
Requires=postgresql.service
[Service]
Type=simple
User=deploy
WorkingDirectory=/opt/myapp
ExecStart=/usr/bin/node server.js
ExecReload=/bin/kill -HUP $MAINPID
Restart=on-failure
RestartSec=5s
StandardOutput=journal
StandardError=journal
Environment=NODE_ENV=production
EnvironmentFile=/etc/myapp/env
[Install]
WantedBy=multi-user.targetsystemctl daemon-reload # required after creating/editing unit files systemctl enable --now myapp # enable + start
Timer Unit Example (cron replacement)
# /etc/systemd/system/backup.timer [Unit] Description=Daily Backup Timer [Timer] OnCalendar=daily Persistent=true [Install] WantedBy=timers.target
# /etc/systemd/system/backup.service [Unit] Description=Backup Service [Service] Type=oneshot ExecStart=/usr/local/bin/backup.sh
systemctl enable --now backup.timer
systemctl list-timers # list all active timersOverride/Drop-in Files
systemctl edit nginx # create drop-in override (recommended) # Creates /etc/systemd/system/nginx.service.d/override.conf systemctl edit --full nginx # edit full unit copy systemctl cat nginx # show effective unit file + overrides
Target Units (Runlevels)
| Target | Equivalent runlevel |
|---|---|
poweroff.target | 0 |
rescue.target | 1 |
multi-user.target | 3 |
graphical.target | 5 |
reboot.target | 6 |
systemctl get-default # show default target systemctl set-default multi-user.target # boot into multi-user (no GUI) systemctl isolate rescue.target # switch target now
User Services (non-root)
systemctl --user status myservice # user-scoped service systemctl --user enable --now myservice journalctl --user -u myservice loginctl enable-linger alice # run user services after logout
Analyzing Boot Performance
systemd-analyze # total boot time systemd-analyze blame # per-unit time, sorted systemd-analyze critical-chain # critical path systemd-analyze plot > boot.svg # visual timeline