Hi friend! As a fellow Linux admin, I know how important it is to master systemd and systemctl. It can seem daunting at first, but with some guidance, you‘ll be a pro in no time!
The default system and service manager for most Linux distributions now is systemd
. It replaces the old SysV init
system.
Here‘s a quick overview of what systemd does:
- Runs as PID 1, the first process after kernel initialization
- Starts services in parallel for faster boot time
- Manages mount points, devices, sockets, timers, and more
- Provides advanced logging and diagnostics
According to the 2020 StackOverflow developer survey, over 75% of developers now use Linux. As Linux admins, understanding systemd is a must-have skill.
Let‘s dive in and see how to manage services with systemctl!
Units: The Building Blocks of Systemd
The configuration files that tell systemd what to manage are called units. These are located in the /lib/systemd/system
directory.
There are several types of units, but for service management we care about .service
units. Each service has a unit file defining its configuration.
For example, sshd.service, apache2.service, etc.
Introducing Systemctl: Your Command Line Interface
systemctl
is the main tool for controlling systemd and services. It has a ton of options!
Here are some common usages I‘ll cover in this guide:
- Starting, stopping services
- Checking status
- Enabling/disabling services
- Reloading configurations
- And more!
Ok, enough background. Let‘s see systemctl in action.
Start and Stop Services
The most basic operations are starting and stopping services.
To start a service:
# systemctl start service_name
For example starting nginx:
# systemctl start nginx
To stop a running service:
# systemctl stop service_name
Stopping nginx:
# systemctl stop nginx
Easy right? By default, systemctl will look for a .service
file matching the service name you specify.
Restart and Reload Services
Sometimes you need to restart a service after making config changes. Use restart
for this:
# systemctl restart service_name
Restarting nginx to apply config changes:
# systemctl restart nginx
If you only need to reload the config file without a full restart, use reload
:
# systemctl reload service_name
Reloading nginx config:
# systemctl reload nginx
Reload just rereads the config file. Restart stops and starts the process which has more overhead, so use reload when you can.
Check Service Status
While a service is running, you can check its status with:
# systemctl status service_name
For example, when nginx is running you would see:
● nginx.service - A high performance web server and a reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: active (running) since Fri 2022-01-28 20:13:23 UTC; 41min ago
Docs: man:nginx(8)
Main PID: 2809 (nginx)
Tasks: 2 (limit: 1137)
Memory: 5.4M
CGroup: /system.slice/nginx.service
├─2809 nginx: master process /usr/sbin/nginx -g pid /run/nginx.pid; error_log stderr;
└─2810 nginx: worker process
This shows the service is active and running. The main PID and task counts are also shown.
If the service is stopped, you‘ll see Active: inactive (dead)
instead.
Enable Persistent Services
By default, services are started manually and not on boot. To start a service automatically on boot, enable it:
# systemctl enable service_name
For example, enabling nginx auto-start:
# systemctl enable nginx
To disable automatic start:
# systemctl disable service_name
Disabling nginx from auto-start:
# systemctl disable nginx
This way you can choose which services start on boot.
View System State and List Units
Beyond individual services, you can query systemd for high-level info.
To see all available unit types:
# systemctl -t help
This will show:
Available unit types:
service
socket
target
device
mount
automount
swap
timer
path
slice
scope
To show all installed unit files:
# systemctl list-unit-files
Sample output:
UNIT FILE STATE
proc-sys-fs-binfmt_misc.automount static
dev-hugepages.mount static
dev-mqueue.mount static
...
accounts-daemon.service enabled
acpid.service enabled
...
nginx.service enabled
postgresql.service enabled
...
This lists all units and their state (enabled, disabled, static, etc).
To filter for just services:
# systemctl list-unit-files --type=service
And to show only active units:
# systemctl list-units
Conclusion
I hope this overview helps you feel more confident managing services with systemctl! Let me know if you have any other questions. Systemd may seem complex at first, but with a bit of practice you‘ll be a pro in no time!
If you liked this guide and want to take your Linux skills to the next level, I highly recommend checking out this Linux Mastery course. It goes deep into system administration including systemd, networking, security, scripting, containers and more.
Anyway, thanks for reading and let me know if you have any other Linux topics you‘d like me to cover!