Master Linux systemd: Runlevels, systemctl commands, and creating custom services
This guide explains Linux runlevels, essential systemctl commands, key service directories, and walks through creating an additional SSH service and a custom shell‑based service using systemd, complete with step‑by‑step commands and example unit files.
Service Overview
In modern Linux distributions, services run as processes kept in memory and are managed uniformly with systemctl. Understanding runlevels, common systemctl operations, and service file locations is essential for system administration.
1. Runlevel Classification
> 运行级别0:系统停机状态
> 运行级别1:单用户工作状态,root权限,用于系统维护,禁止远程登陆
> 运行级别2:多用户状态(没有NFS)
> 运行级别3:完全的多用户状态(有NFS)
> 运行级别4:系统未使用,保留
> 运行级别5:X11控制台
> 运行级别6:系统正常关闭并重启2. Managing Services with systemctl (requires root)
> systemctl [opt] xxx.service
> status 查看当前服务状态
> start 启动服务
> stop 关闭服务
> restart 重启服务
> enable 设置开机启动
> disable 设置开机不启动
> reload 重新加载配置文件(不接具体服务名)
> mask 注销服务
> unmask 取消注销3. Common systemctl Commands
查看已启动的服务: systemctl list-units 查看所有服务单元文件: systemctl list-unit-files 查看服务依赖关系: systemctl list-dependencies <service>.service 查看反向依赖:
systemctl list-dependencies --reverse <service>.service4. Important Service Directories (CentOS example)
/usr/lib/systemd/system/ 系统默认的启动脚本目录
/etc/systemd/system/ 用户自定义的启动脚本目录
/etc/sysconfig/ 服务初始化选项目录
/var/lib/ 服务运行时产生的数据存储目录
/etc/xxx/ 各服务的配置目录5. Example: Adding a Second SSH Service on Port 8888
By default, OpenSSH listens on port 22. To run a second instance on port 8888, copy the original unit file and adjust the ExecStart line and configuration.
Step 1 – Duplicate the unit file
[Unit]
Description=OpenSSH server daemon
Documentation=man:sshd(8) man:sshd_config(5)
After=network.target sshd-keygen.service
Wants=sshd-keygen.service
[Service]
Type=notify
EnvironmentFile=/etc/sysconfig/sshd
ExecStart=/usr/sbin/sshd -D $OPTIONS
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
RestartSec=42s
[Install]
WantedBy=multi-user.targetSave this as /etc/systemd/system/sshd2.service (you may copy from /usr/lib/systemd/system/sshd.service first).
Step 2 – Adjust the ExecStart line
ExecStart=/usr/sbin/sshd -D $OPTIONS -f /etc/ssh/sshd2_configStep 3 – Create a new sshd configuration
# copy the default config
cp /etc/ssh/sshd_config /etc/ssh/sshd2_config
# edit the copy and change the listening port
Port 8888Step 4 – Reload systemd and start the new service
systemctl reload(reload unit files) systemctl status sshd2.service (verify status) systemctl start sshd2.service (start the service) systemctl enable sshd2.service (enable at boot)
Now you can connect from another machine with ssh fancy@<em>IP</em> -p 8888.
Notes:
Make sure the firewall allows traffic on port 8888.
Official recommendation is to place custom unit files under /etc/systemd/system/, though keeping them in /usr/lib/systemd/system/ also works.
6. Example: Creating a Simple Custom Service
We will create a shell script that writes a timestamped log file, then wrap it in a systemd unit.
Step 1 – Write the script
#!/bin/bash
logdate=$(date +%s)
logdir="/root/log/"
logname="fancy.${logdate}.log"
# echo $logname
touch ${logdir}${logname}Place this file at /root/bin/fancy_test.sh and make it executable:
chmod u+x /root/bin/fancy_test.shStep 2 – Create the unit file
[Unit]
Description=fancy_test server daemon
[Service]
Type=simple
ExecStart=/root/bin/fancy_test.sh
[Install]
WantedBy=multi-user.targetSave as /etc/systemd/system/fancy_test.service.
Step 3 – Reload and start
systemctl reload systemctl start fancy_test.serviceAfter starting, a log file will appear in /root/log/.
Note: This minimal service runs a single command, does not stay resident, and has no separate configuration file.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
